Skip to content

Commit 5272068

Browse files
Merge pull request #243 from AAluoxiang/master
第四周作业#1
2 parents ed28e62 + 399aeca commit 5272068

6 files changed

+211
-0
lines changed

Week_04/id_1/LeetCode_169_1.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package week01;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Auth:luoxiang
8+
* Time:2019/6/30 5:31 PM
9+
* Desc: 求众数
10+
*/
11+
public class LeetCode_169_1 {
12+
13+
public static void main(String[] args) {
14+
System.out.println(new LeetCode_169_1().majorityElement(new int[]{3,2,3}));
15+
System.out.println(new LeetCode_169_1().majorityElement(new int[]{2,2,1,1,1,2,2}));
16+
}
17+
18+
public int majorityElement(int[] nums) {
19+
if (nums == null || nums.length == 0) return 0;
20+
Map<Integer, Integer> map = new HashMap<>();
21+
for (int num : nums) {
22+
int temp = map.getOrDefault(num, 0) + 1;
23+
if (temp <= (nums.length / 2)) {
24+
map.put(num, map.getOrDefault(num, 0) + 1);
25+
} else {
26+
return num;
27+
}
28+
29+
}
30+
throw new IllegalArgumentException("异常");
31+
}
32+
}

Week_04/id_1/LeetCode_198_1.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package week04;
2+
3+
/**
4+
* Auth:luoxiang
5+
* Time:2019/6/30 5:46 PM
6+
* Desc: DP 打家劫舍
7+
*/
8+
public class LeetCode_198_1 {
9+
public static void main(String[] args) {
10+
System.out.println(new LeetCode_198_1().rob(new int[]{1,2,3,1}));
11+
System.out.println(new LeetCode_198_1().rob(new int[]{2,7,9,3,1}));
12+
}
13+
14+
public int rob(int[] nums) {
15+
int len = nums.length;
16+
if (len == 0 || len == 1) return len == 0 ? 0 : nums[0];
17+
int[] dp = new int[len];
18+
dp[0]=nums[0];
19+
dp[1]=Math.max(nums[0],nums[1]);
20+
for(int i=2;i<len;i++){
21+
dp[i]=Math.max(dp[i-1],dp[i-2]+nums[i]);
22+
}
23+
return dp[len-1];
24+
}
25+
}

Week_04/id_1/LeetCode_208_1.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class Trie {
2+
3+
TrieNode root;
4+
class TrieNode{
5+
char val;
6+
TrieNode [] child=new TrieNode[26];
7+
boolean isWord=false;
8+
public TrieNode(){}
9+
public TrieNode(char val){
10+
TrieNode node = new TrieNode();
11+
node.val=val;
12+
}
13+
}
14+
15+
/** Initialize your data structure here. */
16+
public Trie() {
17+
root=new TrieNode();
18+
}
19+
20+
/** Inserts a word into the trie. */
21+
public void insert(String word) {
22+
TrieNode node = this.root;
23+
for (char c : word.toCharArray()) {
24+
if(node.child[c-'a']==null){
25+
node.child[c-'a']=new TrieNode(c);
26+
}
27+
node=node.child[c-'a'];
28+
}
29+
node.isWord=true;
30+
}
31+
32+
/** Returns if the word is in the trie. */
33+
public boolean search(String word) {
34+
TrieNode node = this.root;
35+
for (char c : word.toCharArray()) {
36+
if(node.child[c-'a']==null){
37+
return false;
38+
}
39+
node=node.child[c-'a'];
40+
}
41+
return node.isWord;
42+
}
43+
44+
/** Returns if there is any word in the trie that starts with the given prefix. */
45+
public boolean startsWith(String prefix) {
46+
TrieNode node = this.root;
47+
for (char c : prefix.toCharArray()) {
48+
if(node.child[c-'a']==null){
49+
return false;
50+
}
51+
node=node.child[c-'a'];
52+
53+
}
54+
return true;
55+
}
56+
}
57+
58+
/**
59+
* Your Trie object will be instantiated and called as such:
60+
* Trie obj = new Trie();
61+
* obj.insert(word);
62+
* boolean param_2 = obj.search(word);
63+
* boolean param_3 = obj.startsWith(prefix);
64+
*/

Week_04/id_1/LeetCode_455_1.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package week01;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Auth:luoxiang
7+
* Time:2019/6/30 5:54 PM
8+
* Desc:
9+
*/
10+
public class LeetCode_455_1 {
11+
public static void main(String[] args) {
12+
System.out.println(new LeetCode_455_1().findContentChildren(new int[]{1,2},new int[]{1,2,3}));
13+
System.out.println(new LeetCode_455_1().findContentChildren(new int[]{1,2,3},new int[]{1,1}));
14+
}
15+
16+
public int findContentChildren(int[] g, int[] s) {
17+
if (g.length == 0 || s.length == 0) return 0;
18+
// g 与 s 排序
19+
Arrays.sort(g);
20+
Arrays.sort(s);
21+
int count=0;
22+
int gi=0,si=0;
23+
while (gi<g.length && si<s.length){
24+
if(g[gi]<=s[si]){
25+
count++;
26+
gi++;si++;
27+
}else{
28+
si++;
29+
}
30+
}
31+
return count;
32+
}
33+
}

Week_04/id_1/LeetCode_70_1.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package week01;
2+
3+
/**
4+
* Auth:luoxiang
5+
* Time:2019/6/30 5:17 PM
6+
* Desc: 爬楼梯
7+
*/
8+
public class LeetCode_70_1 {
9+
10+
public static void main(String[] args) {
11+
System.out.println(new LeetCode_70_1().climbStairs(3));
12+
}
13+
14+
public int climbStairs(int n) {
15+
if(n<3) return n;
16+
int[] dp = new int[n + 1];
17+
dp[1]=1;
18+
dp[2]=2;
19+
for(int i=3;i<=n;i++){
20+
dp[i]=dp[i-1]+dp[i-2];
21+
}
22+
return dp[n];
23+
24+
}
25+
}

Week_04/id_1/LeetCode_720_1.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package week01;
2+
3+
import java.util.Arrays;
4+
5+
public class LeetCode_720_1 {
6+
public static void main(String[] args) {
7+
System.out.println(new LeetCode_720_1().longestWord(new String[]{"w","wo","wor","worl", "world"}));
8+
}
9+
10+
public String longestWord(String[] words) {
11+
if (words == null || words.length == 0) return "";
12+
Arrays.sort(words);
13+
int len = 0;
14+
for (String word : words) {
15+
len=Math.max(len,word.length());
16+
}
17+
for (int i = len; i > 0; i--) {
18+
for (int j = 0; j < words.length; j++) {
19+
if (words[j].length() == i) {
20+
int temp = i;
21+
for (int k = j - 1; k >= 0; k--) {
22+
if (words[j].substring(0, temp - 1).equals(words[k])) {
23+
temp--;
24+
}
25+
}
26+
if (temp == 1) return words[j];
27+
}
28+
}
29+
}
30+
return "";
31+
}
32+
}

0 commit comments

Comments
 (0)