diff --git a/Week_01/id_50/LeetCode_1021_050.java b/Week_01/id_50/LeetCode_1021_050.java new file mode 100644 index 00000000..8c8b2f81 --- /dev/null +++ b/Week_01/id_50/LeetCode_1021_050.java @@ -0,0 +1,62 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Stack; + + + +/** + * @author yu + * 删除最外层的括号 + */ +public class LeetCode_1021_050 { + /** + * @param args + */ + public static void main(String[] args) { + LeetCode_1021_050 solution = new LeetCode_1021_050(); + System.out.println(solution.removeOuterParentheses("(())(()(()))")); + } + + public String removeOuterParentheses(String S) { + if(S == null){ + return null; + } + if(S.length() == 0){ + return ""; + } + char[] charArr = S.toCharArray(); + Stack stack = new Stack<>(); + StringBuilder strBuild = new StringBuilder(); + int n = 0; + Set setval = new HashSet<>(); + for(int i=0; i stack = new Stack<>(); + for(char c : charArr){ + if(stack.size()==0){ + stack.add(c); + }else{ + if(c == stack.get(stack.size()-1)){ + stack.pop(); + }else{ + stack.add(c); + } + } + } + StringBuilder strBuild = new StringBuilder(); + for(Character c: stack){ + strBuild.append(c); + } + return strBuild.toString(); + } +} \ No newline at end of file diff --git a/Week_01/id_50/LeetCode_104_050.java b/Week_01/id_50/LeetCode_104_050.java new file mode 100644 index 00000000..fba54a8a --- /dev/null +++ b/Week_01/id_50/LeetCode_104_050.java @@ -0,0 +1,32 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Stack; + + + +/** + * @author yu + * 二叉树的最大深度 + */ +public class LeetCode_441_050 { + /** + * @param args + */ + public static void main(String[] args) { + } + + public int maxDepth(TreeNode root) { + if(root == null){ + return 0; + }else{ + int left = maxDepth(root.left); + int right = maxDepth(root.right); + return Math.max(left, right)+1; + } + } +} \ No newline at end of file diff --git a/Week_01/id_50/LeetCode_111_050.java b/Week_01/id_50/LeetCode_111_050.java new file mode 100644 index 00000000..bb593e2e --- /dev/null +++ b/Week_01/id_50/LeetCode_111_050.java @@ -0,0 +1,43 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Stack; + + + +/** + * @author yu + * 二叉树的最小深度 + */ +public class LeetCode_111_050 { + /** + * @param args + */ + public static void main(String[] args) { + } + + public int minDepth(TreeNode root) { + if(root == null){ + return 0; + }else{ + if(root.left == null && root.right == null){ + return 1; + } + int min_val = Integer.MAX_VALUE; + if(root.left != null){ + int left = minDepth(root.left); + min_val = Math.min(left, min_val); + + } + if(root.right != null){ + int right = minDepth(root.right); + min_val = Math.min(right, min_val); + } + return min_val+1; + } + } +} \ No newline at end of file diff --git a/Week_01/id_50/LeetCode_189_050.java b/Week_01/id_50/LeetCode_189_050.java new file mode 100644 index 00000000..bb465c70 --- /dev/null +++ b/Week_01/id_50/LeetCode_189_050.java @@ -0,0 +1,62 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Stack; + + + +/** + * @author yu + * 删除排序数组中的重复项 + */ +public class LeetCode_189_050 { + /** + * @param args + */ + public static void main(String[] args) { + LeetCode_189_050 solution = new LeetCode_189_050(); + int[] nums = {1,2}; + String[] strs = {"nozzle","punjabi","waterlogged","imprison","crux","numismatists"}; +// System.out.println(solution.removeDuplicates(nums)); + solution.rotate(nums, 1); +// System.out.println(solution.isAnagram("dgqztusjuu", "dqugjzutss")); +// solution.groupAnagrams(strs); +// System.out.println(solution.removeDuplicates("abbacaacdad")); +// System.out.println(solution.removeOuterParentheses("(())(()(()))")); +// System.out.println(solution.arrangeCoins(2147483647)); +// System.out.println(solution.myPow(2, 10)); + } + + public void rotate(int[] nums, int k) { + k = k%nums.length; + if( k<=0 ){ + return; + } + int val = 0; + for(int i=0; i<(nums.length-k)/2; i++){ + val=nums[i]; + nums[i] = nums[nums.length-1-k-i]; + nums[nums.length-1-k-i] = val; + + } + for(int i=nums.length-k; i map1 = new HashMap<>(); + Map map2 = new HashMap<>(); + for(int i=0; i> groupAnagrams(String[] strs) { + if( strs.length == 0 ){ + return null; + } + Map mapList = new HashMap(); + for(int i=0; i listStr = new ArrayList<>(); + listStr.add(strs[i]); + mapList.put(sortStr, listStr); + }else{ + List listStr = mapList.get(sortStr); + listStr.add(strs[i]); + mapList.put(sortStr, listStr); + } + } + return new ArrayList(mapList.values()); + } +} \ No newline at end of file diff --git a/Week_01/id_50/LeetCode_50_050.java b/Week_01/id_50/LeetCode_50_050.java new file mode 100644 index 00000000..4f5b3381 --- /dev/null +++ b/Week_01/id_50/LeetCode_50_050.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Stack; + + + +/** + * @author yu + * 实现 pow(x, n) ,即计算 x 的 n 次幂函数。 + */ +public class LeetCode_50_050 { + /** + * @param args + */ + public static void main(String[] args) { + LeetCode_50_050 solution = new LeetCode_50_050(); + System.out.println(solution.myPow(2, 10)); + } + + private double calPow(double x, int n) { + if(n==0){ + return 1; + } + double half = calPow(x, n/2); + if(n%2 == 0){ + return half*half; + }else{ + return half*half*x; + } + } + public double myPow(double x, int n) { + if(n==0){ + return 1; + } + if(n>0){ + return calPow(x, n); + }else{ + return 1/calPow(x, Math.abs(n)); + } + } +} \ No newline at end of file