Skip to content

Commit 1ff4223

Browse files
Merge pull request #35 from flamencoy/master
第一周作业#33
2 parents 5c68b95 + ceda8eb commit 1ff4223

File tree

7 files changed

+149
-0
lines changed

7 files changed

+149
-0
lines changed

Week_01/id_33/LeetCode_101_033.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode id=101 lang=java
3+
*
4+
* [101] Symmetric Tree
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* public class TreeNode {
9+
* int val;
10+
* TreeNode left;
11+
* TreeNode right;
12+
* TreeNode(int x) { val = x; }
13+
* }
14+
*/
15+
class Solution {
16+
public boolean isSymmetric(TreeNode root) {
17+
return isSym(root, root);
18+
}
19+
20+
public boolean isSym(TreeNode leftNode, TreeNode rightNode) {
21+
if(leftNode == null && rightNode == null)
22+
return true;
23+
if(leftNode == null || rightNode == null || leftNode.val != rightNode.val)
24+
return false;
25+
return isSym(leftNode.left, rightNode.right) && isSym(leftNode.right, rightNode.left);
26+
}
27+
}
28+

Week_01/id_33/LeetCode_104_033.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @lc app=leetcode id=104 lang=java
3+
*
4+
* [104] Maximum Depth of Binary Tree
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* public class TreeNode {
9+
* int val;
10+
* TreeNode left;
11+
* TreeNode right;
12+
* TreeNode(int x) { val = x; }
13+
* }
14+
*/
15+
class Solution {
16+
public int maxDepth(TreeNode root) {
17+
if(root == null)
18+
return 0;
19+
int leftMaxDepth = maxDepth(root.left);
20+
int rightMaxDepth = maxDepth(root.right);
21+
return Math.max(leftMaxDepth, rightMaxDepth) + 1;
22+
}
23+
}
24+

Week_01/id_33/LeetCode_24_033.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @lc app=leetcode id=24 lang=java
3+
*
4+
* [24] Swap Nodes in Pairs
5+
*/
6+
/**
7+
* Definition for singly-linked list.
8+
* public class ListNode {
9+
* int val;
10+
* ListNode next;
11+
* ListNode(int x) { val = x; }
12+
* }
13+
*/
14+
class Solution {
15+
public ListNode swapPairs(ListNode head) {
16+
ListNode pre = new ListNode(0);
17+
ListNode cur = head;
18+
pre.next = head;
19+
ListNode tmp = pre;
20+
while(cur != null){
21+
ListNode next = cur.next;
22+
if(next != null){
23+
cur.next = next.next;
24+
next.next = cur;
25+
pre.next = next;
26+
pre = cur;
27+
cur = cur.next;
28+
} else
29+
break;
30+
}
31+
return tmp.next;
32+
}
33+
}
34+

Week_01/id_33/LeetCode_26_033.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @lc app=leetcode id=26 lang=java
3+
*
4+
* [26] Remove Duplicates from Sorted Array
5+
*/
6+
class Solution {
7+
public int removeDuplicates(int[] nums) {
8+
int len = nums.length;
9+
if(len == 0)
10+
return 0;
11+
int cur = 0;
12+
for(int i = 0; i < len; i++){
13+
if(nums[i] != nums[cur]){
14+
cur++;
15+
nums[cur] = nums[i];
16+
}
17+
}
18+
return cur + 1;
19+
}
20+
}
21+

Week_01/id_33/LeetCode_49_033.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.HashMap;
4+
5+
/*
6+
* @lc app=leetcode id=49 lang=java
7+
*
8+
* [49] Group Anagrams
9+
*/
10+
class Solution {
11+
public List<List<String>> groupAnagrams(String[] strs) {
12+
Map<String, List<String>> sortedMap = new HashMap<>();
13+
for (String word : strs) {
14+
char[] charWord = word.toCharArray();
15+
Arrays.sort(charWord);
16+
String newStr = new String(charWord);
17+
sortedMap.putIfAbsent(newStr,new ArrayList<>());
18+
sortedMap.get(newStr).add(word);
19+
}
20+
return new ArrayList<>(sortedMap.values());
21+
}
22+
}
23+

Week_01/id_33/LeetCode_50_033.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @lc app=leetcode id=50 lang=java
3+
*
4+
* [50] Pow(x, n)
5+
*/
6+
class Solution {
7+
public double myPow(double x, int n) {
8+
if(n == 0)
9+
return 1;
10+
if(n == 1)
11+
return x;
12+
double half = myPow(x, n/2);
13+
if(n % 2 == 0)
14+
return half * half;
15+
else
16+
return n > 0 ? half * half * x : half * half * (1/x);
17+
}
18+
}
19+
129 KB
Loading

0 commit comments

Comments
 (0)