Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Week_04/id_134/LeetCode_169_134.java
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;

}
}
76 changes: 76 additions & 0 deletions Week_04/id_134/LeetCode_211_134.java
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);
*/
14 changes: 14 additions & 0 deletions Week_04/id_134/LeetCode_455_134.java
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++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

贪心还是DP?

if (g[gi] <= s[si]) {
cnt ++;
gi ++;
}
}
return cnt;
}
}
73 changes: 73 additions & 0 deletions Week_04/id_134/LeetCode_720_134.java
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;
}
}
}
22 changes: 22 additions & 0 deletions Week_04/id_134/LeetCode_784_134.java
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);
}
}