Skip to content

Commit dea8b17

Browse files
Merge pull request #398 from plin005/master
039 to commit week002 homework (LeetCode_242 & LeetCode_671)
2 parents d51b21e + 7be6014 commit dea8b17

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

Week_01/id_39/LeetCode_083_039.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.paula.algorithmsAndDataStructure.leetCode_83;
2+
3+
public class LeetCode_083_039 {
4+
public ListNode deleteDuplicates(ListNode head) {
5+
if(head == null) return null;
6+
7+
ListNode p = head;
8+
ListNode q = null;
9+
while(p.next != null) {
10+
if(p.val == p.next.val) {
11+
if(q == null || (q != null && q.val != p.val)) {
12+
q = p;
13+
}
14+
}else {
15+
if(q != null && q.val == p.val) {
16+
q.next = p.next;
17+
}
18+
}
19+
p = p.next;
20+
}
21+
22+
if(q != null && p.val == q.val && p.next == null) {
23+
q.next = null;
24+
}
25+
return head;
26+
}
27+
28+
}

Week_01/id_39/LeetCode_922_039.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.paula.algorithmsAndDataStructure;
2+
3+
public class LeetCode_922_039 {
4+
public int[] sortArrayByParityII(int[] A) {
5+
int len = A.length;
6+
int i=0; //偶数下标
7+
int j=i+1; //奇数下标
8+
Boolean evenIndexOddValue = false;
9+
Boolean oddIndexEvenValue = false;
10+
11+
while (i < len-1 && j < len) {
12+
evenIndexOddValue = isOdd(A[i]);
13+
oddIndexEvenValue= isEven(A[j]);
14+
15+
if(!evenIndexOddValue) i +=2;
16+
if(!oddIndexEvenValue) j +=2;
17+
if(evenIndexOddValue && oddIndexEvenValue) {
18+
int tempVal = A[i];
19+
A[i] = A[j];
20+
A[j] = tempVal;
21+
22+
i += 2;
23+
j += 2;
24+
25+
}
26+
}
27+
28+
29+
return A;
30+
}
31+
32+
public boolean isEven(int value) {
33+
34+
return value%2 == 0 ? true : false;
35+
}
36+
37+
public boolean isOdd(int value) {
38+
39+
return value%2 == 1 ? true : false;
40+
}
41+
42+
}

Week_02/id_39/LeetCode_242_039.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
/**
6+
*
7+
* @author Paula
8+
*
9+
*
10+
*/
11+
12+
class Solution {
13+
private static void chechString(String s, Map<Character, Integer> smap) {
14+
for (int i = 0; i < s.length(); i++) {
15+
if (smap.containsKey(s.charAt(i))) {
16+
smap.put(s.charAt(i), smap.get(s.charAt(i)) + 1);
17+
} else {
18+
smap.put(s.charAt(i), 1);
19+
}
20+
}
21+
}
22+
23+
public static boolean isAnagram(String s, String t) {
24+
if (s.equals(t)) {
25+
return true;
26+
} else if (null == s || null == t || s.length() != t.length() || s.length() <= 0) {//如果长度不相等,直接返回
27+
return false;
28+
}
29+
Map<Character, Integer> smap = new HashMap<Character, Integer>();
30+
chechString(s, smap);
31+
for (int i = 0; i < t.length(); i++) {
32+
char tmp = t.charAt(i);
33+
if (smap.containsKey(tmp)) {
34+
smap.put(tmp, smap.get(tmp) - 1);
35+
}
36+
if (0 == smap.get(tmp)) {
37+
smap.remove(tmp);
38+
}
39+
}
40+
41+
return smap.size() == 0;
42+
}
43+
44+
public static void main(String[] args) {
45+
//("anagram", new HashMap<Character, Integer>());
46+
System.out.println(isAnagram("anagram", "nagaram"));
47+
System.out.println(isAnagram("rat", "car"));
48+
}
49+
}

Week_02/id_39/LeetCode_671_039.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
/**
3+
*
4+
* @author Paula
5+
*
6+
* 执行结果:
7+
* 执行用时 : 1 ms, 在Second Minimum Node In a Binary Tree的Java提交中击败了86.02% 的用户
8+
* 内存消耗 : 34 MB, 在Second Minimum Node In a Binary Tree的Java提交中击败了77.61% 的用户
9+
*
10+
* test case:
11+
* [2,2,5,null,null,5,7]
12+
* [1,1,3,1,1,3,4,3,1]
13+
* [5,8,5]
14+
*/
15+
16+
class Solution {
17+
public int findSecondMinimumValue(TreeNode root) {
18+
if(root == null) return -1;
19+
//第二小节点值
20+
int secondMin = -1;
21+
22+
TreeNode p = root; //第二小的节点
23+
24+
//检查左节点
25+
if (p.left != null) {
26+
//如果左子节点值不等于当前节点值, 则左子节点值必然大于当前节点的值
27+
if(p.left.val != p.val) secondMin = p.left.val;
28+
//如果左子节点值不等于当前节点值, 继续遍历左子节点下的分支
29+
else secondMin = findSecondMinimumValue(p.left);
30+
}
31+
32+
//检查右节点
33+
if (p.right != null) {
34+
//如果右子节点值不等于当前节点值, 则右子节点值必然大于当前节点的值
35+
if(p.right.val != p.val) {
36+
//对比从左子树得到的secondMin和右子节点的值
37+
if(secondMin == -1 || (secondMin != -1 && p.right.val < secondMin) )
38+
secondMin = p.right.val;
39+
40+
//如果右子节点值不等于当前节点值, 继续遍历右子节点下的分支
41+
} else {
42+
int tmp = findSecondMinimumValue(p.right);
43+
if (tmp !=-1 && secondMin !=-1 && tmp < secondMin)
44+
secondMin = tmp;
45+
}
46+
47+
}
48+
49+
return secondMin;
50+
}
51+
}

0 commit comments

Comments
 (0)