-
Notifications
You must be signed in to change notification settings - Fork 149
134_week4_homework #670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
134_week4_homework #670
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| public class Solution { | ||
| public int majorityElement(int[] num) { | ||
|
|
||
| int ans = 0, cnt = 0; | ||
| for(int i=0; i<num.length; i++) { | ||
| if(cnt == 0) { | ||
| ans = num[i]; | ||
| cnt ++; | ||
| } else { | ||
| if(ans == num[i]) { | ||
| cnt ++; | ||
| } else { | ||
| cnt --; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ans; | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| class WordDictionary { | ||
|
|
||
| TrieNode root; | ||
| /** Initialize your data structure here. */ | ||
| public WordDictionary() { | ||
| root = new TrieNode(); | ||
| } | ||
|
|
||
| /** Adds a word into the data structure. */ | ||
| public void addWord(String word) { | ||
| if (word == null || word.length() <= 0) | ||
| return; | ||
| TrieNode node = root; | ||
| for (int i=0; i<word.length(); i++) { | ||
| char ch = word.charAt(i); | ||
| int pos = ch - 'a'; | ||
| if (node.sons[pos] == null) { | ||
| node.sons[pos] = new TrieNode(ch); | ||
| } | ||
| node = node.sons[pos]; | ||
| } | ||
| node.isEnd = true; | ||
| } | ||
|
|
||
| /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ | ||
| public boolean search(String word) { | ||
| return patternSearch(word, root); | ||
| } | ||
|
|
||
| public boolean patternSearch(String word, TrieNode node) { | ||
| for (int i=0; i<word.length(); i++) { | ||
| char ch = word.charAt(i); | ||
| int pos = ch -'a'; | ||
| if (ch != '.') { | ||
| if (node != null && node.sons[pos] != null) { | ||
| node = node.sons[pos]; | ||
| } else return false; | ||
| } else { | ||
| int cnt = 0; | ||
| for (int j=0; j<26; j++) { | ||
| if (node.sons[j] != null) { | ||
| if (patternSearch(word.substring(i+1), node.sons[j])) return true; | ||
| else cnt ++; | ||
| } else { | ||
| cnt ++; | ||
| continue; | ||
| } | ||
| } | ||
| if (cnt == 26) return false; | ||
| } | ||
| } | ||
| return node.isEnd; | ||
| } | ||
|
|
||
| class TrieNode { | ||
| char val; | ||
| boolean isEnd; | ||
| TrieNode[] sons; | ||
| public TrieNode() { | ||
| isEnd = false; | ||
| sons = new TrieNode[26]; | ||
| } | ||
| public TrieNode(char val) { | ||
| this.val = val; | ||
| isEnd = false; | ||
| sons = new TrieNode[26]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Your WordDictionary object will be instantiated and called as such: | ||
| * WordDictionary obj = new WordDictionary(); | ||
| * obj.addWord(word); | ||
| * boolean param_2 = obj.search(word); | ||
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class Solution { | ||
| public int findContentChildren(int[] g, int[] s) { | ||
| Arrays.sort(g); | ||
| Arrays.sort(s); | ||
| int cnt = 0; | ||
| for (int gi = 0, si = 0; gi < g.length && si < s.length; si++) { | ||
| if (g[gi] <= s[si]) { | ||
| cnt ++; | ||
| gi ++; | ||
| } | ||
| } | ||
| return cnt; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| class Solution { | ||
| TrieNode root = new TrieNode(); | ||
| StringBuilder curWord = new StringBuilder(); | ||
| String ret = null; | ||
| public String longestWord(String[] words) { | ||
| if (words == null) | ||
| return null; | ||
| for (String word : words) { | ||
| insert(word); | ||
| } | ||
| traverse(root); | ||
| return ret; | ||
| } | ||
|
|
||
| public void traverse(TrieNode root) { | ||
| if (root.isLeaf()) { | ||
| if (curWord != null) { | ||
| if (ret == null || ret.length() < curWord.length() || | ||
| (ret.length() == curWord.length() && ret.compareTo(curWord.toString()) > 0)) { | ||
| ret = curWord.toString(); | ||
| } | ||
| } | ||
| return; | ||
| } | ||
| for (int i=0; i<26; i++) { | ||
| if (root.sons[i] != null && root.sons[i].isEnd) { | ||
| curWord.append(root.sons[i].val); | ||
| traverse(root.sons[i]); | ||
| curWord.deleteCharAt(curWord.length()-1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void insert(String word) { | ||
| TrieNode node = root; | ||
| for (int i=0; i<word.length(); i++) { | ||
| char ch = word.charAt(i); | ||
| int pos = ch - 'a'; | ||
| if (node.sons[pos] == null) { | ||
| node.sons[pos] = new TrieNode(ch); | ||
| } | ||
| node = node.sons[pos]; | ||
| } | ||
| node.isEnd = true; | ||
| } | ||
|
|
||
| class TrieNode { | ||
| char val; | ||
| boolean isEnd; | ||
| TrieNode[] sons; | ||
| public TrieNode() { | ||
| isEnd = false; | ||
| sons = new TrieNode[26]; | ||
| } | ||
| public TrieNode(char val) { | ||
| this.val = val; | ||
| isEnd = false; | ||
| sons = new TrieNode[26]; | ||
| } | ||
| public boolean isLeaf() { | ||
| boolean leafFlag = true, endFlag = true; | ||
| for (int i=0; i<sons.length; i++) { | ||
| if (sons[i] != null) { | ||
| leafFlag = false; | ||
| if (sons[i].isEnd) { | ||
| endFlag = false; | ||
| } | ||
| } | ||
| } | ||
| return leafFlag || endFlag; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| class Solution { | ||
| List<String> ret = new ArrayList<>(); | ||
| public List<String> letterCasePermutation(String S) { | ||
| if (S == null || S.length() <= 0) | ||
| return ret; | ||
| dfs(S.toCharArray(), 0); | ||
| return ret; | ||
| } | ||
|
|
||
| public void dfs(char[] chs, int idx) { | ||
| if (chs.length == idx) { | ||
| ret.add(new String(chs)); | ||
| return; | ||
| } | ||
| if (Character.isLetter(chs[idx])) { | ||
| chs[idx] = Character.toLowerCase(chs[idx]); | ||
| dfs(chs, idx+1); | ||
| chs[idx] = Character.toUpperCase(chs[idx]); | ||
| } | ||
| dfs(chs, idx+1); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
贪心还是DP?