diff --git a/Week_01/id_33/LeetCode_101_033.java b/Week_01/id_33/LeetCode_101_033.java new file mode 100644 index 00000000..c5378e95 --- /dev/null +++ b/Week_01/id_33/LeetCode_101_033.java @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode id=101 lang=java + * + * [101] Symmetric Tree + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public boolean isSymmetric(TreeNode root) { + return isSym(root, root); + } + + public boolean isSym(TreeNode leftNode, TreeNode rightNode) { + if(leftNode == null && rightNode == null) + return true; + if(leftNode == null || rightNode == null || leftNode.val != rightNode.val) + return false; + return isSym(leftNode.left, rightNode.right) && isSym(leftNode.right, rightNode.left); + } +} + diff --git a/Week_01/id_33/LeetCode_104_033.java b/Week_01/id_33/LeetCode_104_033.java new file mode 100644 index 00000000..e242f458 --- /dev/null +++ b/Week_01/id_33/LeetCode_104_033.java @@ -0,0 +1,24 @@ +/* + * @lc app=leetcode id=104 lang=java + * + * [104] Maximum Depth of Binary Tree + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public int maxDepth(TreeNode root) { + if(root == null) + return 0; + int leftMaxDepth = maxDepth(root.left); + int rightMaxDepth = maxDepth(root.right); + return Math.max(leftMaxDepth, rightMaxDepth) + 1; + } +} + diff --git a/Week_01/id_33/LeetCode_24_033.java b/Week_01/id_33/LeetCode_24_033.java new file mode 100644 index 00000000..2d924dc0 --- /dev/null +++ b/Week_01/id_33/LeetCode_24_033.java @@ -0,0 +1,34 @@ +/* + * @lc app=leetcode id=24 lang=java + * + * [24] Swap Nodes in Pairs + */ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode swapPairs(ListNode head) { + ListNode pre = new ListNode(0); + ListNode cur = head; + pre.next = head; + ListNode tmp = pre; + while(cur != null){ + ListNode next = cur.next; + if(next != null){ + cur.next = next.next; + next.next = cur; + pre.next = next; + pre = cur; + cur = cur.next; + } else + break; + } + return tmp.next; + } +} + diff --git a/Week_01/id_33/LeetCode_26_033.java b/Week_01/id_33/LeetCode_26_033.java new file mode 100644 index 00000000..d881d791 --- /dev/null +++ b/Week_01/id_33/LeetCode_26_033.java @@ -0,0 +1,21 @@ +/* + * @lc app=leetcode id=26 lang=java + * + * [26] Remove Duplicates from Sorted Array + */ +class Solution { + public int removeDuplicates(int[] nums) { + int len = nums.length; + if(len == 0) + return 0; + int cur = 0; + for(int i = 0; i < len; i++){ + if(nums[i] != nums[cur]){ + cur++; + nums[cur] = nums[i]; + } + } + return cur + 1; + } +} + diff --git a/Week_01/id_33/LeetCode_49_033.java b/Week_01/id_33/LeetCode_49_033.java new file mode 100644 index 00000000..8bac726c --- /dev/null +++ b/Week_01/id_33/LeetCode_49_033.java @@ -0,0 +1,23 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; + +/* + * @lc app=leetcode id=49 lang=java + * + * [49] Group Anagrams + */ +class Solution { + public List> groupAnagrams(String[] strs) { + Map> sortedMap = new HashMap<>(); + for (String word : strs) { + char[] charWord = word.toCharArray(); + Arrays.sort(charWord); + String newStr = new String(charWord); + sortedMap.putIfAbsent(newStr,new ArrayList<>()); + sortedMap.get(newStr).add(word); + } + return new ArrayList<>(sortedMap.values()); + } +} + diff --git a/Week_01/id_33/LeetCode_50_033.java b/Week_01/id_33/LeetCode_50_033.java new file mode 100644 index 00000000..c3e3b879 --- /dev/null +++ b/Week_01/id_33/LeetCode_50_033.java @@ -0,0 +1,19 @@ +/* + * @lc app=leetcode id=50 lang=java + * + * [50] Pow(x, n) + */ +class Solution { + public double myPow(double x, int n) { + if(n == 0) + return 1; + if(n == 1) + return x; + double half = myPow(x, n/2); + if(n % 2 == 0) + return half * half; + else + return n > 0 ? half * half * x : half * half * (1/x); + } +} + diff --git "a/Week_01/id_33/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\350\204\221\345\233\276.png" "b/Week_01/id_33/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\350\204\221\345\233\276.png" new file mode 100644 index 00000000..098b6914 Binary files /dev/null and "b/Week_01/id_33/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\350\204\221\345\233\276.png" differ