diff --git a/java/001. Two Sum.java b/java/001. Two Sum.java index dd744ad..e69de29 100644 --- a/java/001. Two Sum.java +++ b/java/001. Two Sum.java @@ -1,18 +0,0 @@ -public class Solution { - public int[] twoSum(int[] numbers, int target) { - int[] res = new int[2]; - Map map = new HashMap(); - for (int i = 0; i < numbers.length; i++) { - map.put(numbers[i], i); - } - for (int i = 0; i < numbers.length; i++) { - int gap = target - numbers[i]; - if (map.get(gap) != null && (int) map.get(gap) != i) { - res[0] = i + 1; - res[1] = (int) map.get(gap) + 1; - break; - } - } - return res; - } -} \ No newline at end of file diff --git a/java/002. Add Two Numbers.java b/java/002. Add Two Numbers.java index b5a515d..665b53d 100644 --- a/java/002. Add Two Numbers.java +++ b/java/002. Add Two Numbers.java @@ -8,19 +8,19 @@ */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { - return add(l1,l2,true); + return add(l1,l2,true); } public ListNode add(ListNode l1, ListNode l2,boolean flag) { - boolean a=l1==null; - boolean b=l2==null; - if (a&&b&&flag) { - return null; - } - int val=(a?0:l1.val)+(b?0:l2.val)+(flag?0:1); - int v=val%10; - boolean f=(v==val); - ListNode answer=new ListNode(v); - answer.next=add(a?null:l1.next,b?null:l2.next,f); - return answer; + boolean a=l1==null; + boolean b=l2==null; + if (a&&b&&flag) { + return null; + } + int val=(a?0:l1.val)+(b?0:l2.val)+(flag?0:1); + int v=val%10; + boolean f=(v==val); + ListNode answer=new ListNode(v); + answer.next=add(a?null:l1.next,b?null:l2.next,f); + return answer; } } \ No newline at end of file diff --git a/java/003. Longest Substring Without Repeating Characters.java b/java/003. Longest Substring Without Repeating Characters.java index 0a9bf4f..35a8328 100644 --- a/java/003. Longest Substring Without Repeating Characters.java +++ b/java/003. Longest Substring Without Repeating Characters.java @@ -4,20 +4,20 @@ public int lengthOfLongestSubstring(String s) { Map map=new HashMap(); List list=new LinkedList(); for (int i = 0; i < s.length(); i++) { - String k=s.substring(i, i+1); - if (map.get(k) != null) { - int n=map.get(k)-map.get(list.get(0))+1; - for (int j = 0; j < n; j++) { - map.remove(list.get(0)); - list.remove(0); - } - } - map.put(k, i); - list.add(k); - if (list.size()>answer) { - answer=list.size(); - } - } - return answer; + String k=s.substring(i, i+1); + if (map.get(k) != null) { + int n=map.get(k)-map.get(list.get(0))+1; + for (int j = 0; j < n; j++) { + map.remove(list.get(0)); + list.remove(0); + } + } + map.put(k, i); + list.add(k); + if (list.size()>answer) { + answer=list.size(); + } + } + return answer; } } \ No newline at end of file diff --git a/java/006. ZigZag Conversion.java b/java/006. ZigZag Conversion.java index 4cbf70a..3b63194 100644 --- a/java/006. ZigZag Conversion.java +++ b/java/006. ZigZag Conversion.java @@ -5,28 +5,28 @@ public String convert(String s, int numRows) { }else{ int k=numRows*2-2; String[] res=new String[numRows]; - for (int i = 0; i < res.length; i++) { - res[i]=""; - } - int step=0; - int j=0; - for (int i = 0; i < s.length(); i++) { - step++; - if (step<=numRows) { - j=step; - }else { - j=numRows-(step-numRows); - } - res[j-1]+=s.substring(i, i+1); - if (step==k) { - step=0; - } - } - String result=""; - for (int i = 0; i < res.length; i++) { - result+=res[i]; - } - return result; + for (int i = 0; i < res.length; i++) { + res[i]=""; + } + int step=0; + int j=0; + for (int i = 0; i < s.length(); i++) { + step++; + if (step<=numRows) { + j=step; + }else { + j=numRows-(step-numRows); + } + res[j-1]+=s.substring(i, i+1); + if (step==k) { + step=0; + } + } + String result=""; + for (int i = 0; i < res.length; i++) { + result+=res[i]; + } + return result; } } } \ No newline at end of file diff --git a/java/007. Reverse Integer.java b/java/007. Reverse Integer.java index 50de11e..3eceb9d 100644 --- a/java/007. Reverse Integer.java +++ b/java/007. Reverse Integer.java @@ -1,24 +1,24 @@ public class Solution { public int reverse(int x) { String str=String.valueOf(x); - String r=""; - int re=0; - if (x<0) { - for (int i = str.length()-1; i >0 ; i--) { - r+=str.substring(i,i+1); - } - r="-"+r; - }else { - for (int i = str.length()-1; i >-1 ; i--) { - r+=str.substring(i,i+1); - } - } - try { - re=Integer.parseInt(r); - - } catch (Exception e) { - - } - return re; + String r=""; + int re=0; + if (x<0) { + for (int i = str.length()-1; i >0 ; i--) { + r+=str.substring(i,i+1); + } + r="-"+r; + }else { + for (int i = str.length()-1; i >-1 ; i--) { + r+=str.substring(i,i+1); + } + } + try { + re=Integer.parseInt(r); + + } catch (Exception e) { + + } + return re; } } \ No newline at end of file diff --git a/java/009. Palindrome Number.java b/java/009. Palindrome Number.java index 5556a02..0d2fce7 100644 --- a/java/009. Palindrome Number.java +++ b/java/009. Palindrome Number.java @@ -1,35 +1,35 @@ public class Solution { public boolean isPalindrome(int x) { - if (x < 0) { - return false; - } - int n = x; - int a = 0; - int p = 1; - while (x != 0) { - x /= 10; - a++; - p *= 10; - if (a==1) { - p/=10; - } - } - int b = a / 2; - if (b == 0) { - return true; - } + if (x < 0) { + return false; + } + int n = x; + int a = 0; + int p = 1; + while (x != 0) { + x /= 10; + a++; + p *= 10; + if (a==1) { + p/=10; + } + } + int b = a / 2; + if (b == 0) { + return true; + } - for (int i = 0; i < b; i++) { - int begin = n / p; - int end = n % 10; - if (begin != end) { - return false; - } else { - n = n - (begin * p); - p /= 100; - n = (n - end) / 10; - } - } - return true; - } + for (int i = 0; i < b; i++) { + int begin = n / p; + int end = n % 10; + if (begin != end) { + return false; + } else { + n = n - (begin * p); + p /= 100; + n = (n - end) / 10; + } + } + return true; + } } \ No newline at end of file diff --git a/java/010. Regular Expression Matching.java b/java/010. Regular Expression Matching.java index ecca3f8..baa2f1f 100644 --- a/java/010. Regular Expression Matching.java +++ b/java/010. Regular Expression Matching.java @@ -1,53 +1,53 @@ public class Solution { public boolean isMatch(String s, String p) { if (p.length()==0) { - return s.length()==0; - } + return s.length()==0; + } if (s.length()==0) { - if (p.length()%2!=0) { - return false; - } - char[] c=p.toCharArray(); - for (int i = 1; i < c.length; i+=2) { - if (c[i]!='*') { - return false; - } - } - return true; - } + if (p.length()%2!=0) { + return false; + } + char[] c=p.toCharArray(); + for (int i = 1; i < c.length; i+=2) { + if (c[i]!='*') { + return false; + } + } + return true; + } int pl=p.length(); int sl=s.length(); String ep=p.substring(pl-1,pl); if (ep.equals("*")) { - if (p.substring(pl-2,pl-1).equals(".")) { - for (int i = sl-1; i >=0-1; i--) { - if (isMatch(s.substring(0, i+1),p.substring(0, pl-2))) { - return true; - } - } - return false; - }else { - String k=p.substring(pl-2,pl-1); - for (int i = sl-1; i >=0; i--) { - String t=s.substring(i,i+1); - if (t.equals(k)) { - if (isMatch(s.substring(0, i+1),p.substring(0, pl-2))) { - return true; - } - }else { - return isMatch(s.substring(0, i+1),p.substring(0, pl-2)); - } - } - return isMatch("",p.substring(0, pl-2)); - } - }else if(ep.equals(".")){ - return isMatch(s.substring(0, sl-1),p.substring(0, pl-1)); - }else { - String es=s.substring(sl-1,sl); - if (ep.equals(es)) { - return isMatch(s.substring(0, sl-1),p.substring(0, pl-1)); - } - return false; - } + if (p.substring(pl-2,pl-1).equals(".")) { + for (int i = sl-1; i >=0-1; i--) { + if (isMatch(s.substring(0, i+1),p.substring(0, pl-2))) { + return true; + } + } + return false; + }else { + String k=p.substring(pl-2,pl-1); + for (int i = sl-1; i >=0; i--) { + String t=s.substring(i,i+1); + if (t.equals(k)) { + if (isMatch(s.substring(0, i+1),p.substring(0, pl-2))) { + return true; + } + }else { + return isMatch(s.substring(0, i+1),p.substring(0, pl-2)); + } + } + return isMatch("",p.substring(0, pl-2)); + } + }else if(ep.equals(".")){ + return isMatch(s.substring(0, sl-1),p.substring(0, pl-1)); + }else { + String es=s.substring(sl-1,sl); + if (ep.equals(es)) { + return isMatch(s.substring(0, sl-1),p.substring(0, pl-1)); + } + return false; + } } } \ No newline at end of file diff --git a/java/011. Container With Most Water.java b/java/011. Container With Most Water.java index b47e8dc..028d5bd 100644 --- a/java/011. Container With Most Water.java +++ b/java/011. Container With Most Water.java @@ -4,16 +4,16 @@ public int maxArea(int[] height) { int right=height.length-1; int ans=(right-left)*Math.min(height[left], height[right]); while (right>left) { - int water=(right-left)*Math.min(height[left], height[right]); - if (water>ans) { - ans=water; - } - if (height[left]ans) { + ans=water; + } + if (height[left]> ans=new ArrayList>(); + List> ans=new ArrayList>(); public List> threeSum(int[] nums) { Arrays.sort(nums); - if (nums.length<3) { - return ans; - } - int length=nums.length; - for (int i = 0; i < length-2; i++) { - if (i==0||nums[i]!=nums[i-1]) { - f(nums,nums[i],i+1,length-1); - } - } - return ans; + if (nums.length<3) { + return ans; + } + int length=nums.length; + for (int i = 0; i < length-2; i++) { + if (i==0||nums[i]!=nums[i-1]) { + f(nums,nums[i],i+1,length-1); + } + } + return ans; } public void f(int[] nums,int target,int s,int e){ - while (s list=new ArrayList(); - list.add(target); - list.add(nums[s]); - list.add(nums[e]); - ans.add(list); - while (s list=new ArrayList(); + list.add(target); + list.add(nums[s]); + list.add(nums[e]); + ans.add(list); + while (s letterCombinations(String digits) { - //存放答案的list + //存放答案的list List list=new ArrayList(); //建立map存放手机按键 Map> map=new HashMap>(); @@ -48,33 +48,33 @@ public List letterCombinations(String digits) { map.put("9", list9); //循环判断 for (int i = 0; i < digits.length(); i++) { - String s=digits.substring(i,i+1); - //如果能查到数字键盘 - if (map.get(s)!=null) { - //如果list不为空,即并非刚开始 - if (list.size()!=0) { - //记住目前list的长度 - int size=list.size(); - //循环list当前长度 - for (int j = 0; j < size; j++) { - //将list中的每个值与接下来的对应数字键盘的可能性进行拼接 - for (int k = 0; k =0; j--) { - list.remove(j); - } - }else { - for (int j = 0; j < map.get(s).size(); j++) { - String a=map.get(s).get(j); - list.add(a); - } - } - } - } + String s=digits.substring(i,i+1); + //如果能查到数字键盘 + if (map.get(s)!=null) { + //如果list不为空,即并非刚开始 + if (list.size()!=0) { + //记住目前list的长度 + int size=list.size(); + //循环list当前长度 + for (int j = 0; j < size; j++) { + //将list中的每个值与接下来的对应数字键盘的可能性进行拼接 + for (int k = 0; k =0; j--) { + list.remove(j); + } + }else { + for (int j = 0; j < map.get(s).size(); j++) { + String a=map.get(s).get(j); + list.add(a); + } + } + } + } return list; } } \ No newline at end of file diff --git a/java/018. 4Sum.java b/java/018. 4Sum.java index 8cc6112..d31f641 100644 --- a/java/018. 4Sum.java +++ b/java/018. 4Sum.java @@ -1,45 +1,45 @@ public class Solution { - List> ans=new ArrayList>(); + List> ans=new ArrayList>(); public List> fourSum(int[] nums, int target) { if (nums.length<4) { - return ans; - } - Arrays.sort(nums); - int length=nums.length; - for (int i = 0; i =i+3; j--) { - if ((i==0||nums[i]!=nums[i-1])&&(j==length-1||nums[j]!=nums[j+1])) { - f(nums,target,i,i+1,j-1,j); - } - } - } - return ans; + return ans; + } + Arrays.sort(nums); + int length=nums.length; + for (int i = 0; i =i+3; j--) { + if ((i==0||nums[i]!=nums[i-1])&&(j==length-1||nums[j]!=nums[j+1])) { + f(nums,target,i,i+1,j-1,j); + } + } + } + return ans; } public void f(int[] nums, int target,int s,int m,int n,int e){ - int is=nums[s]; - int ie=nums[e]; - while (m list=new ArrayList(); - list.add(is); - list.add(nums[m]); - list.add(nums[n]); - list.add(ie); - ans.add(list); - while (m list=new ArrayList(); + list.add(is); + list.add(nums[m]); + list.add(nums[n]); + list.add(ie); + ans.add(list); + while (m list=new ArrayList(); list=addlist(head, list); if (list.size()==n) { - return head.next; - } + return head.next; + } list.get(list.size()-n-1).next=n==1?null:list.get(list.size()-n+1); return head; } public List addlist(ListNode head, List list){ - if (head!=null) { - list.add(head); - }else { - return list; - } - return addlist(head.next, list); + if (head!=null) { + list.add(head); + }else { + return list; + } + return addlist(head.next, list); } } \ No newline at end of file diff --git a/java/020. Valid Parentheses.java b/java/020. Valid Parentheses.java index 94314d8..6c3f682 100644 --- a/java/020. Valid Parentheses.java +++ b/java/020. Valid Parentheses.java @@ -1,49 +1,49 @@ public class Solution { public boolean isValid(String s) { - String ns=""; - for (int i = 0; i 0&&ns.substring(ns.length()-1, ns.length()).equals("{")) { - ns=ns.substring(0, ns.length()-1); - continue; - }else { - return false; - } - } - if (k.equals("]")) { - if (ns.length()>0&&ns.substring(ns.length()-1, ns.length()).equals("[")) { - ns=ns.substring(0, ns.length()-1); - continue; - }else { - return false; - } - } - if (k.equals(")")) { - if (ns.length()>0&&ns.substring(ns.length()-1, ns.length()).equals("(")) { - ns=ns.substring(0, ns.length()-1); - continue; - }else { - return false; - } - } - } - if (ns.length()!=0) { - return false; - }else { - return true; - } + String ns=""; + for (int i = 0; i 0&&ns.substring(ns.length()-1, ns.length()).equals("{")) { + ns=ns.substring(0, ns.length()-1); + continue; + }else { + return false; + } + } + if (k.equals("]")) { + if (ns.length()>0&&ns.substring(ns.length()-1, ns.length()).equals("[")) { + ns=ns.substring(0, ns.length()-1); + continue; + }else { + return false; + } + } + if (k.equals(")")) { + if (ns.length()>0&&ns.substring(ns.length()-1, ns.length()).equals("(")) { + ns=ns.substring(0, ns.length()-1); + continue; + }else { + return false; + } + } + } + if (ns.length()!=0) { + return false; + }else { + return true; + } } } \ No newline at end of file diff --git a/java/021. Merge Two Sorted Lists.java b/java/021. Merge Two Sorted Lists.java index 05c5282..43ace6b 100644 --- a/java/021. Merge Two Sorted Lists.java +++ b/java/021. Merge Two Sorted Lists.java @@ -8,20 +8,20 @@ */ public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { - if (l1==null) { - return l2; - } + if (l1==null) { + return l2; + } if (l2==null) { - return l1; - } + return l1; + } ListNode l=null; if (l1.val>l2.val) { - l=new ListNode(l2.val); - l.next=mergeTwoLists(l1,l2.next); - }else { - l=new ListNode(l1.val); - l.next=mergeTwoLists(l1.next,l2); - } + l=new ListNode(l2.val); + l.next=mergeTwoLists(l1,l2.next); + }else { + l=new ListNode(l1.val); + l.next=mergeTwoLists(l1.next,l2); + } return l; } } \ No newline at end of file diff --git a/java/022. Generate Parentheses.java b/java/022. Generate Parentheses.java index 27f0dea..3fcab07 100644 --- a/java/022. Generate Parentheses.java +++ b/java/022. Generate Parentheses.java @@ -1,42 +1,42 @@ public class Solution { public List generateParenthesis(int n) { - List list = new ArrayList(); - String a = "("; - list.add(a); - for (int i = 1; i <= 2 * n - 1; i++) { - int size = list.size(); - for (int j = 0; j < size; j++) { - String s = list.get(j); - int use = 0; - int have = 0; - // 判断当前的字符串已经使用掉的"("和还未被")"消耗掉的"(" - for (int k = 0; k < s.length(); k++) { - if (s.substring(k, k + 1).equals("(")) { - use++; - have++; - } - if (s.substring(k, k + 1).equals(")")) { - have--; - } - } - if (use == n) { - s += ")"; - list.add(s); - } else if (have == 0) { - s += "("; - list.add(s); - } else { - String sa = s + "("; - String sb = s + ")"; - list.add(sa); - list.add(sb); - } - } - for (int j = size - 1; j >= 0; j--) { - list.remove(j); - } - } + List list = new ArrayList(); + String a = "("; + list.add(a); + for (int i = 1; i <= 2 * n - 1; i++) { + int size = list.size(); + for (int j = 0; j < size; j++) { + String s = list.get(j); + int use = 0; + int have = 0; + // 判断当前的字符串已经使用掉的"("和还未被")"消耗掉的"(" + for (int k = 0; k < s.length(); k++) { + if (s.substring(k, k + 1).equals("(")) { + use++; + have++; + } + if (s.substring(k, k + 1).equals(")")) { + have--; + } + } + if (use == n) { + s += ")"; + list.add(s); + } else if (have == 0) { + s += "("; + list.add(s); + } else { + String sa = s + "("; + String sb = s + ")"; + list.add(sa); + list.add(sb); + } + } + for (int j = size - 1; j >= 0; j--) { + list.remove(j); + } + } - return list; - } + return list; + } } \ No newline at end of file diff --git a/java/023. Merge k Sorted Lists.java b/java/023. Merge k Sorted Lists.java index 059752d..38ee0b7 100644 --- a/java/023. Merge k Sorted Lists.java +++ b/java/023. Merge k Sorted Lists.java @@ -8,24 +8,24 @@ */ public class Solution { public ListNode mergeKLists(ListNode[] lists) { - if (lists.length>2) { - ListNode[] l1=new ListNode[lists.length/2]; - ListNode[] l2=new ListNode[lists.length-lists.length/2]; - for (int i = 0; i < lists.length; i++) { - if (i2) { + ListNode[] l1=new ListNode[lists.length/2]; + ListNode[] l2=new ListNode[lists.length-lists.length/2]; + for (int i = 0; i < lists.length; i++) { + if (i val = new ArrayList(); - for (int i = 0; i < k; i++) { - if (l == null) { - return head; - } - val.add(l.val); - l = l.next; - } - l = head; - for (int i = val.size() - 1; i >= 0; i--) { - l.val = val.get(i); - l = l.next; - } - l = reverseKGroup(l, k); - return head; - } + ListNode l = head; + List val = new ArrayList(); + for (int i = 0; i < k; i++) { + if (l == null) { + return head; + } + val.add(l.val); + l = l.next; + } + l = head; + for (int i = val.size() - 1; i >= 0; i--) { + l.val = val.get(i); + l = l.next; + } + l = reverseKGroup(l, k); + return head; + } }/** * Definition for singly-linked list. * public class ListNode { @@ -35,21 +35,21 @@ public ListNode reverseKGroup(ListNode head, int k) { */ public class Solution { public ListNode reverseKGroup(ListNode head, int k) { - ListNode l = head; - List val = new ArrayList(); - for (int i = 0; i < k; i++) { - if (l == null) { - return head; - } - val.add(l.val); - l = l.next; - } - l = head; - for (int i = val.size() - 1; i >= 0; i--) { - l.val = val.get(i); - l = l.next; - } - l = reverseKGroup(l, k); - return head; - } + ListNode l = head; + List val = new ArrayList(); + for (int i = 0; i < k; i++) { + if (l == null) { + return head; + } + val.add(l.val); + l = l.next; + } + l = head; + for (int i = val.size() - 1; i >= 0; i--) { + l.val = val.get(i); + l = l.next; + } + l = reverseKGroup(l, k); + return head; + } } \ No newline at end of file diff --git a/java/026. Remove Duplicates from Sorted Array.java b/java/026. Remove Duplicates from Sorted Array.java index d778d4d..24b2f6d 100644 --- a/java/026. Remove Duplicates from Sorted Array.java +++ b/java/026. Remove Duplicates from Sorted Array.java @@ -3,24 +3,24 @@ public int removeDuplicates(int[] nums) { if(nums.length<=1){ return nums.length; } - int tar=nums[0]; - int l=1; - int end=nums.length; - int kuang=0; - for (int i = 1; i < end; i++) { - if (nums[i]==tar) { - for (int j = i; j < end-1; j++) { - kuang=nums[j]; - nums[j]=nums[j+1]; - nums[j+1]=kuang; - } - end--; - i--; - }else { - tar=nums[i]; - l++; - } - } - return l; + int tar=nums[0]; + int l=1; + int end=nums.length; + int kuang=0; + for (int i = 1; i < end; i++) { + if (nums[i]==tar) { + for (int j = i; j < end-1; j++) { + kuang=nums[j]; + nums[j]=nums[j+1]; + nums[j+1]=kuang; + } + end--; + i--; + }else { + tar=nums[i]; + l++; + } + } + return l; } } \ No newline at end of file diff --git a/java/027. Remove Element.java b/java/027. Remove Element.java index 95c03bb..4110a0d 100644 --- a/java/027. Remove Element.java +++ b/java/027. Remove Element.java @@ -1,18 +1,18 @@ public class Solution { public int removeElement(int[] nums, int val) { int end=nums.length; - int k=0; - for (int i = 0; i < end; i++) { - if (nums[i]==val) { - for (int j = i; j < end-1; j++) { - k=nums[j]; - nums[j]=nums[j+1]; - nums[j+1]=k; - } - end--; - i--; - } - } - return end; + int k=0; + for (int i = 0; i < end; i++) { + if (nums[i]==val) { + for (int j = i; j < end-1; j++) { + k=nums[j]; + nums[j]=nums[j+1]; + nums[j+1]=k; + } + end--; + i--; + } + } + return end; } } \ No newline at end of file diff --git a/java/028. Implement strStr().java b/java/028. Implement strStr().java index 83a7b03..251c15f 100644 --- a/java/028. Implement strStr().java +++ b/java/028. Implement strStr().java @@ -1,13 +1,13 @@ public class Solution { public int strStr(String haystack, String needle) { if (needle.length()==0) { - return 0; - } + return 0; + } for (int i = 0; i 1) { + if (nums.length>1) { - int a=0; - for (int i = 1; i < nums.length; i++) { - if (nums[i]>nums[i-1]) { - a=i; - } - } + int a=0; + for (int i = 1; i < nums.length; i++) { + if (nums[i]>nums[i-1]) { + a=i; + } + } for (int i = a; i < (a+nums.length)/2; i++) { - int k=nums[i]; - nums[i]=nums[nums.length-i+a-1]; - nums[nums.length-i+a-1]=k; - } + int k=nums[i]; + nums[i]=nums[nums.length-i+a-1]; + nums[nums.length-i+a-1]=k; + } if (a>0) { - for (int i = a; i < nums.length; i++) { - if (nums[a-1]= 0; i--) { - if (c[i]=='(') { - int t=i+1+vals[i+1]; - if (t1) { - int b=nums[begin]; - int h=nums[half]; - int e=nums[end]; - if (h==target) { - return half; - } - if (b==target) { - return begin; - } - if (e==target) { - return end; - } - if (h>b) { - if (target>h) { - begin=half; - half=(end+begin)/2; - }else { - if (target>b) { - end=half; - half=(end+begin)/2; - }else { - begin=half; - half=(end+begin)/2; - } - } - }else { - if (targete) { - end=half; - half=(end+begin)/2; - }else { - begin=half; - half=(end+begin)/2; - } - } - } - } - if (nums[begin]==target) { - return begin; - } - if (nums[end]==target) { - return end; - } + if (nums.length==1) { + if (nums[0]==target) { + return 0; + }else { + return -1; + } + } + int begin=0; + int end=nums.length-1; + int half=(end+begin)/2; + while (end-begin>1) { + int b=nums[begin]; + int h=nums[half]; + int e=nums[end]; + if (h==target) { + return half; + } + if (b==target) { + return begin; + } + if (e==target) { + return end; + } + if (h>b) { + if (target>h) { + begin=half; + half=(end+begin)/2; + }else { + if (target>b) { + end=half; + half=(end+begin)/2; + }else { + begin=half; + half=(end+begin)/2; + } + } + }else { + if (targete) { + end=half; + half=(end+begin)/2; + }else { + begin=half; + half=(end+begin)/2; + } + } + } + } + if (nums[begin]==target) { + return begin; + } + if (nums[end]==target) { + return end; + } - return -1; - } + return -1; + } } \ No newline at end of file diff --git a/java/034. Search for a Range.java b/java/034. Search for a Range.java index 67f62c7..607edfc 100644 --- a/java/034. Search for a Range.java +++ b/java/034. Search for a Range.java @@ -1,72 +1,72 @@ public class Solution { public int[] searchRange(int[] nums, int target) { int begin=0; - int end=nums.length-1; - int a=(begin+end)/2; - int[] answer=new int[2]; - while (nums[a]!=target) { - if(begin==end){ - answer[0]=-1; - answer[1]=-1; - return answer; - } - if (nums[a]>target) { - if (a==end) { - end--; - }else { - end=a; - } - - }else { - if (a==begin) { - begin++; - }else { - begin=a; - } - - } - a=(begin+end)/2; - } - int bb=0; - int be=a; - int ba=(bb+be)/2; - while (!(nums[ba]==target&&(ba==0||nums[ba-1]!=target))) { - if (nums[ba]>=target) { - if (ba==be) { - be--; - }else { - be=ba; - } - }else { - if (ba==bb) { - bb++; - }else { - bb=ba; - } - } - ba=(bb+be)/2; - } - int eb=a; - int ee=nums.length-1; - int ea=(eb+ee)/2; + int end=nums.length-1; + int a=(begin+end)/2; + int[] answer=new int[2]; + while (nums[a]!=target) { + if(begin==end){ + answer[0]=-1; + answer[1]=-1; + return answer; + } + if (nums[a]>target) { + if (a==end) { + end--; + }else { + end=a; + } + + }else { + if (a==begin) { + begin++; + }else { + begin=a; + } + + } + a=(begin+end)/2; + } + int bb=0; + int be=a; + int ba=(bb+be)/2; + while (!(nums[ba]==target&&(ba==0||nums[ba-1]!=target))) { + if (nums[ba]>=target) { + if (ba==be) { + be--; + }else { + be=ba; + } + }else { + if (ba==bb) { + bb++; + }else { + bb=ba; + } + } + ba=(bb+be)/2; + } + int eb=a; + int ee=nums.length-1; + int ea=(eb+ee)/2; while (!(nums[ea]==target&&(ea==nums.length-1||nums[ea+1]!=target))) { - if (nums[ea]<=target) { - if (ea==eb) { - eb++; - }else { - eb=ea; - } - }else { - if (ea==ee) { - ee--; - }else { - ee=ea; - } - } - ea=(eb+ee)/2; - } - answer[0]=ba; - answer[1]=ea; - return answer; + if (nums[ea]<=target) { + if (ea==eb) { + eb++; + }else { + eb=ea; + } + }else { + if (ea==ee) { + ee--; + }else { + ee=ea; + } + } + ea=(eb+ee)/2; + } + answer[0]=ba; + answer[1]=ea; + return answer; } } \ No newline at end of file diff --git a/java/035. Search Insert Position.java b/java/035. Search Insert Position.java index 109aab1..0903be1 100644 --- a/java/035. Search Insert Position.java +++ b/java/035. Search Insert Position.java @@ -1,33 +1,33 @@ public class Solution { public int searchInsert(int[] nums, int target) { int begin=0; - int end=nums.length-1; - int a=(begin+end)/2; - if (targetnums[nums.length-1]) { - return nums.length; - } - while (nums[a]!=target) { - if (end-1==begin&&(nums[begin]target) { - if (end==a) { - end--; - }else { - end=a; - } - }else { - if (begin==a) { - begin++; - }else { - begin=a; - } - } - a=(begin+end)/2; - } - return a; + int end=nums.length-1; + int a=(begin+end)/2; + if (targetnums[nums.length-1]) { + return nums.length; + } + while (nums[a]!=target) { + if (end-1==begin&&(nums[begin]target) { + if (end==a) { + end--; + }else { + end=a; + } + }else { + if (begin==a) { + begin++; + }else { + begin=a; + } + } + a=(begin+end)/2; + } + return a; } } \ No newline at end of file diff --git a/java/036. Valid Sudoku.java b/java/036. Valid Sudoku.java index 3ab4779..c9ffe01 100644 --- a/java/036. Valid Sudoku.java +++ b/java/036. Valid Sudoku.java @@ -1,43 +1,43 @@ public class Solution { public boolean isValidSudoku(char[][] board) { - //验证每行每列有没有重复数据 - for (int i = 0; i < 9; i++) { - Map cross=new HashMap(); - Map vertical=new HashMap(); - for (int j = 0; j < 9; j++) { - if (!(board[i][j]=='.')) { - if (cross.get(board[i][j])!=null) { - return false; - }else { - cross.put(board[i][j], ""); - } - } - if (board[j][i]!='.') { - if (vertical.get(board[j][i])!=null) { - return false; - }else { - vertical.put(board[j][i], ""); - } - } - } - } - //验证每个小九个字有无重复数据 - for (int a = 0; a < 3; a++) { - for (int b = 0; b < 3; b++) { - Map squ=new HashMap(); - for (int c = 0; c < 3; c++) { - for (int d = 0; d < 3; d++) { - if (board[3*a+c][3*b+d]!='.') { - if (squ.get(board[3*a+c][3*b+d])!=null) { - return false; - }else { - squ.put(board[3*a+c][3*b+d], ""); - } - } - } - } - } - } - return true; + //验证每行每列有没有重复数据 + for (int i = 0; i < 9; i++) { + Map cross=new HashMap(); + Map vertical=new HashMap(); + for (int j = 0; j < 9; j++) { + if (!(board[i][j]=='.')) { + if (cross.get(board[i][j])!=null) { + return false; + }else { + cross.put(board[i][j], ""); + } + } + if (board[j][i]!='.') { + if (vertical.get(board[j][i])!=null) { + return false; + }else { + vertical.put(board[j][i], ""); + } + } + } + } + //验证每个小九个字有无重复数据 + for (int a = 0; a < 3; a++) { + for (int b = 0; b < 3; b++) { + Map squ=new HashMap(); + for (int c = 0; c < 3; c++) { + for (int d = 0; d < 3; d++) { + if (board[3*a+c][3*b+d]!='.') { + if (squ.get(board[3*a+c][3*b+d])!=null) { + return false; + }else { + squ.put(board[3*a+c][3*b+d], ""); + } + } + } + } + } + } + return true; } } \ No newline at end of file diff --git a/java/037. Sudoku Solver.java b/java/037. Sudoku Solver.java index 3f9a5ea..c5510c5 100644 --- a/java/037. Sudoku Solver.java +++ b/java/037. Sudoku Solver.java @@ -1,67 +1,67 @@ public class Solution { public void solveSudoku(char[][] board) { - //true表示需要填的 + //true表示需要填的 boolean[][] flag=new boolean[9][9]; for (int i = 0; i < 9; i++) { - for (int j = 0; j < 9; j++) { - flag[i][j]=board[i][j]=='.'; - } - } + for (int j = 0; j < 9; j++) { + flag[i][j]=board[i][j]=='.'; + } + } for (int i = 0; i < 81;) { - int x=i/9; - int y=i%9; - if (flag[x][y]) { - if (board[x][y]=='.') { - board[x][y]='1'; - if (f(board,x,y,board[x][y])) { - i++; - } - }else if(board[x][y]<'9'){ - board[x][y]++; - if (f(board,x,y,board[x][y])) { - i++; - } - }else { - //回退 - while ((!flag[x][y])||board[x][y]=='9') { - if (flag[x][y]) { - board[x][y]='.'; - } - i--; - if (i<0) { - return ; - } - x=i/9; - y=i%9; - } - } - }else { - i++; - } - } + int x=i/9; + int y=i%9; + if (flag[x][y]) { + if (board[x][y]=='.') { + board[x][y]='1'; + if (f(board,x,y,board[x][y])) { + i++; + } + }else if(board[x][y]<'9'){ + board[x][y]++; + if (f(board,x,y,board[x][y])) { + i++; + } + }else { + //回退 + while ((!flag[x][y])||board[x][y]=='9') { + if (flag[x][y]) { + board[x][y]='.'; + } + i--; + if (i<0) { + return ; + } + x=i/9; + y=i%9; + } + } + }else { + i++; + } + } } public boolean f(char[][] board,int x,int y,char val){ - int a=x/3; - int b=y/3; - for (int i = 0; i < 9; i++) { - if (i!=y) { - if (board[x][i]==val) { - return false; - } - } - if (x!=i) { - if (board[i][y]==val) { - return false; - } - } - int ax=i/3; - int by=i%3; - if (x!=a*3+ax||y!=b*3+by) { - if (board[a*3+ax][b*3+by]==val) { - return false; - } - } - } - return true; + int a=x/3; + int b=y/3; + for (int i = 0; i < 9; i++) { + if (i!=y) { + if (board[x][i]==val) { + return false; + } + } + if (x!=i) { + if (board[i][y]==val) { + return false; + } + } + int ax=i/3; + int by=i%3; + if (x!=a*3+ax||y!=b*3+by) { + if (board[a*3+ax][b*3+by]==val) { + return false; + } + } + } + return true; } } \ No newline at end of file diff --git a/java/038. Count and Say.java b/java/038. Count and Say.java index e310375..faa38ef 100644 --- a/java/038. Count and Say.java +++ b/java/038. Count and Say.java @@ -1,31 +1,31 @@ public class Solution { public String countAndSay(int n) { - if (n == 1) { - return "1"; - } - String answer = "1"; - for (int i = 0; i < n - 1; i++) { - answer = new Solution().readNum(answer); - } - return answer; - } + if (n == 1) { + return "1"; + } + String answer = "1"; + for (int i = 0; i < n - 1; i++) { + answer = new Solution().readNum(answer); + } + return answer; + } - public String readNum(String num) { - int n = 1; - String t = num.substring(0, 1); - StringBuffer answer=new StringBuffer(); - String k = ""; - for (int i = 1; i < num.length(); i++) { - k = num.substring(i, i + 1); - if (k.equals(t)) { - n++; - } else { - answer=answer.append(n).append(t); - t = k; - n = 1; - } - } - answer = answer.append(n).append(t); - return answer.toString(); - } + public String readNum(String num) { + int n = 1; + String t = num.substring(0, 1); + StringBuffer answer=new StringBuffer(); + String k = ""; + for (int i = 1; i < num.length(); i++) { + k = num.substring(i, i + 1); + if (k.equals(t)) { + n++; + } else { + answer=answer.append(n).append(t); + t = k; + n = 1; + } + } + answer = answer.append(n).append(t); + return answer.toString(); + } } \ No newline at end of file diff --git a/java/039. Combination Sum.java b/java/039. Combination Sum.java index 965ff88..1ef0287 100644 --- a/java/039. Combination Sum.java +++ b/java/039. Combination Sum.java @@ -1,39 +1,39 @@ public class Solution { public List> combinationSum(int[] candidates, int target) { - Arrays.sort(candidates); - return f(candidates, target, new HashMap>>()); + Arrays.sort(candidates); + return f(candidates, target, new HashMap>>()); } public List> f(int[] candidates, int target,HashMap>> map){ - if (map.get(target)==null) { - List> ans=new ArrayList>(); - if (target> list=f(candidates,target-k,map); - if (list!=null&&list.size()!=0) { - for (int j = 0; j < list.size(); j++) { - if (list.get(j).get(0)>=k) { - List l=new ArrayList(); - l.add(k); - l.addAll(list.get(j)); - ans.add(l); - } - } - } - }else if (k==target) { - List l=new ArrayList(); - l.add(k); - ans.add(l); - }else { - break; - } - } - map.put(target, ans); - return ans; - } - return map.get(target); + if (map.get(target)==null) { + List> ans=new ArrayList>(); + if (target> list=f(candidates,target-k,map); + if (list!=null&&list.size()!=0) { + for (int j = 0; j < list.size(); j++) { + if (list.get(j).get(0)>=k) { + List l=new ArrayList(); + l.add(k); + l.addAll(list.get(j)); + ans.add(l); + } + } + } + }else if (k==target) { + List l=new ArrayList(); + l.add(k); + ans.add(l); + }else { + break; + } + } + map.put(target, ans); + return ans; + } + return map.get(target); } } \ No newline at end of file diff --git a/java/040. Combination Sum II.java b/java/040. Combination Sum II.java index a3f7b90..e35e627 100644 --- a/java/040. Combination Sum II.java +++ b/java/040. Combination Sum II.java @@ -4,30 +4,30 @@ public List> combinationSum2(int[] candidates, int target) { return f(candidates,target,candidates.length-1); } public List> f(int[] candidates, int target,int l) { - List> ans=new ArrayList>(); - if (candidates[0]>target||target<0||l<0) { - return ans; - } - for (int i =l; i >=0 ; i--) { - int k=candidates[i]; - if (k==target) { - List list=new ArrayList(); - list.add(k); - ans.add(list); - }else { - List> q=f(candidates,target-k,i-1); - if (q!=null&&q.size()!=0) { - for (int j = 0; j < q.size(); j++) { - List list=q.get(j); - list.add(k); - ans.add(list); - } - } - } - while (i>0&&candidates[i]==candidates[i-1]) { - i--; - } - } - return ans; + List> ans=new ArrayList>(); + if (candidates[0]>target||target<0||l<0) { + return ans; + } + for (int i =l; i >=0 ; i--) { + int k=candidates[i]; + if (k==target) { + List list=new ArrayList(); + list.add(k); + ans.add(list); + }else { + List> q=f(candidates,target-k,i-1); + if (q!=null&&q.size()!=0) { + for (int j = 0; j < q.size(); j++) { + List list=q.get(j); + list.add(k); + ans.add(list); + } + } + } + while (i>0&&candidates[i]==candidates[i-1]) { + i--; + } + } + return ans; } } \ No newline at end of file diff --git a/java/041. First Missing Positive.java b/java/041. First Missing Positive.java index fcc75a5..3c2d60b 100644 --- a/java/041. First Missing Positive.java +++ b/java/041. First Missing Positive.java @@ -1,21 +1,21 @@ public class Solution { public int firstMissingPositive(int[] nums) { - for (int i = 0; i < nums.length; i++) { - if (nums[i]!=i+1) { - if (nums[i]<=nums.length&&nums[i]>0&&nums[nums[i]-1]!=nums[i]) { - int k=nums[nums[i]-1]; - nums[nums[i]-1]=nums[i]; - nums[i]=k; - i--; - } - } - } - for (int i = 0; i < nums.length; i++) { - if (nums[i]!=i+1) { - return i+1; - } - } - return nums.length+1; + for (int i = 0; i < nums.length; i++) { + if (nums[i]!=i+1) { + if (nums[i]<=nums.length&&nums[i]>0&&nums[nums[i]-1]!=nums[i]) { + int k=nums[nums[i]-1]; + nums[nums[i]-1]=nums[i]; + nums[i]=k; + i--; + } + } + } + for (int i = 0; i < nums.length; i++) { + if (nums[i]!=i+1) { + return i+1; + } + } + return nums.length+1; } diff --git a/java/042. Trapping Rain Water.java b/java/042. Trapping Rain Water.java index c59b046..0d04929 100644 --- a/java/042. Trapping Rain Water.java +++ b/java/042. Trapping Rain Water.java @@ -1,19 +1,19 @@ public class Solution { public int trap(int[] height) { - if (height.length<=1) { - return 0; - } - int a=0; - int h=height[a]; - /** - * 求出最高峰 - */ + if (height.length<=1) { + return 0; + } + int a=0; + int h=height[a]; + /** + * 求出最高峰 + */ for (int i = 1; i < height.length; i++) { - if (height[i]>h) { - a=i; - h=height[i]; - } - } + if (height[i]>h) { + a=i; + h=height[i]; + } + } Map leftMap=new HashMap(); Map rightMap=new HashMap(); leftMap.put("result", 0); @@ -22,7 +22,7 @@ public int trap(int[] height) { rightMap.put("a",a); Map leftrRsult=new Solution().left(leftMap, height); Map rightResult=new Solution().right(leftMap, height); - return leftrRsult.get("result")+rightResult.get("result"); + return leftrRsult.get("result")+rightResult.get("result"); } /** @@ -32,31 +32,31 @@ public int trap(int[] height) { * @return */ public Map left(Map m,int[] height){ - Map map=new HashMap(); - if (m==null||m.get("a")==null) { - return m; - } - if (m.get("a")<=1) { - return m; - } - int h=height[0]; - int result=m.get("result"); - int sum=0; - int a=0; - for (int i = 1; i map=new HashMap(); + if (m==null||m.get("a")==null) { + return m; + } + if (m.get("a")<=1) { + return m; + } + int h=height[0]; + int result=m.get("result"); + int sum=0; + int a=0; + for (int i = 1; i left(Map m,int[] height){ * @return */ public Map right(Map m,int[] height){ - Map map=new HashMap(); - if (m==null||m.get("a")==null) { - return m; - } - if (m.get("a")>=height.length-2) { - return m; - } - int h=height[height.length-1]; - int result=m.get("result"); - int sum=0; - int a=0; - for (int i = height.length-1; i>m.get("a"); i--) { - if (height[i] map=new HashMap(); + if (m==null||m.get("a")==null) { + return m; + } + if (m.get("a")>=height.length-2) { + return m; + } + int h=height[height.length-1]; + int result=m.get("result"); + int sum=0; + int a=0; + for (int i = height.length-1; i>m.get("a"); i--) { + if (height[i]=0 ; i--) { - sb.append(String.valueOf(a[i])); - } - return sb.toString(); + int sum=0; + int carry=0; + for (int i = 0; i < a.length; i++) { + // 每一位乘级相加的结果 + for (int j = Math.max(0,i-l2+1); j <= Math.min(i,l1-1); j++) { + sum += n1[j] * n2[i-j]; + } + // 加进位 + sum += carry; + carry = sum / 10; + sum%=10; + a[i]=sum; + sum = 0; + } + StringBuffer sb=new StringBuffer(); + if (carry!=0) { + sb.append(String.valueOf(carry)); + } + for (int i = a.length-1; i >=0 ; i--) { + sb.append(String.valueOf(a[i])); + } + return sb.toString(); } } \ No newline at end of file diff --git a/java/045. Jump Game II.java b/java/045. Jump Game II.java index 53072d4..f5fef6a 100644 --- a/java/045. Jump Game II.java +++ b/java/045. Jump Game II.java @@ -1,25 +1,25 @@ public class Solution { public int jump(int[] nums) { - if (nums.length==1) { - return 0; - } - int max=0; + if (nums.length==1) { + return 0; + } + int max=0; int[] steps=new int[nums.length]; steps[0]=0; for (int i = 0; i < nums.length-1; i++) { - int n=nums[i]; - if (n+i>max) { - int s=steps[i]; - for (int j = max+1; j <=n+i; j++) { - if (js+1) { - steps[j]=s+1; - } - } - } - max=n+i; - } - } - return steps[steps.length-1]; + int n=nums[i]; + if (n+i>max) { + int s=steps[i]; + for (int j = max+1; j <=n+i; j++) { + if (js+1) { + steps[j]=s+1; + } + } + } + max=n+i; + } + } + return steps[steps.length-1]; } } \ No newline at end of file diff --git a/java/046. Permutations.java b/java/046. Permutations.java index fd2e67d..a90d4a7 100644 --- a/java/046. Permutations.java +++ b/java/046. Permutations.java @@ -1,42 +1,42 @@ public class Solution { public List> permute(int[] nums) { - List> answerList=new ArrayList>(); - for (int i = 0; i < nums.length; i++) { - int size=answerList.size(); - if (size==0) { - for (int j = 0; j < nums.length; j++) { - List intList=new ArrayList(); - intList.add(nums[j]); - answerList.add(intList); - } - }else { - for (int j = 0; j < size; j++) { - List intList=answerList.get(j); - Map map=new HashMap(); - for (int l = 0; l =0&&nums[i]==n) { - nums[i]=0; - i--; - } - if (i>=0) { - nums[i]++; - } - }else { - if (f(nums,i,nums[i])) { - i++; - }else { - nums[i]++; - } - } - } - - } -// return ans; + //装n皇后的数组 + int[] nums=new int[n]; + //答案 + List> ans=new ArrayList>(); + int i=0; + while (true) { + //所有可能已经尝试完毕,退出游戏 + if (i<0) { + return ans; + } + //成功尝试出一种答案, 装载答案 + if (i==n) { + List list=new ArrayList(); + for (int j = 0; j < nums.length; j++) { + StringBuffer sb=new StringBuffer(); + for (int k = 0; k < n; k++) { + if (k!=nums[j]) { + sb.append("."); + }else { + sb.append("Q"); + } + } + list.add(sb.toString()); + } + ans.add(list); + //回退 + i--; + while (nums[i]==n) { + nums[i]=0; + i--; + } + nums[i]++; + }else { + if (nums[i]==n) { + //回退 + while (i>=0&&nums[i]==n) { + nums[i]=0; + i--; + } + if (i>=0) { + nums[i]++; + } + }else { + if (f(nums,i,nums[i])) { + i++; + }else { + nums[i]++; + } + } + } + + } +// return ans; } public boolean f(int[] nums,int x,int val){ - for (int i = 0; i < x; i++) { - int t=x-i; - if (val==nums[i]||val==nums[i]+t||val==nums[i]-t) { - return false; - } - } - return true; + for (int i = 0; i < x; i++) { + int t=x-i; + if (val==nums[i]||val==nums[i]+t||val==nums[i]-t) { + return false; + } + } + return true; } } \ No newline at end of file diff --git a/java/052. N-Queens II.java b/java/052. N-Queens II.java index 02dafcb..e6c3f42 100644 --- a/java/052. N-Queens II.java +++ b/java/052. N-Queens II.java @@ -4,67 +4,67 @@ public int totalNQueens(int n) { } public List> solveNQueens(int n) { - //װnʺ - int[] nums=new int[n]; - // - List> ans=new ArrayList>(); - int i=0; - while (true) { - //пѾϣ˳Ϸ - if (i<0) { - return ans; - } - //ɹԳһִ𰸣 װش - if (i==n) { - List list=new ArrayList(); - for (int j = 0; j < nums.length; j++) { - StringBuffer sb=new StringBuffer(); - for (int k = 0; k < n; k++) { - if (k!=nums[j]) { - sb.append("."); - }else { - sb.append("Q"); - } - } - list.add(sb.toString()); - } - ans.add(list); - // - i--; - while (nums[i]==n) { - nums[i]=0; - i--; - } - nums[i]++; - }else { - if (nums[i]==n) { - // - while (i>=0&&nums[i]==n) { - nums[i]=0; - i--; - } - if (i>=0) { - nums[i]++; - } - }else { - if (f(nums,i,nums[i])) { - i++; - }else { - nums[i]++; - } - } - } - - } -// return ans; + //װnʺ + int[] nums=new int[n]; + // + List> ans=new ArrayList>(); + int i=0; + while (true) { + //пѾϣ˳Ϸ + if (i<0) { + return ans; + } + //ɹԳһִ𰸣 װش + if (i==n) { + List list=new ArrayList(); + for (int j = 0; j < nums.length; j++) { + StringBuffer sb=new StringBuffer(); + for (int k = 0; k < n; k++) { + if (k!=nums[j]) { + sb.append("."); + }else { + sb.append("Q"); + } + } + list.add(sb.toString()); + } + ans.add(list); + // + i--; + while (nums[i]==n) { + nums[i]=0; + i--; + } + nums[i]++; + }else { + if (nums[i]==n) { + // + while (i>=0&&nums[i]==n) { + nums[i]=0; + i--; + } + if (i>=0) { + nums[i]++; + } + }else { + if (f(nums,i,nums[i])) { + i++; + }else { + nums[i]++; + } + } + } + + } +// return ans; } public boolean f(int[] nums,int x,int val){ - for (int i = 0; i < x; i++) { - int t=x-i; - if (val==nums[i]||val==nums[i]+t||val==nums[i]-t) { - return false; - } - } - return true; + for (int i = 0; i < x; i++) { + int t=x-i; + if (val==nums[i]||val==nums[i]+t||val==nums[i]-t) { + return false; + } + } + return true; } } \ No newline at end of file diff --git a/java/053. Maximum Subarray.java b/java/053. Maximum Subarray.java index 3f34e7d..7470ba0 100644 --- a/java/053. Maximum Subarray.java +++ b/java/053. Maximum Subarray.java @@ -3,14 +3,14 @@ public int maxSubArray(int[] nums) { int sum=0; int answer=nums[0]; for (int i = 0; i < nums.length; i++) { - sum+=nums[i]; - if (sum>answer) { - answer=sum; - } - if (sum<0) { - sum=0; - } - } - return answer; + sum+=nums[i]; + if (sum>answer) { + answer=sum; + } + if (sum<0) { + sum=0; + } + } + return answer; } } \ No newline at end of file diff --git a/java/054. Spiral Matrix.java b/java/054. Spiral Matrix.java index 6565bb3..85acfd8 100644 --- a/java/054. Spiral Matrix.java +++ b/java/054. Spiral Matrix.java @@ -1,59 +1,59 @@ public class Solution { public List spiralOrder(int[][] matrix) { - List list=new ArrayList(); - //m:行,n列 - int mb=0; - int nb=0; + List list=new ArrayList(); + //m:行,n列 + int mb=0; + int nb=0; int me=matrix.length; if (me==0) { - return list; - } + return list; + } int ne=matrix[0].length; - while (true) { - list=new Solution().right(matrix, mb, nb, me, ne, list); - mb++; - if (nb>ne-1||mb>me-1) { - return list; - } - list=new Solution().low(matrix, mb, nb, me, ne, list); - ne--; - if (nb>ne-1||mb>me-1) { - return list; - } - list=new Solution().left(matrix, mb, nb, me, ne, list); - me--; - if (nb>ne-1||mb>me-1) { - return list; - } - list=new Solution().up(matrix, mb, nb, me, ne, list); - nb++; - if (nb>ne-1||mb>me-1) { - return list; - } - } + while (true) { + list=new Solution().right(matrix, mb, nb, me, ne, list); + mb++; + if (nb>ne-1||mb>me-1) { + return list; + } + list=new Solution().low(matrix, mb, nb, me, ne, list); + ne--; + if (nb>ne-1||mb>me-1) { + return list; + } + list=new Solution().left(matrix, mb, nb, me, ne, list); + me--; + if (nb>ne-1||mb>me-1) { + return list; + } + list=new Solution().up(matrix, mb, nb, me, ne, list); + nb++; + if (nb>ne-1||mb>me-1) { + return list; + } + } } public List right(int[][] matrix,int mb,int nb,int me,int ne,List list){ - for (int i = nb; i < ne; i++) { - list.add(matrix[mb][i]); - } - return list; + for (int i = nb; i < ne; i++) { + list.add(matrix[mb][i]); + } + return list; } public List left(int[][] matrix,int mb,int nb,int me,int ne,List list){ - for (int i = ne-1; i >=nb; i--) { - list.add(matrix[me-1][i]); - } - return list; + for (int i = ne-1; i >=nb; i--) { + list.add(matrix[me-1][i]); + } + return list; } public List low(int[][] matrix,int mb,int nb,int me,int ne,List list){ - for (int i = mb; i < me; i++) { - list.add(matrix[i][ne-1]); - } - return list; + for (int i = mb; i < me; i++) { + list.add(matrix[i][ne-1]); + } + return list; } public List up(int[][] matrix,int mb,int nb,int me,int ne,List list){ - for (int i = me-1; i >=mb; i--) { - list.add(matrix[i][nb]); - } - return list; + for (int i = me-1; i >=mb; i--) { + list.add(matrix[i][nb]); + } + return list; } } \ No newline at end of file diff --git a/java/055. Jump Game.java b/java/055. Jump Game.java index 2b1834a..7e0ae78 100644 --- a/java/055. Jump Game.java +++ b/java/055. Jump Game.java @@ -1,25 +1,25 @@ public class Solution { public boolean canJump(int[] nums) { - int k=-1; - int tar=-1; + int k=-1; + int tar=-1; for (int i = nums.length-1; i>=0 ; i--) { - if (k==-1&&nums[i]==0) { - k=0; - tar=i; - } - if (k==0) { - if(tar==nums.length-1&&nums[i]>=tar-i){ - k=-1; - } - if (nums[i]>tar-i) { - k=-1; - } - } - } - if (k==-1) { - return true; - }else { - return false; - } + if (k==-1&&nums[i]==0) { + k=0; + tar=i; + } + if (k==0) { + if(tar==nums.length-1&&nums[i]>=tar-i){ + k=-1; + } + if (nums[i]>tar-i) { + k=-1; + } + } + } + if (k==-1) { + return true; + }else { + return false; + } } } \ No newline at end of file diff --git a/java/056. Merge Intervals.java b/java/056. Merge Intervals.java index 551da01..0287b27 100644 --- a/java/056. Merge Intervals.java +++ b/java/056. Merge Intervals.java @@ -9,60 +9,60 @@ */ public class Solution { public List merge(List intervals) { - //0开头,2结束,-1原地,1正常 - Map map=new HashMap(); - int sum=0; + //0开头,2结束,-1原地,1正常 + Map map=new HashMap(); + int sum=0; for (int i = 0; i sum) { - sum=e; - } - if (s==e) { - if (map.get(s)==null) { - map.put(s, -1); - } - }else { - for (int j = s; j <=e; j++) { - if (j==s) { - if(map.get(j)==null||map.get(j)==-1||map.get(j)==0){ - map.put(j, 0); - }else { - map.put(j, 1); - } - }else if (j==e) { - if (map.get(j)==null||map.get(j)==-1||map.get(j)==2) { - map.put(j, 2); - }else { - map.put(j, 1); - } - }else { - map.put(j, 1); - } - } - } - } + int s=intervals.get(i).start; + int e=intervals.get(i).end; + if (e>sum) { + sum=e; + } + if (s==e) { + if (map.get(s)==null) { + map.put(s, -1); + } + }else { + for (int j = s; j <=e; j++) { + if (j==s) { + if(map.get(j)==null||map.get(j)==-1||map.get(j)==0){ + map.put(j, 0); + }else { + map.put(j, 1); + } + }else if (j==e) { + if (map.get(j)==null||map.get(j)==-1||map.get(j)==2) { + map.put(j, 2); + }else { + map.put(j, 1); + } + }else { + map.put(j, 1); + } + } + } + } boolean flag=true; List answerList=new ArrayList(); int s=0; int e=0; for (int i = 0; i insert(List intervals, Interval newInterval) { - int s = newInterval.start; - int e = newInterval.end; - if (intervals.size()==0) { - intervals.add(newInterval); - return intervals; - } - List answerList = new ArrayList(); - int ns=0; - int ne=0; - boolean beginFlag=true; - boolean endFlag=true; - for (int i = 0; i < intervals.size(); i++) { - if (endFlag&&beginFlag) { - if (intervals.get(i).start>=s) { - ns=s; - beginFlag=false; - }else if (intervals.get(i).end>=s) { - if (intervals.get(i).end>=e) { - endFlag=false; - }else { - ns=intervals.get(i).start; - beginFlag=false; - } - } - } - if (endFlag&&!beginFlag) { - if (intervals.get(i).start>e) { - ne=e; - answerList.add(new Interval(ns,ne)); - endFlag=false; - beginFlag=true; - }else if(intervals.get(i).start==e){ - ne=intervals.get(i).end; - answerList.add(new Interval(ns,ne)); - endFlag=false; - beginFlag=true; - continue; - }else if(intervals.get(i).end>=e){ - ne=intervals.get(i).end; - answerList.add(new Interval(ns,ne)); - endFlag=false; - beginFlag=true; - continue; - }else if(i==intervals.size()-1){ - ne=e; - answerList.add(new Interval(ns,ne)); - return answerList; - } - } - if (beginFlag) { - answerList.add(intervals.get(i)); - } - } - if (endFlag) { - answerList.add(newInterval); - } - return answerList; - } + int s = newInterval.start; + int e = newInterval.end; + if (intervals.size()==0) { + intervals.add(newInterval); + return intervals; + } + List answerList = new ArrayList(); + int ns=0; + int ne=0; + boolean beginFlag=true; + boolean endFlag=true; + for (int i = 0; i < intervals.size(); i++) { + if (endFlag&&beginFlag) { + if (intervals.get(i).start>=s) { + ns=s; + beginFlag=false; + }else if (intervals.get(i).end>=s) { + if (intervals.get(i).end>=e) { + endFlag=false; + }else { + ns=intervals.get(i).start; + beginFlag=false; + } + } + } + if (endFlag&&!beginFlag) { + if (intervals.get(i).start>e) { + ne=e; + answerList.add(new Interval(ns,ne)); + endFlag=false; + beginFlag=true; + }else if(intervals.get(i).start==e){ + ne=intervals.get(i).end; + answerList.add(new Interval(ns,ne)); + endFlag=false; + beginFlag=true; + continue; + }else if(intervals.get(i).end>=e){ + ne=intervals.get(i).end; + answerList.add(new Interval(ns,ne)); + endFlag=false; + beginFlag=true; + continue; + }else if(i==intervals.size()-1){ + ne=e; + answerList.add(new Interval(ns,ne)); + return answerList; + } + } + if (beginFlag) { + answerList.add(intervals.get(i)); + } + } + if (endFlag) { + answerList.add(newInterval); + } + return answerList; + } } \ No newline at end of file diff --git a/java/058. Length of Last Word.java b/java/058. Length of Last Word.java index c256f79..29f442c 100644 --- a/java/058. Length of Last Word.java +++ b/java/058. Length of Last Word.java @@ -1,25 +1,25 @@ public class Solution { public int lengthOfLastWord(String s) { if (s.length()==0) { - return 0; - } + return 0; + } int e=-1; for (int i = s.length()-1; i >=0; i--) { - if (!s.substring(i,i+1).equals(" ")) { - e=i; - break; - } - } + if (!s.substring(i,i+1).equals(" ")) { + e=i; + break; + } + } if (e==-1) { - return 0; - } + return 0; + } int b=-1; for (int i = e-1; i >=0; i--) { - if (s.substring(i,i+1).equals(" ")) { - b=i; - break; - } - } + if (s.substring(i,i+1).equals(" ")) { + b=i; + break; + } + } System.out.println("b="+b); return b==-1?e+1:e-b; } diff --git a/java/059. Spiral Matrix II.java b/java/059. Spiral Matrix II.java index 67401c7..19a021a 100644 --- a/java/059. Spiral Matrix II.java +++ b/java/059. Spiral Matrix II.java @@ -1,53 +1,53 @@ public class Solution { public int[][] generateMatrix(int n) { - int[][] answer = new int[n][n]; - // 顶部 - int t = 0; - // 底部 - int b = n - 1; - // 左边 - int l = 0; - // 右边 - int r = n - 1; - int a = 1; - while (true) { - // 左 - for (int i = l; i <= r; i++) { - answer[t][i] = a; - a++; - } - t++; - if (t > b) { - return answer; - } - // 下 - for (int i = t; i <= b; i++) { - answer[i][r] = a; - a++; - } - r--; - if (l > r) { - return answer; - } - // 右 - for (int i = r; i >= l; i--) { - answer[b][i] = a; - a++; - } - b--; - if (t > b) { - return answer; - } - // 上 - for (int i = b; i >= t; i--) { - answer[i][l] = a; - a++; - } - l++; - if (l > r) { - return answer; - } + int[][] answer = new int[n][n]; + // 顶部 + int t = 0; + // 底部 + int b = n - 1; + // 左边 + int l = 0; + // 右边 + int r = n - 1; + int a = 1; + while (true) { + // 左 + for (int i = l; i <= r; i++) { + answer[t][i] = a; + a++; + } + t++; + if (t > b) { + return answer; + } + // 下 + for (int i = t; i <= b; i++) { + answer[i][r] = a; + a++; + } + r--; + if (l > r) { + return answer; + } + // 右 + for (int i = r; i >= l; i--) { + answer[b][i] = a; + a++; + } + b--; + if (t > b) { + return answer; + } + // 上 + for (int i = b; i >= t; i--) { + answer[i][l] = a; + a++; + } + l++; + if (l > r) { + return answer; + } - } - } + } + } } \ No newline at end of file diff --git a/java/060. Permutation Sequence.java b/java/060. Permutation Sequence.java index 69cea79..520d066 100644 --- a/java/060. Permutation Sequence.java +++ b/java/060. Permutation Sequence.java @@ -3,20 +3,20 @@ public String getPermutation(int n, int k) { int s=1; List list=new ArrayList(); for (int i = 0; i list=new ArrayList(); while (l!=null) { - list.add(l); - l=l.next; - } + list.add(l); + l=l.next; + } k=k%list.size(); if (k==0) { - return head; - } + return head; + } list.get(list.size()-k-1).next=null; list.get(list.size()-1).next=head; - return list.get(list.size()-k); + return list.get(list.size()-k); } } \ No newline at end of file diff --git a/java/062. Unique Paths.java b/java/062. Unique Paths.java index eb70744..69c9909 100644 --- a/java/062. Unique Paths.java +++ b/java/062. Unique Paths.java @@ -1,30 +1,30 @@ public class Solution { public int uniquePaths(int m, int n) { - if (m==1||n==1) { - return 1; - } - Map map=new HashMap(); - for (int i = 0; i < m; i++) { - map.put(i, 1); - } - for (int i = 1; i < n-1; i++) { - int sum=0; - for (int j = m-1; j >=0; j--) { - sum+=map.get(j); - } - int k=map.get(m-1); - for (int j = m-1; j >=0; j--) { - map.put(j, sum); - sum-=k; - if (j>1) { - k=map.get(j-1); - } - } - } - int sum=0; - for (int i = 0; i < m; i++) { - sum+=map.get(i); - } - return sum; + if (m==1||n==1) { + return 1; + } + Map map=new HashMap(); + for (int i = 0; i < m; i++) { + map.put(i, 1); + } + for (int i = 1; i < n-1; i++) { + int sum=0; + for (int j = m-1; j >=0; j--) { + sum+=map.get(j); + } + int k=map.get(m-1); + for (int j = m-1; j >=0; j--) { + map.put(j, sum); + sum-=k; + if (j>1) { + k=map.get(j-1); + } + } + } + int sum=0; + for (int i = 0; i < m; i++) { + sum+=map.get(i); + } + return sum; } } \ No newline at end of file diff --git a/java/063. Unique Paths II.java b/java/063. Unique Paths II.java index 8faaecd..7b4cc98 100644 --- a/java/063. Unique Paths II.java +++ b/java/063. Unique Paths II.java @@ -1,23 +1,23 @@ public class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { - if (obstacleGrid[0][0]==1||obstacleGrid[obstacleGrid.length-1][obstacleGrid[0].length-1]==1) { - return 0; - } - Map map=new HashMap(); - return f(obstacleGrid,0,0,obstacleGrid.length-1,obstacleGrid[0].length-1,map); + if (obstacleGrid[0][0]==1||obstacleGrid[obstacleGrid.length-1][obstacleGrid[0].length-1]==1) { + return 0; + } + Map map=new HashMap(); + return f(obstacleGrid,0,0,obstacleGrid.length-1,obstacleGrid[0].length-1,map); } public int f(int[][] obstacleGrid,int x,int y,int xl,int yl,Map map){ - String coordinate=x+","+y; - if (x==xl&&y==yl) { - return 1; - } - if (map.get(coordinate)==null) { - boolean xflag=(x map=new HashMap(); - return f(grid,0,0,xl,yl,map); + int xl=grid.length; + int yl=grid[0].length; + Map map=new HashMap(); + return f(grid,0,0,xl,yl,map); } public int f(int[][] grid,int x,int y,int xl,int yl,Map map){ - String coordinate=x+","+y; - if (map.get(coordinate)==null) { - if (x>=xl||y>=yl) { - return -1; - }else { - int xv=f(grid,x+1,y,xl,yl,map); - int yv=f(grid,x,y+1,xl,yl,map); - int v=0; - if (xv<0) { - v=yv; - }else if (yv<0) { - v=xv; - }else if (xv>yv){ - v=yv; - }else { - v=xv; - } - int value=grid[x][y]+(v<0?0:v); - map.put(coordinate, value); - return value; - } - } - return map.get(coordinate); + String coordinate=x+","+y; + if (map.get(coordinate)==null) { + if (x>=xl||y>=yl) { + return -1; + }else { + int xv=f(grid,x+1,y,xl,yl,map); + int yv=f(grid,x,y+1,xl,yl,map); + int v=0; + if (xv<0) { + v=yv; + }else if (yv<0) { + v=xv; + }else if (xv>yv){ + v=yv; + }else { + v=xv; + } + int value=grid[x][y]+(v<0?0:v); + map.put(coordinate, value); + return value; + } + } + return map.get(coordinate); } } diff --git a/java/066. Plus One.java b/java/066. Plus One.java index 093ca22..aaacf4e 100644 --- a/java/066. Plus One.java +++ b/java/066. Plus One.java @@ -1,23 +1,23 @@ public class Solution { public int[] plusOne(int[] digits) { - int i=digits.length-1; + int i=digits.length-1; boolean flag= false; do { - if (digits[i]<9) { - digits[i]+=1; - flag=false; - }else { - digits[i]=0; - flag=true; - } - i--; - } while (flag&&i>=0); + if (digits[i]<9) { + digits[i]+=1; + flag=false; + }else { + digits[i]=0; + flag=true; + } + i--; + } while (flag&&i>=0); if (flag) { - int[] answer=new int[digits.length+1]; - answer[0]=1; - return answer; - }else { - return digits; - } + int[] answer=new int[digits.length+1]; + answer[0]=1; + return answer; + }else { + return digits; + } } } \ No newline at end of file diff --git a/java/067. Add Binary.java b/java/067. Add Binary.java index 298ac1c..0fbb96d 100644 --- a/java/067. Add Binary.java +++ b/java/067. Add Binary.java @@ -1,43 +1,43 @@ public class Solution { public String addBinary(String a, String b) { if (a.equals("0")) { - return b; - } - if (b.equals("0")) { - return a; - } - if (a.length()=0; i--) { - ia[a.length()-1-i]=Integer.parseInt(a.substring(i, i+1)); - } - int[] ib=new int[b.length()]; - for (int i = b.length()-1; i >=0; i--) { - ib[b.length()-1-i]=Integer.parseInt(b.substring(i, i+1)); - } - int target=0; - int[] ias=new int[a.length()]; - for (int i = 0; i < ia.length; i++) { - int k=0; - if (i=0 ; i--) { - sb.append(""+ias[i]); - } - return sb.toString(); + return b; + } + if (b.equals("0")) { + return a; + } + if (a.length()=0; i--) { + ia[a.length()-1-i]=Integer.parseInt(a.substring(i, i+1)); + } + int[] ib=new int[b.length()]; + for (int i = b.length()-1; i >=0; i--) { + ib[b.length()-1-i]=Integer.parseInt(b.substring(i, i+1)); + } + int target=0; + int[] ias=new int[a.length()]; + for (int i = 0; i < ia.length; i++) { + int k=0; + if (i=0 ; i--) { + sb.append(""+ias[i]); + } + return sb.toString(); } } \ No newline at end of file diff --git a/java/068. Text Justification.java b/java/068. Text Justification.java index 1179b63..d518019 100644 --- a/java/068. Text Justification.java +++ b/java/068. Text Justification.java @@ -5,66 +5,66 @@ public List fullJustify(String[] words, int maxWidth) { List list=new ArrayList(); //分行 for (int i = 0; i < words.length; i++) { - if (words[i].length()<=length) { - list.add(words[i]); - length-=(words[i].length()+1); - }else { - sumList.add(list); - list=new ArrayList(); - length=maxWidth; - i--; - } - } + if (words[i].length()<=length) { + list.add(words[i]); + length-=(words[i].length()+1); + }else { + sumList.add(list); + list=new ArrayList(); + length=maxWidth; + i--; + } + } if (list.size()!=0) { - sumList.add(list); - } + sumList.add(list); + } List ans=new ArrayList(); //处理每行的字符串 for (int i = 0; i < sumList.size()-1; i++) { - List l=sumList.get(i); - //区分一行只有一个字符串的和多个字符串的情况 - if (l.size()==1) { - StringBuffer sb=new StringBuffer(); - sb.append(l.get(0)); - for (int j = 0; j < maxWidth-l.get(0).length(); j++) { - sb.append(" "); - } - System.out.println("sb.toString()="+sb.toString()); - ans.add(sb.toString()); - }else { - int totalSpace=maxWidth; - for (int j = 0; j < l.size(); j++) { - totalSpace-=l.get(j).length(); - } - int eachSpace=totalSpace/(l.size()-1); - int extraSpace=totalSpace-eachSpace*(l.size()-1); - StringBuffer sb=new StringBuffer(); - for (int j = 0; j < l.size()-1; j++) { - sb.append(l.get(j)); - for (int k = 0; k < eachSpace; k++) { - sb.append(" "); - } - if (j l=sumList.get(i); + //区分一行只有一个字符串的和多个字符串的情况 + if (l.size()==1) { + StringBuffer sb=new StringBuffer(); + sb.append(l.get(0)); + for (int j = 0; j < maxWidth-l.get(0).length(); j++) { + sb.append(" "); + } + System.out.println("sb.toString()="+sb.toString()); + ans.add(sb.toString()); + }else { + int totalSpace=maxWidth; + for (int j = 0; j < l.size(); j++) { + totalSpace-=l.get(j).length(); + } + int eachSpace=totalSpace/(l.size()-1); + int extraSpace=totalSpace-eachSpace*(l.size()-1); + StringBuffer sb=new StringBuffer(); + for (int j = 0; j < l.size()-1; j++) { + sb.append(l.get(j)); + for (int k = 0; k < eachSpace; k++) { + sb.append(" "); + } + if (j l=sumList.get(sumList.size()-1); StringBuffer sb=new StringBuffer(); for (int i = 0; i < l.size()-1; i++) { - sb.append(l.get(i)); - sb.append(" "); - } + sb.append(l.get(i)); + sb.append(" "); + } sb.append(l.get(l.size()-1)); int tt=sb.toString().length(); for (int j = 0; j < maxWidth-tt; j++) { - sb.append(" "); - } + sb.append(" "); + } ans.add(sb.toString()); - return ans; + return ans; } } \ No newline at end of file diff --git a/java/069. Sqrt(x).java b/java/069. Sqrt(x).java index ebc9f31..3232967 100644 --- a/java/069. Sqrt(x).java +++ b/java/069. Sqrt(x).java @@ -7,17 +7,17 @@ public int mySqrt(int x) { return 1; } int s=1; - for (int i = 0; i <20; i++) { - s=(x/s)/2+s/2; - } - int k=0; - int answer=0; - for (int i = -30; i < 30; i++) { - k=s+i; - if (k*k>0&&k*k<=x) { - answer=k; - } - } - return answer; + for (int i = 0; i <20; i++) { + s=(x/s)/2+s/2; + } + int k=0; + int answer=0; + for (int i = -30; i < 30; i++) { + k=s+i; + if (k*k>0&&k*k<=x) { + answer=k; + } + } + return answer; } } \ No newline at end of file diff --git a/java/070. Climbing Stairs.java b/java/070. Climbing Stairs.java index 3911394..ee87759 100644 --- a/java/070. Climbing Stairs.java +++ b/java/070. Climbing Stairs.java @@ -1,15 +1,15 @@ public class Solution { public int climbStairs(int n) { if (n<=2) { - return n; - }else { - int[] sk=new int[n]; - sk[0]=1; - sk[1]=2; - for (int i =2; i < sk.length; i++) { - sk[i]=sk[i-1]+sk[i-2]; - } - return sk[n-1]; - } + return n; + }else { + int[] sk=new int[n]; + sk[0]=1; + sk[1]=2; + for (int i =2; i < sk.length; i++) { + sk[i]=sk[i-1]+sk[i-2]; + } + return sk[n-1]; + } } } \ No newline at end of file diff --git a/java/072. Edit Distance.java b/java/072. Edit Distance.java index e3a726c..d3f9fa7 100644 --- a/java/072. Edit Distance.java +++ b/java/072. Edit Distance.java @@ -3,26 +3,26 @@ public int minDistance(String word1, String word2) { int l1=word1.length(); int l2=word2.length(); if (l1*l2==0) { - return l1==0?l2:l1; - } + return l1==0?l2:l1; + } int[][] board=new int[l1+1][l2+1]; for (int i = 0; i < l1+1; i++) { - board[i][0]=i; - } + board[i][0]=i; + } for (int i = 0; i < l2+1; i++) { - board[0][i]=i; - } + board[0][i]=i; + } char[] c1=word1.toCharArray(); char[] c2=word2.toCharArray(); for (int i = 1; i < l1+1; i++) { - for (int j = 1; j < l2+1; j++) { - if (c1[i-1]==c2[j-1]) { - board[i][j]=board[i-1][j-1]; - }else { - board[i][j]=Math.min(Math.min(board[i-1][j], board[i][j-1]), board[i-1][j-1])+1; - } - } - } + for (int j = 1; j < l2+1; j++) { + if (c1[i-1]==c2[j-1]) { + board[i][j]=board[i-1][j-1]; + }else { + board[i][j]=Math.min(Math.min(board[i-1][j], board[i][j-1]), board[i-1][j-1])+1; + } + } + } return board[l1][l2]; } } \ No newline at end of file diff --git a/java/073. Set Matrix Zeroes.java b/java/073. Set Matrix Zeroes.java index 369e501..ae50f07 100644 --- a/java/073. Set Matrix Zeroes.java +++ b/java/073. Set Matrix Zeroes.java @@ -1,42 +1,42 @@ public class Solution { public void setZeroes(int[][] matrix) { - boolean f=true; - for (int i = 0; i < matrix.length; i++) { - if (matrix[i][0]==0) { - f=false; - break; - } - } - boolean l=true; - for (int i = 0; i < matrix[0].length; i++) { - if (matrix[0][i]==0) { - l=false; - } - } + boolean f=true; for (int i = 0; i < matrix.length; i++) { - for (int j = 0; j < matrix[i].length; j++) { - if (matrix[i][j]==0) { - matrix[i][0]=0; - matrix[0][j]=0; - } - } - } + if (matrix[i][0]==0) { + f=false; + break; + } + } + boolean l=true; + for (int i = 0; i < matrix[0].length; i++) { + if (matrix[0][i]==0) { + l=false; + } + } + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[i].length; j++) { + if (matrix[i][j]==0) { + matrix[i][0]=0; + matrix[0][j]=0; + } + } + } for (int i = 1; i < matrix.length; i++) { - for (int j = 1; j < matrix[i].length; j++) { - if (matrix[i][0]==0||matrix[0][j]==0) { - matrix[i][j]=0; - } - } - } + for (int j = 1; j < matrix[i].length; j++) { + if (matrix[i][0]==0||matrix[0][j]==0) { + matrix[i][j]=0; + } + } + } if (!f) { - for (int i = 0; i < matrix.length; i++) { - matrix[i][0]=0; - } - } + for (int i = 0; i < matrix.length; i++) { + matrix[i][0]=0; + } + } if (!l) { - for (int i = 0; i < matrix[0].length; i++) { - matrix[0][i]=0; - } - } + for (int i = 0; i < matrix[0].length; i++) { + matrix[0][i]=0; + } + } } } \ No newline at end of file diff --git a/java/074. Search a 2D Matrix.java b/java/074. Search a 2D Matrix.java index 5f55259..a2d5a30 100644 --- a/java/074. Search a 2D Matrix.java +++ b/java/074. Search a 2D Matrix.java @@ -1,48 +1,48 @@ public class Solution { public boolean searchMatrix(int[][] matrix, int target) { - int begin=0; - int end=matrix.length-1; - int t=end/2; - if (targetmatrix[matrix.length-1][matrix[matrix.length-1].length-1]) { - return false; - } + int begin=0; + int end=matrix.length-1; + int t=end/2; + if (targetmatrix[matrix.length-1][matrix[matrix.length-1].length-1]) { + return false; + } while (end-begin>1) { - if (matrix[t][0]==target) { - return true; - }else if (matrix[end][0]target) { - end=t; - t=(begin+end)/2; - }else { - begin=t; - t=(begin+end)/2; - } - } + if (matrix[t][0]==target) { + return true; + }else if (matrix[end][0]target) { + end=t; + t=(begin+end)/2; + }else { + begin=t; + t=(begin+end)/2; + } + } if (matrix[t][0]==target||matrix[begin][0]==target||matrix[end][0]==target) { - return true; - }else if (matrix[t][0]>target) { - t=t-1; - } + return true; + }else if (matrix[t][0]>target) { + t=t-1; + } int b=0; int e=matrix[t].length-1; int tt=(b+e)/2; while (e-b>1) { - if (matrix[t][tt]==target) { - return true; - }else if (matrix[t][tt]>target) { - e=tt; - tt=(b+e)/2; - }else { - b=tt; - tt=(b+e)/2; - } - } + if (matrix[t][tt]==target) { + return true; + }else if (matrix[t][tt]>target) { + e=tt; + tt=(b+e)/2; + }else { + b=tt; + tt=(b+e)/2; + } + } if (matrix[t][b]==target||matrix[t][e]==target) { - return true; - } - return false; + return true; + } + return false; } } \ No newline at end of file diff --git a/java/075. Sort Colors.java b/java/075. Sort Colors.java index 4db3dbe..7eeed81 100644 --- a/java/075. Sort Colors.java +++ b/java/075. Sort Colors.java @@ -1,47 +1,47 @@ public class Solution { public void sortColors(int[] nums) { - int t=0; - int b=0; - int e=nums.length-1; - boolean flag=true; + int t=0; + int b=0; + int e=nums.length-1; + boolean flag=true; for (int i = 0; i < nums.length; i++) { - if (flag) { - if (nums[b]!=0) { - t=b; - flag=false; - }else { - t++; - } - b++; - }else { - if (nums[e]==0) { - nums[e]=nums[t]; - nums[t]=0; - flag=true; - t++; - } - e--; - } - } - b=t; - e=nums.length-1; - flag=true; - for (int i = t; i < nums.length; i++) { - if (flag) { - if (nums[b]!=1) { - t=b; - flag=false; - } - b++; - }else { - if (nums[e]==1) { - nums[e]=nums[t]; - nums[t]=1; - flag=true; - } - e--; - } - } + if (flag) { + if (nums[b]!=0) { + t=b; + flag=false; + }else { + t++; + } + b++; + }else { + if (nums[e]==0) { + nums[e]=nums[t]; + nums[t]=0; + flag=true; + t++; + } + e--; + } + } + b=t; + e=nums.length-1; + flag=true; + for (int i = t; i < nums.length; i++) { + if (flag) { + if (nums[b]!=1) { + t=b; + flag=false; + } + b++; + }else { + if (nums[e]==1) { + nums[e]=nums[t]; + nums[t]=1; + flag=true; + } + e--; + } + } } diff --git a/java/077. Combinations.java b/java/077. Combinations.java index 870319d..55a6944 100644 --- a/java/077. Combinations.java +++ b/java/077. Combinations.java @@ -2,31 +2,31 @@ public class Solution { public List> combine(int n, int k) { List> answerList=new ArrayList>(); for (int i = 1; i <=n; i++) { - List list=new ArrayList(); - list.add(i); - answerList.add(list); - } + List list=new ArrayList(); + list.add(i); + answerList.add(list); + } for (int i = 0; i =0; i--) { - nums1[i+nums1.length-m]=nums1[i]; - } + nums1[i+nums1.length-m]=nums1[i]; + } int a=nums1.length-m; int b=0; for (int i = 0; i < m+n; i++) { - if (a==nums1.length) { - nums1[i]=nums2[b]; - b++; - }else if (b==n||nums1[a] grayCode(int n) { - List ans=new ArrayList(); - List list=new ArrayList(); - if (n==0) { - ans.add(0); - return ans; - } - list.add("0"); - list.add("1"); - for (int i = 1; i < n; i++) { - List newList=new ArrayList(); - for (int j = 0; j < list.size(); j++) { - newList.add("0"+list.get(j)); - } - for (int j = list.size()-1; j >= 0 ; j--) { - newList.add("1"+list.get(j)); - } - list=newList; - } - for (int i = 0; i < list.size(); i++) { - ans.add(Integer.valueOf(list.get(i), 2)); - } - return ans; + List ans=new ArrayList(); + List list=new ArrayList(); + if (n==0) { + ans.add(0); + return ans; + } + list.add("0"); + list.add("1"); + for (int i = 1; i < n; i++) { + List newList=new ArrayList(); + for (int j = 0; j < list.size(); j++) { + newList.add("0"+list.get(j)); + } + for (int j = list.size()-1; j >= 0 ; j--) { + newList.add("1"+list.get(j)); + } + list=newList; + } + for (int i = 0; i < list.size(); i++) { + ans.add(Integer.valueOf(list.get(i), 2)); + } + return ans; } } \ No newline at end of file diff --git a/java/090. Subsets II.java b/java/090. Subsets II.java index 9972d7d..c42aea3 100644 --- a/java/090. Subsets II.java +++ b/java/090. Subsets II.java @@ -1,39 +1,39 @@ public class Solution { public List> subsetsWithDup(int[] nums) { - for (int i = 0; i < nums.length; i++) { - for (int j = i+1; j < nums.length; j++) { - if (nums[i]=0;i--) { - n[i]=sum; - sum*=2; - } + n[i]=sum; + sum*=2; + } List> answer=new ArrayList>(); for (int i = 0; i < sum; i++) { - List list=new ArrayList(); - for (int j = nums.length-1; j >=0 ; j--) { - if ((i/n[j])%2==1) { - list.add(nums[j]); - } - } - answer.add(list); - } + List list=new ArrayList(); + for (int j = nums.length-1; j >=0 ; j--) { + if ((i/n[j])%2==1) { + list.add(nums[j]); + } + } + answer.add(list); + } Map, Integer> map=new HashMap, Integer>(); for (int i = 0; i < answer.size(); i++) { - if (map.get(answer.get(i))==null) { - map.put(answer.get(i), 1); - }else { - answer.remove(i); - i--; - } - } - return answer; + if (map.get(answer.get(i))==null) { + map.put(answer.get(i), 1); + }else { + answer.remove(i); + i--; + } + } + return answer; } } \ No newline at end of file diff --git a/java/091. Decode Ways.java b/java/091. Decode Ways.java index edd9171..fbc086c 100644 --- a/java/091. Decode Ways.java +++ b/java/091. Decode Ways.java @@ -1,29 +1,29 @@ public class Solution { public int numDecodings(String s) { - if (s.length()==0) { - return 0; - } + if (s.length()==0) { + return 0; + } Map map=new HashMap(); return f(s,map); } public int f(String s,Map map){ - if (s.length()==0) { - return 1; - } - if (map.get(s)==null) { - int length=s.length(); - int one=-1; - if (length>=1) { - one=Integer.parseInt(s.substring(0,1)); - } - int two=-1; - if (length>=2) { - two=Integer.parseInt(s.substring(0,2)); - } - int value=(one>0?f(s.substring(1,s.length()),map):0)+((one>0&&two>0&&two<27)?f(s.substring(2,s.length()),map):0); - map.put(s, value); - return value; - } - return map.get(s); + if (s.length()==0) { + return 1; + } + if (map.get(s)==null) { + int length=s.length(); + int one=-1; + if (length>=1) { + one=Integer.parseInt(s.substring(0,1)); + } + int two=-1; + if (length>=2) { + two=Integer.parseInt(s.substring(0,2)); + } + int value=(one>0?f(s.substring(1,s.length()),map):0)+((one>0&&two>0&&two<27)?f(s.substring(2,s.length()),map):0); + map.put(s, value); + return value; + } + return map.get(s); } } \ No newline at end of file diff --git a/java/092. Reverse Linked List II.java b/java/092. Reverse Linked List II.java index d72ef24..6bc6dc7 100644 --- a/java/092. Reverse Linked List II.java +++ b/java/092. Reverse Linked List II.java @@ -8,23 +8,23 @@ */ public class Solution { public ListNode reverseBetween(ListNode head, int m, int n) { - List list=f(head, new ArrayList()); - list.get(m-1).next=list.get(n-1).next; - for (int i = n-1; i >=m; i--) { - list.get(i).next=list.get(i-1); - } - if (m!=1) { - list.get(m-2).next=list.get(n-1); - return list.get(0); - }else { - return list.get(n-1); - } + List list=f(head, new ArrayList()); + list.get(m-1).next=list.get(n-1).next; + for (int i = n-1; i >=m; i--) { + list.get(i).next=list.get(i-1); + } + if (m!=1) { + list.get(m-2).next=list.get(n-1); + return list.get(0); + }else { + return list.get(n-1); + } } public List f(ListNode head,List list){ - if (head!=null) { - list.add(head); - list=f(head.next,list); - } - return list; + if (head!=null) { + list.add(head); + list=f(head.next,list); + } + return list; } } \ No newline at end of file diff --git a/java/093. Restore IP Addresses.java b/java/093. Restore IP Addresses.java index ab02e69..4e4efb9 100644 --- a/java/093. Restore IP Addresses.java +++ b/java/093. Restore IP Addresses.java @@ -1,32 +1,32 @@ public class Solution { public List restoreIpAddresses(String s) { - List answerList=new ArrayList(); + List answerList=new ArrayList(); for (int i = 1; i <=3; i++) { - for (int j = 1; j <=3; j++) { - for (int k = 1; k <=3; k++) { - int[] a={i,j,k}; - if (i+j+k3||(s4.length()>1&&s4.substring(0, 1).equals("0"))||Integer.parseInt(s4)>255) { - return ""; - } - String s1=s.substring(0,a[0]); - String s2=s.substring(a[0],a[0]+a[1]); - String s3=s.substring(a[0]+a[1],a[0]+a[1]+a[2]); - if ((s3.length()>1&&s3.substring(0, 1).equals("0"))||(s2.length()>1&&s2.substring(0, 1).equals("0"))||(s1.length()>1&&s1.substring(0, 1).equals("0"))||Integer.parseInt(s3)>255||Integer.parseInt(s2)>255||Integer.parseInt(s1)>255) { - return ""; - } - return s1+"."+s2+"."+s3+"."+s4; + String s4=s.substring(a[0]+a[1]+a[2],s.length()); + if (s4.length()>3||(s4.length()>1&&s4.substring(0, 1).equals("0"))||Integer.parseInt(s4)>255) { + return ""; + } + String s1=s.substring(0,a[0]); + String s2=s.substring(a[0],a[0]+a[1]); + String s3=s.substring(a[0]+a[1],a[0]+a[1]+a[2]); + if ((s3.length()>1&&s3.substring(0, 1).equals("0"))||(s2.length()>1&&s2.substring(0, 1).equals("0"))||(s1.length()>1&&s1.substring(0, 1).equals("0"))||Integer.parseInt(s3)>255||Integer.parseInt(s2)>255||Integer.parseInt(s1)>255) { + return ""; + } + return s1+"."+s2+"."+s3+"."+s4; } } \ No newline at end of file diff --git a/java/094. Binary Tree Inorder Traversal.java b/java/094. Binary Tree Inorder Traversal.java index 79b5ca9..a0aba43 100644 --- a/java/094. Binary Tree Inorder Traversal.java +++ b/java/094. Binary Tree Inorder Traversal.java @@ -9,14 +9,14 @@ */ public class Solution { public List inorderTraversal(TreeNode root) { - return inorder(root,new ArrayList()); + return inorder(root,new ArrayList()); } public List inorder(TreeNode root,List list){ - if (root!=null) { - list=inorder(root.left,list); - list.add(root.val); - list=inorder(root.right,list); - } - return list; + if (root!=null) { + list=inorder(root.left,list); + list.add(root.val); + list=inorder(root.right,list); + } + return list; } } \ No newline at end of file diff --git a/java/095. Unique Binary Search Trees II.java b/java/095. Unique Binary Search Trees II.java index ee50c4a..a862954 100644 --- a/java/095. Unique Binary Search Trees II.java +++ b/java/095. Unique Binary Search Trees II.java @@ -12,29 +12,29 @@ public List generateTrees(int n) { return f(1,n,new HashMap>()); } public List f(int s,int e,Map> map) { - if (map.get(""+s+","+e)==null) { - List ans=new ArrayList(); - if (s>e) { - ans.add(null); - }else if (s==e) { - ans.add(new TreeNode(s)); - }else { - for (int i = s; i <=e; i++) { - List left=f(s,i-1,map); - List right=f(i+1,e,map); - for (int j = 0; j < left.size(); j++) { - for (int k = 0; k < right.size(); k++) { - TreeNode node=new TreeNode(i); - node.left=left.get(j); - node.right=right.get(k); - ans.add(node); - } - } - } - } - map.put(""+s+","+e, ans); - return ans; - } - return map.get(""+s+","+e); + if (map.get(""+s+","+e)==null) { + List ans=new ArrayList(); + if (s>e) { + ans.add(null); + }else if (s==e) { + ans.add(new TreeNode(s)); + }else { + for (int i = s; i <=e; i++) { + List left=f(s,i-1,map); + List right=f(i+1,e,map); + for (int j = 0; j < left.size(); j++) { + for (int k = 0; k < right.size(); k++) { + TreeNode node=new TreeNode(i); + node.left=left.get(j); + node.right=right.get(k); + ans.add(node); + } + } + } + } + map.put(""+s+","+e, ans); + return ans; + } + return map.get(""+s+","+e); } } \ No newline at end of file diff --git a/java/096. Unique Binary Search Trees.java b/java/096. Unique Binary Search Trees.java index b57aed6..843146a 100644 --- a/java/096. Unique Binary Search Trees.java +++ b/java/096. Unique Binary Search Trees.java @@ -1,24 +1,24 @@ public class Solution { public int numTrees(int n) { - return f(n,new HashMap()); + return f(n,new HashMap()); } public int f(int n,Map map){ - if (map.get(n)!=null) { - return map.get(n); - }else { - - if (n==1||n==0) { - return 1; - }else { - int sum=0; - for (int i = 0; i < n; i++) { - int left=f(i,map); - int right=f((n-1-i),map); - sum+=(left*right); - } - map.put(n,sum); - return sum; - } - } + if (map.get(n)!=null) { + return map.get(n); + }else { + + if (n==1||n==0) { + return 1; + }else { + int sum=0; + for (int i = 0; i < n; i++) { + int left=f(i,map); + int right=f((n-1-i),map); + sum+=(left*right); + } + map.put(n,sum); + return sum; + } + } } } \ No newline at end of file diff --git a/java/097. Interleaving String.java b/java/097. Interleaving String.java index 3f46ff0..9ed1345 100644 --- a/java/097. Interleaving String.java +++ b/java/097. Interleaving String.java @@ -1,52 +1,52 @@ public class Solution { public boolean isInterleave(String s1, String s2, String s3) { - return f(s1,s2,s3,new HashMap()); + return f(s1,s2,s3,new HashMap()); } public boolean f(String s1, String s2, String s3,Map map){ - if (map.get(s1+","+s2+","+s3)==null) { - if (s3.length()!=(s1.length()+s2.length())) { - return false; - } - if (s3.length()==0) { - return true; - } - - String w=s3.substring(0,1); - boolean flag1=false; - boolean flag2=false; - if (s1.length()!=0) { - if(s2.length()==0){ - flag1=s1.equals(s3); - }else { - String w1=s1.substring(0,1); - if (w.equals(w1)) { - flag1=f(s1.substring(1, s1.length()),s2,s3.substring(1, s3.length()),map); - } - } - if (flag1) { - return flag1; - }else { - map.put(s1.substring(1, s1.length())+","+s2+","+s3.substring(1, s3.length()),false); - } - } - if (s2.length()!=0) { - if (s1.length()==0) { - flag2=s2.equals(s3); - }else { - String w2=s2.substring(0,1); - if (w.equals(w2)) { - flag2=f(s1,s2.substring(1, s2.length()),s3.substring(1, s3.length()),map); - } - } - if (flag2) { - return flag2; - }else { - map.put(s1+","+s2.substring(1, s2.length())+","+s3.substring(1, s3.length()),false); - map.put(s1+","+s2+","+s3,false); - } - } + if (map.get(s1+","+s2+","+s3)==null) { + if (s3.length()!=(s1.length()+s2.length())) { + return false; + } + if (s3.length()==0) { + return true; + } + + String w=s3.substring(0,1); + boolean flag1=false; + boolean flag2=false; + if (s1.length()!=0) { + if(s2.length()==0){ + flag1=s1.equals(s3); + }else { + String w1=s1.substring(0,1); + if (w.equals(w1)) { + flag1=f(s1.substring(1, s1.length()),s2,s3.substring(1, s3.length()),map); + } + } + if (flag1) { + return flag1; + }else { + map.put(s1.substring(1, s1.length())+","+s2+","+s3.substring(1, s3.length()),false); + } + } + if (s2.length()!=0) { + if (s1.length()==0) { + flag2=s2.equals(s3); + }else { + String w2=s2.substring(0,1); + if (w.equals(w2)) { + flag2=f(s1,s2.substring(1, s2.length()),s3.substring(1, s3.length()),map); + } + } + if (flag2) { + return flag2; + }else { + map.put(s1+","+s2.substring(1, s2.length())+","+s3.substring(1, s3.length()),false); + map.put(s1+","+s2+","+s3,false); + } + } } - return false; + return false; } diff --git a/java/098. Validate Binary Search Tree.java b/java/098. Validate Binary Search Tree.java index 7123633..f307ca5 100644 --- a/java/098. Validate Binary Search Tree.java +++ b/java/098. Validate Binary Search Tree.java @@ -9,19 +9,19 @@ */ public class Solution { public boolean isValidBST(TreeNode root) { - if (root==null) { - return true; - } - List list=inorder(root,new ArrayList()); - if (list.size()<2) { - return true; - } - for (int i = 0; i < list.size()-1; i++) { - if (list.get(i)>=list.get(i+1)) { - return false; - } - } - return true; + if (root==null) { + return true; + } + List list=inorder(root,new ArrayList()); + if (list.size()<2) { + return true; + } + for (int i = 0; i < list.size()-1; i++) { + if (list.get(i)>=list.get(i+1)) { + return false; + } + } + return true; } public List inorder(TreeNode root,List list){ if (root!=null) { diff --git a/java/100. Same Tree.java b/java/100. Same Tree.java index 99ff095..24d4b65 100644 --- a/java/100. Same Tree.java +++ b/java/100. Same Tree.java @@ -9,16 +9,16 @@ */ public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { - if (p==null&&q==null) { - return true; - }else if (p==null||q==null) { - return false; - } - if (p.val!=q.val) { - return false; - }else{ - return isSameTree(p.left, q.left)&&isSameTree(p.right, q.right); - } - + if (p==null&&q==null) { + return true; + }else if (p==null||q==null) { + return false; + } + if (p.val!=q.val) { + return false; + }else{ + return isSameTree(p.left, q.left)&&isSameTree(p.right, q.right); + } + } } \ No newline at end of file diff --git a/java/101. Symmetric Tree.java b/java/101. Symmetric Tree.java index 480a62c..2417638 100644 --- a/java/101. Symmetric Tree.java +++ b/java/101. Symmetric Tree.java @@ -10,23 +10,23 @@ public class Solution { public boolean isSymmetric(TreeNode root) { if (root==null) { - return true; - }else { - TreeNode p=root.left; - TreeNode q=root.right; - return isSy(p, q); - } + return true; + }else { + TreeNode p=root.left; + TreeNode q=root.right; + return isSy(p, q); + } } public boolean isSy(TreeNode p,TreeNode q){ - if (p==null&&q==null) { - return true; - }else if (p==null||q==null) { - return false; - } - if (p.val!=q.val) { - return false; - }else{ - return isSy(p.right, q.left)&&isSy(p.left, q.right); - } + if (p==null&&q==null) { + return true; + }else if (p==null||q==null) { + return false; + } + if (p.val!=q.val) { + return false; + }else{ + return isSy(p.right, q.left)&&isSy(p.left, q.right); + } } } \ No newline at end of file diff --git a/java/102. Binary Tree Level Order Traversal.java b/java/102. Binary Tree Level Order Traversal.java index d375e6c..bd5a4e3 100644 --- a/java/102. Binary Tree Level Order Traversal.java +++ b/java/102. Binary Tree Level Order Traversal.java @@ -9,32 +9,32 @@ */ public class Solution { public List> levelOrder(TreeNode root) { - List> answerList = new ArrayList>(); - List> list = new ArrayList>(); - if (root == null) { - return list; - } - List initList = new ArrayList(); - initList.add(root); - answerList.add(initList); - for (int i = 0; i < answerList.size(); i++) { - List l = new ArrayList(); - List newList = new ArrayList(); - for (int j = 0; j < answerList.get(i).size(); j++) { - TreeNode tree = answerList.get(i).get(j); - l.add(answerList.get(i).get(j).val); - if (tree.left != null) { - newList.add(tree.left); - } - if (tree.right != null) { - newList.add(tree.right); - } - } - list.add(l); - if (newList.size() != 0) { - answerList.add(newList); - } - } - return list; - } + List> answerList = new ArrayList>(); + List> list = new ArrayList>(); + if (root == null) { + return list; + } + List initList = new ArrayList(); + initList.add(root); + answerList.add(initList); + for (int i = 0; i < answerList.size(); i++) { + List l = new ArrayList(); + List newList = new ArrayList(); + for (int j = 0; j < answerList.get(i).size(); j++) { + TreeNode tree = answerList.get(i).get(j); + l.add(answerList.get(i).get(j).val); + if (tree.left != null) { + newList.add(tree.left); + } + if (tree.right != null) { + newList.add(tree.right); + } + } + list.add(l); + if (newList.size() != 0) { + answerList.add(newList); + } + } + return list; + } } \ No newline at end of file diff --git a/java/103. Binary Tree Zigzag Level Order Traversal.java b/java/103. Binary Tree Zigzag Level Order Traversal.java index ce26e94..34b4345 100644 --- a/java/103. Binary Tree Zigzag Level Order Traversal.java +++ b/java/103. Binary Tree Zigzag Level Order Traversal.java @@ -9,42 +9,42 @@ */ public class Solution { public List> zigzagLevelOrder(TreeNode root) { - List> answerList = new ArrayList>(); - List> list = new ArrayList>(); - if (root == null) { - return list; - } - List initList = new ArrayList(); - initList.add(root); - answerList.add(initList); - for (int i = 0; i < answerList.size(); i++) { - System.out.println("i=" + i); - List l = new ArrayList(); - List newList = new ArrayList(); - for (int j = answerList.get(i).size() - 1; j >= 0; j--) { - TreeNode tree = answerList.get(i).get(j); - l.add(answerList.get(i).get(j).val); - if (i % 2 == 1) { - if (tree.right != null) { - newList.add(tree.right); - } - if (tree.left != null) { - newList.add(tree.left); - } - } else { - if (tree.left != null) { - newList.add(tree.left); - } - if (tree.right != null) { - newList.add(tree.right); - } - } - } - list.add(l); - if (newList.size() != 0) { - answerList.add(newList); - } - } - return list; - } + List> answerList = new ArrayList>(); + List> list = new ArrayList>(); + if (root == null) { + return list; + } + List initList = new ArrayList(); + initList.add(root); + answerList.add(initList); + for (int i = 0; i < answerList.size(); i++) { + System.out.println("i=" + i); + List l = new ArrayList(); + List newList = new ArrayList(); + for (int j = answerList.get(i).size() - 1; j >= 0; j--) { + TreeNode tree = answerList.get(i).get(j); + l.add(answerList.get(i).get(j).val); + if (i % 2 == 1) { + if (tree.right != null) { + newList.add(tree.right); + } + if (tree.left != null) { + newList.add(tree.left); + } + } else { + if (tree.left != null) { + newList.add(tree.left); + } + if (tree.right != null) { + newList.add(tree.right); + } + } + } + list.add(l); + if (newList.size() != 0) { + answerList.add(newList); + } + } + return list; + } } \ No newline at end of file diff --git a/java/104. Maximum Depth of Binary Tree.java b/java/104. Maximum Depth of Binary Tree.java index 5910891..0510054 100644 --- a/java/104. Maximum Depth of Binary Tree.java +++ b/java/104. Maximum Depth of Binary Tree.java @@ -9,28 +9,28 @@ */ public class Solution { public int maxDepth(TreeNode root) { - if (root==null) { - return 0; - } - if(root.left==null&&root.right==null){ - return 1; - } - int left=-1; - if(root.left!=null){ - left=maxDepth(root.left)+1; - } - int right=-1; - if(root.right!=null){ - right=maxDepth(root.right)+1; - } - if(left==-1){ - return right; - } - if(right==-1){ - return left; - } - - - return Math.max(left, right); - } + if (root==null) { + return 0; + } + if(root.left==null&&root.right==null){ + return 1; + } + int left=-1; + if(root.left!=null){ + left=maxDepth(root.left)+1; + } + int right=-1; + if(root.right!=null){ + right=maxDepth(root.right)+1; + } + if(left==-1){ + return right; + } + if(right==-1){ + return left; + } + + + return Math.max(left, right); + } } \ No newline at end of file diff --git a/java/105. Construct Binary Tree from Preorder and Inorder Traversal.java b/java/105. Construct Binary Tree from Preorder and Inorder Traversal.java index 609cde8..3ce4f9d 100644 --- a/java/105. Construct Binary Tree from Preorder and Inorder Traversal.java +++ b/java/105. Construct Binary Tree from Preorder and Inorder Traversal.java @@ -9,24 +9,24 @@ */ public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { - return build(preorder, inorder,0,preorder.length-1,0,inorder.length-1); + return build(preorder, inorder,0,preorder.length-1,0,inorder.length-1); } public TreeNode build(int[] preorder, int[] inorder,int ps,int pe,int is,int ie){ - if(ps>pe){ - return null; - } - int val=preorder[ps]; - TreeNode node=new TreeNode(val); - int i = is; - for (; i <=ie; i++) { - if (inorder[i]==val) { - break; - } - } - int rang=i-is; - node.left=build(preorder, inorder,ps+1,ps+rang,is,i-1); - node.right=build(preorder,inorder,ps+rang+1,pe,i+1,ie); - return node; - + if(ps>pe){ + return null; + } + int val=preorder[ps]; + TreeNode node=new TreeNode(val); + int i = is; + for (; i <=ie; i++) { + if (inorder[i]==val) { + break; + } + } + int rang=i-is; + node.left=build(preorder, inorder,ps+1,ps+rang,is,i-1); + node.right=build(preorder,inorder,ps+rang+1,pe,i+1,ie); + return node; + } } \ No newline at end of file diff --git a/java/106. Construct Binary Tree from Inorder and Postorder Traversal.java b/java/106. Construct Binary Tree from Inorder and Postorder Traversal.java index 357ea0e..e2924d9 100644 --- a/java/106. Construct Binary Tree from Inorder and Postorder Traversal.java +++ b/java/106. Construct Binary Tree from Inorder and Postorder Traversal.java @@ -9,24 +9,24 @@ */ public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { - return build(inorder, postorder,0,inorder.length-1,0,postorder.length-1); + return build(inorder, postorder,0,inorder.length-1,0,postorder.length-1); } public TreeNode build(int[] inorder, int[] postorder,int is,int ie,int ps,int pe){ - if(ps>pe){ - return null; - } - int val=postorder[pe]; - TreeNode node=new TreeNode(val); - int i = is; - for (; i <=ie; i++) { - if (inorder[i]==val) { - break; - } - } - int rang=i-is; - node.left=build(inorder, postorder,is,i-1,ps,ps+rang-1); - node.right=build(inorder,postorder,i+1,ie,ps+rang,pe-1); - return node; - + if(ps>pe){ + return null; + } + int val=postorder[pe]; + TreeNode node=new TreeNode(val); + int i = is; + for (; i <=ie; i++) { + if (inorder[i]==val) { + break; + } + } + int rang=i-is; + node.left=build(inorder, postorder,is,i-1,ps,ps+rang-1); + node.right=build(inorder,postorder,i+1,ie,ps+rang,pe-1); + return node; + } } \ No newline at end of file diff --git a/java/107. Binary Tree Level Order Traversal II.java b/java/107. Binary Tree Level Order Traversal II.java index 4fdd182..0e11503 100644 --- a/java/107. Binary Tree Level Order Traversal II.java +++ b/java/107. Binary Tree Level Order Traversal II.java @@ -9,37 +9,37 @@ */ public class Solution { public List> levelOrderBottom(TreeNode root) { - List> answerList = new ArrayList>(); - List> list = new ArrayList>(); - if (root == null) { - return list; - } - List initList = new ArrayList(); - initList.add(root); - answerList.add(initList); - for (int i = 0; i < answerList.size(); i++) { - List newList = new ArrayList(); - for (int j = 0; j < answerList.get(i).size(); j++) { - TreeNode tree = answerList.get(i).get(j); - if (tree.left != null) { - newList.add(tree.left); - } - if (tree.right != null) { - newList.add(tree.right); - } - } - if (newList.size() != 0) { - answerList.add(newList); - } - } - for (int i = answerList.size()-1; i >=0; i--) { - List l = new ArrayList(); - for (int j = 0; j < answerList.get(i).size(); j++) { - l.add(answerList.get(i).get(j).val); - } - list.add(l); - } - - return list; - } + List> answerList = new ArrayList>(); + List> list = new ArrayList>(); + if (root == null) { + return list; + } + List initList = new ArrayList(); + initList.add(root); + answerList.add(initList); + for (int i = 0; i < answerList.size(); i++) { + List newList = new ArrayList(); + for (int j = 0; j < answerList.get(i).size(); j++) { + TreeNode tree = answerList.get(i).get(j); + if (tree.left != null) { + newList.add(tree.left); + } + if (tree.right != null) { + newList.add(tree.right); + } + } + if (newList.size() != 0) { + answerList.add(newList); + } + } + for (int i = answerList.size()-1; i >=0; i--) { + List l = new ArrayList(); + for (int j = 0; j < answerList.get(i).size(); j++) { + l.add(answerList.get(i).get(j).val); + } + list.add(l); + } + + return list; + } } \ No newline at end of file diff --git a/java/108. Convert Sorted Array to Binary Search Tree.java b/java/108. Convert Sorted Array to Binary Search Tree.java index 8ae5e5a..3c3eb3c 100644 --- a/java/108. Convert Sorted Array to Binary Search Tree.java +++ b/java/108. Convert Sorted Array to Binary Search Tree.java @@ -9,17 +9,17 @@ */ public class Solution { public TreeNode sortedArrayToBST(int[] nums) { - return build(nums,0,nums.length-1); + return build(nums,0,nums.length-1); } public TreeNode build(int[] nums,int s,int e){ - if (s>e) { - return null; - } - int m=(s+e)/2; + if (s>e) { + return null; + } + int m=(s+e)/2; int val=nums[m]; TreeNode node=new TreeNode(val); node.left=build(nums,s,m-1); - node.right=build(nums,m+1,e); - return node; + node.right=build(nums,m+1,e); + return node; } } \ No newline at end of file diff --git a/java/109. Convert Sorted List to Binary Search Tree.java b/java/109. Convert Sorted List to Binary Search Tree.java index 95504ef..5807186 100644 --- a/java/109. Convert Sorted List to Binary Search Tree.java +++ b/java/109. Convert Sorted List to Binary Search Tree.java @@ -17,21 +17,21 @@ */ public class Solution { public TreeNode sortedListToBST(ListNode head) { - return f(head,null); + return f(head,null); } public TreeNode f(ListNode head,ListNode end) { - if (head==end) { - return null; - } + if (head==end) { + return null; + } ListNode fast=head; ListNode slow=head; - while (fast.next!=end&&fast.next.next!=end) { - fast=fast.next.next; - slow=slow.next; - } - TreeNode node=new TreeNode(slow.val); - node.right=f(slow.next,end); - node.left=f(head,slow); - return node; + while (fast.next!=end&&fast.next.next!=end) { + fast=fast.next.next; + slow=slow.next; + } + TreeNode node=new TreeNode(slow.val); + node.right=f(slow.next,end); + node.left=f(head,slow); + return node; } } \ No newline at end of file diff --git a/java/110. Balanced Binary Tree.java b/java/110. Balanced Binary Tree.java index 6aefe3a..37852ca 100644 --- a/java/110. Balanced Binary Tree.java +++ b/java/110. Balanced Binary Tree.java @@ -10,18 +10,18 @@ public class Solution { public boolean isBalanced(TreeNode root) { if (root==null) { - return true; - } + return true; + } if (Math.abs(f(root.left)-f(root.right))>1) { - return false; - } + return false; + } return isBalanced(root.left)&&isBalanced(root.right); } public int f(TreeNode root){ - if (root==null) { - return 0; - }else { - return 1+Math.max(f(root.left), f(root.right)); - } + if (root==null) { + return 0; + }else { + return 1+Math.max(f(root.left), f(root.right)); + } } } \ No newline at end of file diff --git a/java/111. Minimum Depth of Binary Tree.java b/java/111. Minimum Depth of Binary Tree.java index 0c3654f..9177ffc 100644 --- a/java/111. Minimum Depth of Binary Tree.java +++ b/java/111. Minimum Depth of Binary Tree.java @@ -8,29 +8,29 @@ * } */ public class Solution { - public int minDepth(TreeNode root) { - if (root==null) { - return 0; - } - if(root.left==null&&root.right==null){ - return 1; - } - int left=-1; - if(root.left!=null){ - left=minDepth(root.left)+1; - } - int right=-1; - if(root.right!=null){ - right=minDepth(root.right)+1; - } - if(left==-1){ - return right; - } - if(right==-1){ - return left; - } - - - return Math.min(left, right); - } + public int minDepth(TreeNode root) { + if (root==null) { + return 0; + } + if(root.left==null&&root.right==null){ + return 1; + } + int left=-1; + if(root.left!=null){ + left=minDepth(root.left)+1; + } + int right=-1; + if(root.right!=null){ + right=minDepth(root.right)+1; + } + if(left==-1){ + return right; + } + if(right==-1){ + return left; + } + + + return Math.min(left, right); + } } \ No newline at end of file diff --git a/java/112. Path Sum.java b/java/112. Path Sum.java index edce037..249f4ed 100644 --- a/java/112. Path Sum.java +++ b/java/112. Path Sum.java @@ -9,22 +9,22 @@ */ public class Solution { public boolean hasPathSum(TreeNode root, int sum) { - if (root==null) { - return false; - } - boolean flag1=false; - if (root.val==sum&&root.left==null&&root.right==null) { - return true; - } - if (root.left!=null) { - flag1=hasPathSum(root.left, sum-root.val); - } - if (flag1) { - return true; - } - if (root.right!=null) { - return hasPathSum(root.right, sum-root.val); - } - return false; + if (root==null) { + return false; + } + boolean flag1=false; + if (root.val==sum&&root.left==null&&root.right==null) { + return true; + } + if (root.left!=null) { + flag1=hasPathSum(root.left, sum-root.val); + } + if (flag1) { + return true; + } + if (root.right!=null) { + return hasPathSum(root.right, sum-root.val); + } + return false; } } \ No newline at end of file diff --git a/java/113. Path Sum II.java b/java/113. Path Sum II.java index 7a813b6..4959734 100644 --- a/java/113. Path Sum II.java +++ b/java/113. Path Sum II.java @@ -9,46 +9,46 @@ */ public class Solution { public List> pathSum(TreeNode root, int sum) { - List> answerList=new ArrayList>(); - if (root==null) { - return answerList; - } - List> rootList=new ArrayList>(); - List initList=new ArrayList(); - initList.add(root); - rootList.add(initList); + List> answerList=new ArrayList>(); + if (root==null) { + return answerList; + } + List> rootList=new ArrayList>(); + List initList=new ArrayList(); + initList.add(root); + rootList.add(initList); for (int i = 0; i < rootList.size(); i++) { - List list=rootList.get(i); - TreeNode node=list.get(list.size()-1); - if (node.left!=null||node.right!=null) { - if (node.left!=null) { - List newLeftList=new ArrayList(); - newLeftList.addAll(list); - newLeftList.add(node.left); - rootList.add(newLeftList); - } - if (node.right!=null) { - List newRightList=new ArrayList(); - newRightList.addAll(list); - newRightList.add(node.right); - rootList.add(newRightList); - } - rootList.remove(i); - i--; - } - } + List list=rootList.get(i); + TreeNode node=list.get(list.size()-1); + if (node.left!=null||node.right!=null) { + if (node.left!=null) { + List newLeftList=new ArrayList(); + newLeftList.addAll(list); + newLeftList.add(node.left); + rootList.add(newLeftList); + } + if (node.right!=null) { + List newRightList=new ArrayList(); + newRightList.addAll(list); + newRightList.add(node.right); + rootList.add(newRightList); + } + rootList.remove(i); + i--; + } + } for (int i = 0; i < rootList.size(); i++) { - List list=rootList.get(i); - List valList=new ArrayList(); - int val=0; - for (int j = 0; j < list.size(); j++) { - val+=list.get(j).val; - valList.add(list.get(j).val); - } - if (val==sum) { - answerList.add(valList); - } - } - return answerList; + List list=rootList.get(i); + List valList=new ArrayList(); + int val=0; + for (int j = 0; j < list.size(); j++) { + val+=list.get(j).val; + valList.add(list.get(j).val); + } + if (val==sum) { + answerList.add(valList); + } + } + return answerList; } } \ No newline at end of file diff --git a/java/114. Flatten Binary Tree to Linked List.java b/java/114. Flatten Binary Tree to Linked List.java index 3ff58ea..a50a440 100644 --- a/java/114. Flatten Binary Tree to Linked List.java +++ b/java/114. Flatten Binary Tree to Linked List.java @@ -8,22 +8,22 @@ * } */ public class Solution { - List list=new ArrayList(); + List list=new ArrayList(); public void flatten(TreeNode root) { if (root!=null) { - f(root); - for (int i = 0; i < list.size()-1; i++) { - list.get(i).right=list.get(i+1); - list.get(i).left=null; - } + f(root); + for (int i = 0; i < list.size()-1; i++) { + list.get(i).right=list.get(i+1); + list.get(i).left=null; + } root=list.get(0); - } + } } public void f(TreeNode root){ - if (root!=null) { - list.add(root); - f(root.left); - f(root.right); - } + if (root!=null) { + list.add(root); + f(root.left); + f(root.right); + } } } \ No newline at end of file diff --git a/java/116. Populating Next Right Pointers in Each Node.java b/java/116. Populating Next Right Pointers in Each Node.java index a0cb7af..5cb211e 100644 --- a/java/116. Populating Next Right Pointers in Each Node.java +++ b/java/116. Populating Next Right Pointers in Each Node.java @@ -8,34 +8,34 @@ */ public class Solution { public void connect(TreeLinkNode root) { - if (root!=null) { - List> sumList=new ArrayList>(); - List initList=new ArrayList(); - initList.add(root); - sumList.add(initList); - for (int i = 0; i < sumList.size(); i++) { - List list=sumList.get(i); - List newList=new ArrayList(); - for (int j = 0; j < list.size(); j++) { - if (list.get(i).left!=null) { - newList.add(list.get(j).left); - newList.add(list.get(j).right); - } - } - if (newList.size()!=0) { - sumList.add(newList); - } - } - for (int i = 0; i < sumList.size(); i++) { - List list=sumList.get(i); - for (int j = 0; j < list.size(); j++) { - if (j> sumList=new ArrayList>(); + List initList=new ArrayList(); + initList.add(root); + sumList.add(initList); + for (int i = 0; i < sumList.size(); i++) { + List list=sumList.get(i); + List newList=new ArrayList(); + for (int j = 0; j < list.size(); j++) { + if (list.get(i).left!=null) { + newList.add(list.get(j).left); + newList.add(list.get(j).right); + } + } + if (newList.size()!=0) { + sumList.add(newList); + } + } + for (int i = 0; i < sumList.size(); i++) { + List list=sumList.get(i); + for (int j = 0; j < list.size(); j++) { + if (j> generate(int numRows) { - List> answerList = new ArrayList>(); - for (int i = 0; i < numRows; i++) { - List list = new ArrayList(); - for (int j = 0; j <= i; j++) { - if (j==0||j==i) { - list.add(1); - }else { - list.add(answerList.get(i-1).get(j-1)+answerList.get(i-1).get(j)); - } - } - answerList.add(list); - } + List> answerList = new ArrayList>(); + for (int i = 0; i < numRows; i++) { + List list = new ArrayList(); + for (int j = 0; j <= i; j++) { + if (j==0||j==i) { + list.add(1); + }else { + list.add(answerList.get(i-1).get(j-1)+answerList.get(i-1).get(j)); + } + } + answerList.add(list); + } - return answerList; - } + return answerList; + } } \ No newline at end of file diff --git a/java/119. Pascal's Triangle II.java b/java/119. Pascal's Triangle II.java index f8c3654..5504f01 100644 --- a/java/119. Pascal's Triangle II.java +++ b/java/119. Pascal's Triangle II.java @@ -1,17 +1,17 @@ public class Solution { public List getRow(int rowIndex) { - Map map=new HashMap(); + Map map=new HashMap(); for (int i = 0; i <=rowIndex; i++) { - map.put(i, 1); - //反向计算防止值在使用前被覆盖 - for (int j = i-1; j >=1; j--) { - map.put(j, map.get(j-1)+map.get(j)); - } - } + map.put(i, 1); + //反向计算防止值在使用前被覆盖 + for (int j = i-1; j >=1; j--) { + map.put(j, map.get(j-1)+map.get(j)); + } + } List answerList=new ArrayList(); - for (int i = 0; i <=rowIndex; i++) { - answerList.add(map.get(i)); - } - return answerList; + for (int i = 0; i <=rowIndex; i++) { + answerList.add(map.get(i)); + } + return answerList; } } \ No newline at end of file diff --git a/java/120. Triangle.java b/java/120. Triangle.java index 40df1f1..48ec636 100644 --- a/java/120. Triangle.java +++ b/java/120. Triangle.java @@ -1,17 +1,17 @@ public class Solution { public int minimumTotal(List> triangle) { - int[] ans=new int[triangle.get(triangle.size()-1).size()]; - for (int i = 0; i 1) { - for (int i = triangle.size()-2; i >=0; i--) { - List list=triangle.get(i); - for (int j = 0; j < list.size(); j++) { - ans[j]=list.get(j)+(ans[j]1) { + for (int i = triangle.size()-2; i >=0; i--) { + List list=triangle.get(i); + for (int j = 0; j < list.size(); j++) { + ans[j]=list.get(j)+(ans[j] 0) { - end = i; - } - if (s > sum) { - sum = s; - } + if (prices.length <= 1) { + return 0; + } + int[] change = new int[prices.length - 1]; + for (int i = 0; i < change.length; i++) { + change[i] = prices[i + 1] - prices[i]; + } + int sum = 0; + int s = 0; + int begin = 0; + int end = 0; + for (int i = 0; i < change.length; i++) { + s += change[i]; + if (s < 0) { + s = 0; + begin = i + 1; + end = i + 1; + } + if (change[i] > 0) { + end = i; + } + if (s > sum) { + sum = s; + } - } - return sum; - } + } + return sum; + } } \ No newline at end of file diff --git a/java/122. Best Time to Buy and Sell Stock II.java b/java/122. Best Time to Buy and Sell Stock II.java index 55b8794..fd1e692 100644 --- a/java/122. Best Time to Buy and Sell Stock II.java +++ b/java/122. Best Time to Buy and Sell Stock II.java @@ -1,18 +1,18 @@ public class Solution { public int maxProfit(int[] prices) { - if (prices.length <= 1) { - return 0; - } - int[] change = new int[prices.length - 1]; - for (int i = 0; i < change.length; i++) { - change[i] = prices[i + 1] - prices[i]; - } - int sum = 0; - for (int i = 0; i < change.length; i++) { - if (change[i]>0) { - sum+=change[i]; - } - } - return sum; - } + if (prices.length <= 1) { + return 0; + } + int[] change = new int[prices.length - 1]; + for (int i = 0; i < change.length; i++) { + change[i] = prices[i + 1] - prices[i]; + } + int sum = 0; + for (int i = 0; i < change.length; i++) { + if (change[i]>0) { + sum+=change[i]; + } + } + return sum; + } } \ No newline at end of file diff --git a/java/123. Best Time to Buy and Sell Stock III.java b/java/123. Best Time to Buy and Sell Stock III.java index f56b221..c59add0 100644 --- a/java/123. Best Time to Buy and Sell Stock III.java +++ b/java/123. Best Time to Buy and Sell Stock III.java @@ -1,67 +1,67 @@ public class Solution { public int maxProfit(int[] prices) { - if (prices.length <= 1) { - return 0; - } - int[] change = new int[prices.length - 1]; - for (int i = 0; i < change.length; i++) { - change[i] = prices[i + 1] - prices[i]; - } - int sum0 = 0; - int s0 = 0; - int b=0; - int e=0; - int begin0 = 0; - int end0 = 0; - for (int i = 0; i < change.length; i++) { - s0 += change[i]; - if (s0 < 0) { - s0 = 0; - b=i+1; - e=i+1; - } - if (change[i] > 0) { - e=i; - } - if (s0 > sum0) { - sum0 = s0; - begin0=b; - end0=e; - } - } - int sum1 = 0; - int s1 = 0; - for (int i = 0; i sum1) { - sum1 = s1; - } - } - int sum2 = 0; - int s2 = 0; - for (int i = end0+1; i sum2) { - sum2 = s2; - } - } - int sum3 = 0; - int s3 = 0; - for (int i = begin0+1; i 0) { - s3 = 0; - } - if (s3 < sum3) { - sum3 = s3; - } - } - return sum0+Math.max(0-sum3, Math.max(sum1, sum2)); - } + if (prices.length <= 1) { + return 0; + } + int[] change = new int[prices.length - 1]; + for (int i = 0; i < change.length; i++) { + change[i] = prices[i + 1] - prices[i]; + } + int sum0 = 0; + int s0 = 0; + int b=0; + int e=0; + int begin0 = 0; + int end0 = 0; + for (int i = 0; i < change.length; i++) { + s0 += change[i]; + if (s0 < 0) { + s0 = 0; + b=i+1; + e=i+1; + } + if (change[i] > 0) { + e=i; + } + if (s0 > sum0) { + sum0 = s0; + begin0=b; + end0=e; + } + } + int sum1 = 0; + int s1 = 0; + for (int i = 0; i sum1) { + sum1 = s1; + } + } + int sum2 = 0; + int s2 = 0; + for (int i = end0+1; i sum2) { + sum2 = s2; + } + } + int sum3 = 0; + int s3 = 0; + for (int i = begin0+1; i 0) { + s3 = 0; + } + if (s3 < sum3) { + sum3 = s3; + } + } + return sum0+Math.max(0-sum3, Math.max(sum1, sum2)); + } } \ No newline at end of file diff --git a/java/125. Valid Palindrome.java b/java/125. Valid Palindrome.java index 82d4688..1d91efa 100644 --- a/java/125. Valid Palindrome.java +++ b/java/125. Valid Palindrome.java @@ -1,26 +1,26 @@ public class Solution { public boolean isPalindrome(String s) { if (s.length()<=1) { - return true; - } - char[] c=s.toCharArray(); - char[] nc=new char[c.length]; - int a=0; - for (int i = 0; i < c.length; i++) { - if ((c[i]>96&&c[i]<123)||(c[i]>47&&c[i]<58)) { - nc[a]=c[i]; - a++; - } - if (c[i]>64&&c[i]<91) { - nc[a]=(char) (c[i]+32); - a++; - } - } - for (int i = 0; i < a/2; i++) { - if (nc[i]!=nc[a-i-1]) { - return false; - } - } - return true; + return true; + } + char[] c=s.toCharArray(); + char[] nc=new char[c.length]; + int a=0; + for (int i = 0; i < c.length; i++) { + if ((c[i]>96&&c[i]<123)||(c[i]>47&&c[i]<58)) { + nc[a]=c[i]; + a++; + } + if (c[i]>64&&c[i]<91) { + nc[a]=(char) (c[i]+32); + a++; + } + } + for (int i = 0; i < a/2; i++) { + if (nc[i]!=nc[a-i-1]) { + return false; + } + } + return true; } } \ No newline at end of file diff --git a/java/127. Word Ladder.java b/java/127. Word Ladder.java index 87645c4..f6f0ce8 100644 --- a/java/127. Word Ladder.java +++ b/java/127. Word Ladder.java @@ -1,49 +1,49 @@ public class Solution { public int ladderLength(String beginWord, String endWord, Set wordList) { - if (beginWord==null||endWord==null||wordList==null||beginWord.equals(endWord)||beginWord.length()!=endWord.length()) { - return 0; - } - Map> map=new HashMap>(); - int count=1; - wordList.add(beginWord); - wordList.add(endWord); - for (String str:wordList) { - count++; - List list=new ArrayList(); - char[] cs=str.toCharArray(); - for (int i = 0; i < cs.length; i++) { - char k=cs[i]; - for (char j = 'a'; j <='z'; j++) { - if (j!=k) { - cs[i]=j; - String test=String.valueOf(cs); - if (wordList.contains(test)) { - list.add(test); - } - } - } - cs[i]=k; - } - map.put(str, list); - } - Map test=new HashMap(); - test.put(beginWord, 1); - for (int i = 1; i < count; i++) { - Map newTest=new HashMap(); - for (String str:test.keySet()) { - List list=map.get(str); - for (int j = 0; j < list.size(); j++) { - String k=list.get(j); - if (k.equals(endWord)) { - return i+1; - } - if (test.get(k)==null&&newTest.get(k)==null) { - newTest.put(k, 1); - } - } - } - test=newTest; - } - return 0; + if (beginWord==null||endWord==null||wordList==null||beginWord.equals(endWord)||beginWord.length()!=endWord.length()) { + return 0; + } + Map> map=new HashMap>(); + int count=1; + wordList.add(beginWord); + wordList.add(endWord); + for (String str:wordList) { + count++; + List list=new ArrayList(); + char[] cs=str.toCharArray(); + for (int i = 0; i < cs.length; i++) { + char k=cs[i]; + for (char j = 'a'; j <='z'; j++) { + if (j!=k) { + cs[i]=j; + String test=String.valueOf(cs); + if (wordList.contains(test)) { + list.add(test); + } + } + } + cs[i]=k; + } + map.put(str, list); + } + Map test=new HashMap(); + test.put(beginWord, 1); + for (int i = 1; i < count; i++) { + Map newTest=new HashMap(); + for (String str:test.keySet()) { + List list=map.get(str); + for (int j = 0; j < list.size(); j++) { + String k=list.get(j); + if (k.equals(endWord)) { + return i+1; + } + if (test.get(k)==null&&newTest.get(k)==null) { + newTest.put(k, 1); + } + } + } + test=newTest; + } + return 0; } } \ No newline at end of file diff --git a/java/128. Longest Consecutive Sequence.java b/java/128. Longest Consecutive Sequence.java index 034e0fb..2caed2a 100644 --- a/java/128. Longest Consecutive Sequence.java +++ b/java/128. Longest Consecutive Sequence.java @@ -1,24 +1,24 @@ public class Solution { public int longestConsecutive(int[] nums) { - if (nums.length<2) { - return nums.length; - } - Arrays.sort(nums); - int sum=1; - int length=1; - for (int i = 1; i < nums.length; i++) { - if (nums[i]==nums[i-1]) { - continue; - } - if (nums[i]==nums[i-1]+1) { - sum++; - }else { - if (sum>length) { - length=sum; - } - sum=1; - } - } - return length>sum?length:sum; + if (nums.length<2) { + return nums.length; + } + Arrays.sort(nums); + int sum=1; + int length=1; + for (int i = 1; i < nums.length; i++) { + if (nums[i]==nums[i-1]) { + continue; + } + if (nums[i]==nums[i-1]+1) { + sum++; + }else { + if (sum>length) { + length=sum; + } + sum=1; + } + } + return length>sum?length:sum; } } \ No newline at end of file diff --git a/java/129. Sum Root to Leaf Numbers.java b/java/129. Sum Root to Leaf Numbers.java index 6624f63..d8dcfab 100644 --- a/java/129. Sum Root to Leaf Numbers.java +++ b/java/129. Sum Root to Leaf Numbers.java @@ -9,44 +9,44 @@ */ public class Solution { public int sumNumbers(TreeNode root) { - if (root==null) { - return 0; - } - List> totalList=new ArrayList>(); - List initList=new ArrayList(); - initList.add(root); - totalList.add(initList); - for (int i = 0; i < totalList.size(); i++) { - List list=totalList.get(i); - TreeNode node=list.get(list.size()-1); - if (node.left!=null||node.right!=null) { - if (node.left!=null) { - List newList=new ArrayList(); - newList.addAll(list); - newList.add(node.left); - totalList.add(newList); - } - if (node.right!=null) { - List newList=new ArrayList(); - newList.addAll(list); - newList.add(node.right); - totalList.add(newList); - } - totalList.remove(i); - i--; - } - } - long answer=0; - for (int i = 0; i < totalList.size(); i++) { - List list=totalList.get(i); - long sum=0; - for (int j = 0; j < list.size(); j++) { - sum+=list.get(j).val; - sum*=10; - } - sum/=10; - answer+=sum; - } - return Integer.valueOf(""+answer); + if (root==null) { + return 0; + } + List> totalList=new ArrayList>(); + List initList=new ArrayList(); + initList.add(root); + totalList.add(initList); + for (int i = 0; i < totalList.size(); i++) { + List list=totalList.get(i); + TreeNode node=list.get(list.size()-1); + if (node.left!=null||node.right!=null) { + if (node.left!=null) { + List newList=new ArrayList(); + newList.addAll(list); + newList.add(node.left); + totalList.add(newList); + } + if (node.right!=null) { + List newList=new ArrayList(); + newList.addAll(list); + newList.add(node.right); + totalList.add(newList); + } + totalList.remove(i); + i--; + } + } + long answer=0; + for (int i = 0; i < totalList.size(); i++) { + List list=totalList.get(i); + long sum=0; + for (int j = 0; j < list.size(); j++) { + sum+=list.get(j).val; + sum*=10; + } + sum/=10; + answer+=sum; + } + return Integer.valueOf(""+answer); } } \ No newline at end of file diff --git a/java/131. Palindrome Partitioning.java b/java/131. Palindrome Partitioning.java index 191cc3e..3679115 100644 --- a/java/131. Palindrome Partitioning.java +++ b/java/131. Palindrome Partitioning.java @@ -1,42 +1,42 @@ public class Solution { - Map>> map=new HashMap>>(); + Map>> map=new HashMap>>(); public List> partition(String s) { if (map.get(s)!=null) { - return map.get(s); - } + return map.get(s); + } List> ans=new ArrayList>(); if(isPartition(s)){ - List list=new ArrayList(); - list.add(s); - ans.add(list); + List list=new ArrayList(); + list.add(s); + ans.add(list); } for (int i = 0; i < s.length()-1; i++) { - String k=s.substring(0,i+1); - if (isPartition(k)) { - String t=s.substring(i+1,s.length()); - List> list=partition(t); - for (int j = 0; j < list.size(); j++) { - List l=new ArrayList(); - l.add(k); - l.addAll(list.get(j)); - ans.add(l); - } - } - } - map.put(s, ans); - return ans; + String k=s.substring(0,i+1); + if (isPartition(k)) { + String t=s.substring(i+1,s.length()); + List> list=partition(t); + for (int j = 0; j < list.size(); j++) { + List l=new ArrayList(); + l.add(k); + l.addAll(list.get(j)); + ans.add(l); + } + } + } + map.put(s, ans); + return ans; } public boolean isPartition(String s){ - if(s.length()==1){ - return true; - } - char[] c=s.toCharArray(); - int length=c.length; - for (int i = 0; i < length/2; i++) { - if(c[i]!=c[length-1-i]){ - return false; - } - } - return true; + if(s.length()==1){ + return true; + } + char[] c=s.toCharArray(); + int length=c.length; + for (int i = 0; i < length/2; i++) { + if(c[i]!=c[length-1-i]){ + return false; + } + } + return true; } } \ No newline at end of file diff --git a/java/133. Clone Graph.java b/java/133. Clone Graph.java index a6f1569..d985706 100644 --- a/java/133. Clone Graph.java +++ b/java/133. Clone Graph.java @@ -8,45 +8,45 @@ */ public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { - if (node!=null) { - Map initMap=f(node,new HashMap()); - Map map=new HashMap(); - map.put(node, 0); - List list=new ArrayList(); - list.add(node); - List ans=new ArrayList(); - UndirectedGraphNode init=new UndirectedGraphNode(node.label); - ans.add(init); - int k=1; - for (UndirectedGraphNode ugn : initMap.keySet()) { - if (ugn!=node) { - map.put(ugn, k); - list.add(ugn); - ans.add(new UndirectedGraphNode(ugn.label)); - k++; - } - } - for (int i = 0; i < list.size(); i++) { - List neis=list.get(i).neighbors; - UndirectedGraphNode u=ans.get(i); - for (int j = 0; j < neis.size(); j++) { - u.neighbors.add(ans.get(map.get(neis.get(j)))); - } - } - return init; - } - return null; + if (node!=null) { + Map initMap=f(node,new HashMap()); + Map map=new HashMap(); + map.put(node, 0); + List list=new ArrayList(); + list.add(node); + List ans=new ArrayList(); + UndirectedGraphNode init=new UndirectedGraphNode(node.label); + ans.add(init); + int k=1; + for (UndirectedGraphNode ugn : initMap.keySet()) { + if (ugn!=node) { + map.put(ugn, k); + list.add(ugn); + ans.add(new UndirectedGraphNode(ugn.label)); + k++; + } + } + for (int i = 0; i < list.size(); i++) { + List neis=list.get(i).neighbors; + UndirectedGraphNode u=ans.get(i); + for (int j = 0; j < neis.size(); j++) { + u.neighbors.add(ans.get(map.get(neis.get(j)))); + } + } + return init; + } + return null; } public Map f(UndirectedGraphNode node,Map map){ - if (node!=null&&map.get(node)==null) { - map.put(node, 1); - List list=node.neighbors; - if (list!=null) { - for (int i = 0; i < list.size(); i++) { - map=f(list.get(i),map); - } - } - } - return map; + if (node!=null&&map.get(node)==null) { + map.put(node, 1); + List list=node.neighbors; + if (list!=null) { + for (int i = 0; i < list.size(); i++) { + map=f(list.get(i),map); + } + } + } + return map; } } \ No newline at end of file diff --git a/java/134. Gas Station.java b/java/134. Gas Station.java index 83330fe..0dfdc58 100644 --- a/java/134. Gas Station.java +++ b/java/134. Gas Station.java @@ -1,23 +1,23 @@ public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { - int[] n = new int[gas.length]; - for (int i = 0; i < n.length; i++) { - n[i] = gas[i] - cost[i]; - } - int sum = 0; - int begin = 0; - int truebegin=0; - for (int i = 0; i < n.length*2; i++) { - sum += n[i%n.length]; - while (sum < 0) { - sum -= n[truebegin]; - begin++; - truebegin=begin%n.length; - } - if (i-begin>=n.length) { - return truebegin; - } - } - return -1; - } + int[] n = new int[gas.length]; + for (int i = 0; i < n.length; i++) { + n[i] = gas[i] - cost[i]; + } + int sum = 0; + int begin = 0; + int truebegin=0; + for (int i = 0; i < n.length*2; i++) { + sum += n[i%n.length]; + while (sum < 0) { + sum -= n[truebegin]; + begin++; + truebegin=begin%n.length; + } + if (i-begin>=n.length) { + return truebegin; + } + } + return -1; + } } \ No newline at end of file diff --git a/java/135. Candy.java b/java/135. Candy.java index 01ae341..5e6b7f5 100644 --- a/java/135. Candy.java +++ b/java/135. Candy.java @@ -5,21 +5,21 @@ public int candy(int[] ratings) { candies[0]=1; //ɨ裬Ҳ1ֻ1 for (int i = 1; i < ratings.length; i++) { - if (ratings[i]>ratings[i-1]) { - candies[i]=candies[i-1]+1; - }else { - candies[i]=1; - } - } + if (ratings[i]>ratings[i-1]) { + candies[i]=candies[i-1]+1; + }else { + candies[i]=1; + } + } for (int i = length - 2; i >= 0; i--) { if (ratings[i]>ratings[i+1]&&candies[i]<=candies[i+1]) { - candies[i] = candies[i+1]+1; + candies[i] = candies[i+1]+1; } } int ans=0; for (int i = 0; i wordDict) { - return f(s,wordDict,new HashMap()); + return f(s,wordDict,new HashMap()); } public boolean f(String s, Set wordDict,Map map){ if (s==null||s.length()==0) { - return true; - } - if (map.get(s)!=null) { - return map.get(s); - }else { - for (int i = 0; i < s.length(); i++) { - String str=s.substring(0,i+1); - boolean flag=false; - for (String test : wordDict) { - if (str.equals(test)) { - flag=true; - if (f(s.substring(i+1,s.length()),wordDict,map)) { - return true; - } - } - } - map.put(str, flag); - } - return false; - } + return true; + } + if (map.get(s)!=null) { + return map.get(s); + }else { + for (int i = 0; i < s.length(); i++) { + String str=s.substring(0,i+1); + boolean flag=false; + for (String test : wordDict) { + if (str.equals(test)) { + flag=true; + if (f(s.substring(i+1,s.length()),wordDict,map)) { + return true; + } + } + } + map.put(str, flag); + } + return false; + } } } \ No newline at end of file diff --git a/java/140. Word Break II.java b/java/140. Word Break II.java index 207465a..11c70da 100644 --- a/java/140. Word Break II.java +++ b/java/140. Word Break II.java @@ -1,46 +1,46 @@ public class Solution { - Map> map=new HashMap>(); - Map bmap=new HashMap(); + Map> map=new HashMap>(); + Map bmap=new HashMap(); public List wordBreak(String s, Set wordDict) { - List list=new ArrayList(); - if(s==null||s.length()==0){ - return list; - } - if (map.get(s)!=null) { - return map.get(s); - } - if(bmap.get(s)!=null){ - return null; - } - for (int i = 0; i < s.length()-1; i++) { - String k=s.substring(0,i+1); - if(wordDict.contains(k)){ - String t=s.substring(i+1,s.length()); - if(bmap.get(t)==null){ - List l=map.get(t); - if (l!=null) { - for (int j = 0; j < l.size(); j++) { - list.add(k+" "+l.get(j)); - } - }else { - List a=wordBreak(t,wordDict); - if (a!=null) { - for (int j = 0; j < a.size(); j++) { - list.add(k+" "+a.get(j)); - } - } - } - } - } - } - if(wordDict.contains(s)){ - list.add(s); - } - if (list.size()==0) { - bmap.put(s, false); - return list; - } - map.put(s, list); - return list; + List list=new ArrayList(); + if(s==null||s.length()==0){ + return list; + } + if (map.get(s)!=null) { + return map.get(s); + } + if(bmap.get(s)!=null){ + return null; + } + for (int i = 0; i < s.length()-1; i++) { + String k=s.substring(0,i+1); + if(wordDict.contains(k)){ + String t=s.substring(i+1,s.length()); + if(bmap.get(t)==null){ + List l=map.get(t); + if (l!=null) { + for (int j = 0; j < l.size(); j++) { + list.add(k+" "+l.get(j)); + } + }else { + List a=wordBreak(t,wordDict); + if (a!=null) { + for (int j = 0; j < a.size(); j++) { + list.add(k+" "+a.get(j)); + } + } + } + } + } + } + if(wordDict.contains(s)){ + list.add(s); + } + if (list.size()==0) { + bmap.put(s, false); + return list; + } + map.put(s, list); + return list; } } \ No newline at end of file diff --git a/java/141. Linked List Cycle.java b/java/141. Linked List Cycle.java index a31fa63..7ffa958 100644 --- a/java/141. Linked List Cycle.java +++ b/java/141. Linked List Cycle.java @@ -11,17 +11,17 @@ */ public class Solution { public boolean hasCycle(ListNode head) { - ListNode fast=head; - ListNode slow=head; + ListNode fast=head; + ListNode slow=head; while (true) { - if (fast==null||fast.next==null||slow==null) { - return false; - } - fast=fast.next.next; - slow=slow.next; - if (fast==slow) { - return true; - } - } + if (fast==null||fast.next==null||slow==null) { + return false; + } + fast=fast.next.next; + slow=slow.next; + if (fast==slow) { + return true; + } + } } } \ No newline at end of file diff --git a/java/142. Linked List Cycle II.java b/java/142. Linked List Cycle II.java index 66bc838..82014a7 100644 --- a/java/142. Linked List Cycle II.java +++ b/java/142. Linked List Cycle II.java @@ -11,25 +11,25 @@ */ public class Solution { public ListNode detectCycle(ListNode head) { - ListNode fast=head; - ListNode slow=head; + ListNode fast=head; + ListNode slow=head; while (true) { - if (fast==null||fast.next==null) { - return null; - } - fast=fast.next.next; - slow=slow.next; - if (fast==slow) { - ListNode again=head; - while (true) { - if (slow==again) { - return again; - }else { - slow=slow.next; - again=again.next; - } - } - } - } + if (fast==null||fast.next==null) { + return null; + } + fast=fast.next.next; + slow=slow.next; + if (fast==slow) { + ListNode again=head; + while (true) { + if (slow==again) { + return again; + }else { + slow=slow.next; + again=again.next; + } + } + } + } } } \ No newline at end of file diff --git a/java/143. Reorder List.java b/java/143. Reorder List.java index 73f90bf..41c55ae 100644 --- a/java/143. Reorder List.java +++ b/java/143. Reorder List.java @@ -8,21 +8,21 @@ */ public class Solution { public void reorderList(ListNode head) { - if (head!=null) { - List list = zuhe(head, new ArrayList()); - int l=list.size(); - for (int i = 0; i < l/2; i++) { - list.get(i).next = list.get(l - 1 - i); - list.get(l - 1 - i).next = list.get(i + 1); - } - list.get(l/2).next = null; - } - } - public List zuhe(ListNode head, List list) { - while (head!=null) { - list.add(head); - head=head.next; - } - return list; - } + if (head!=null) { + List list = zuhe(head, new ArrayList()); + int l=list.size(); + for (int i = 0; i < l/2; i++) { + list.get(i).next = list.get(l - 1 - i); + list.get(l - 1 - i).next = list.get(i + 1); + } + list.get(l/2).next = null; + } + } + public List zuhe(ListNode head, List list) { + while (head!=null) { + list.add(head); + head=head.next; + } + return list; + } } \ No newline at end of file diff --git a/java/144. Binary Tree Preorder Traversal.java b/java/144. Binary Tree Preorder Traversal.java index bcd91e4..f9d6a11 100644 --- a/java/144. Binary Tree Preorder Traversal.java +++ b/java/144. Binary Tree Preorder Traversal.java @@ -9,16 +9,16 @@ */ public class Solution { public List preorderTraversal(TreeNode root) { - List list=new ArrayList(); + List list=new ArrayList(); return preorder(root,list); } public List preorder(TreeNode root,List list){ - if (root==null) { - return list; - } - list.add(root.val); - list=preorder(root.left,list); - list=preorder(root.right, list); - return list; + if (root==null) { + return list; + } + list.add(root.val); + list=preorder(root.left,list); + list=preorder(root.right, list); + return list; } } \ No newline at end of file diff --git a/java/145. Binary Tree Postorder Traversal.java b/java/145. Binary Tree Postorder Traversal.java index 316f6ce..5d26b5d 100644 --- a/java/145. Binary Tree Postorder Traversal.java +++ b/java/145. Binary Tree Postorder Traversal.java @@ -9,16 +9,16 @@ */ public class Solution { public List postorderTraversal(TreeNode root) { - List list=new ArrayList(); + List list=new ArrayList(); return postorder(root,list); } public List postorder(TreeNode root,List list){ - if (root==null) { - return list; - } - list=postorder(root.left,list); - list=postorder(root.right, list); - list.add(root.val); - return list; + if (root==null) { + return list; + } + list=postorder(root.left,list); + list=postorder(root.right, list); + list.add(root.val); + return list; } } \ No newline at end of file diff --git a/java/147. Insertion Sort List.java b/java/147. Insertion Sort List.java index ef396b1..21443d2 100644 --- a/java/147. Insertion Sort List.java +++ b/java/147. Insertion Sort List.java @@ -8,29 +8,29 @@ */ public class Solution { public ListNode insertionSortList(ListNode head) { - if (head==null||head.next==null) { - return head; - } + if (head==null||head.next==null) { + return head; + } ListNode node=head.next; ListNode ans=new ListNode(head.val); while (node!=null) { - if (node.val<=ans.val) { - ListNode newAns=new ListNode(node.val); - newAns.next=ans; - ans=newAns; - }else { - ListNode test=ans; - ListNode a=null; - while (test!=null&&test.valanswer) { - answer=sum; - } - }else { - int sum=0; - for (int k = 0; k < points.length; k++) { - if ((points[k].x==points[j].x)&&(points[k].y==points[j].y)) { - sum++; - } - } - if (sum>answer) { - answer=sum; - } - } - } - } - return answer; + for (int i = 0; i < points.length; i++) { + for (int j = i+1; j < points.length; j++) { + if((points[i].x!=points[j].x)||(points[i].y!=points[j].y)){ + int sum=0; + for (int k = 0; k < points.length; k++) { + if (isStraight(points[i],points[j],points[k])) { + sum++; + } + } + if (sum>answer) { + answer=sum; + } + }else { + int sum=0; + for (int k = 0; k < points.length; k++) { + if ((points[k].x==points[j].x)&&(points[k].y==points[j].y)) { + sum++; + } + } + if (sum>answer) { + answer=sum; + } + } + } + } + return answer; } public boolean isStraight (Point a,Point b,Point c){ - return ((b.y-c.y)*(a.x-c.x))==((a.y-c.y)*(b.x-c.x)); + return ((b.y-c.y)*(a.x-c.x))==((a.y-c.y)*(b.x-c.x)); } } \ No newline at end of file diff --git a/java/150. Evaluate Reverse Polish Notation.java b/java/150. Evaluate Reverse Polish Notation.java index 8a4e316..5ce6496 100644 --- a/java/150. Evaluate Reverse Polish Notation.java +++ b/java/150. Evaluate Reverse Polish Notation.java @@ -1,39 +1,39 @@ public class Solution { public int evalRPN(String[] tokens) { - List list = new ArrayList(); - for (int i = 0; i < tokens.length; i++) { - if (tokens[i].equals("+")) { - int b = list.get(list.size() - 1); - list.remove(list.size() - 1); - int a = list.get(list.size() - 1); - list.remove(list.size() - 1); - int sum = a + b; - list.add(sum); - } else if (tokens[i].equals("-")) { - int b = list.get(list.size() - 1); - list.remove(list.size() - 1); - int a = list.get(list.size() - 1); - list.remove(list.size() - 1); - int sum = a - b; - list.add(sum); - } else if (tokens[i].equals("*")) { - int b = list.get(list.size() - 1); - list.remove(list.size() - 1); - int a = list.get(list.size() - 1); - list.remove(list.size() - 1); - int sum = a * b; - list.add(sum); - } else if (tokens[i].equals("/")) { - int b = list.get(list.size() - 1); - list.remove(list.size() - 1); - int a = list.get(list.size() - 1); - list.remove(list.size() - 1); - int sum = a / b; - list.add(sum); - } else { - list.add(Integer.parseInt(tokens[i])); - } - } - return list.get(0); - } + List list = new ArrayList(); + for (int i = 0; i < tokens.length; i++) { + if (tokens[i].equals("+")) { + int b = list.get(list.size() - 1); + list.remove(list.size() - 1); + int a = list.get(list.size() - 1); + list.remove(list.size() - 1); + int sum = a + b; + list.add(sum); + } else if (tokens[i].equals("-")) { + int b = list.get(list.size() - 1); + list.remove(list.size() - 1); + int a = list.get(list.size() - 1); + list.remove(list.size() - 1); + int sum = a - b; + list.add(sum); + } else if (tokens[i].equals("*")) { + int b = list.get(list.size() - 1); + list.remove(list.size() - 1); + int a = list.get(list.size() - 1); + list.remove(list.size() - 1); + int sum = a * b; + list.add(sum); + } else if (tokens[i].equals("/")) { + int b = list.get(list.size() - 1); + list.remove(list.size() - 1); + int a = list.get(list.size() - 1); + list.remove(list.size() - 1); + int sum = a / b; + list.add(sum); + } else { + list.add(Integer.parseInt(tokens[i])); + } + } + return list.get(0); + } } \ No newline at end of file diff --git a/java/151. Reverse Words in a String.java b/java/151. Reverse Words in a String.java index 9cb9f17..1a80890 100644 --- a/java/151. Reverse Words in a String.java +++ b/java/151. Reverse Words in a String.java @@ -1,42 +1,42 @@ public class Solution { public String reverseWords(String s) { - if (s.length()<1) { - return s; - } - String a = ""; - String answer = ""; - boolean flag=true; - for (int i = s.length() - 1; i >= 0; i--) { - String k = s.substring(i, i + 1); - if (flag) { - if (k.equals(" ")) { - - }else { - flag=false; - a=k+a; - } - }else { - if (k.equals(" ")) { - flag=true; - answer=answer+" "+a; - a=""; - }else { - a=k+a; - } - } - } - if (a.length()!=0) { - answer=answer+" "+a; - } - if (answer.length()==0) { - return ""; - } - while (answer.substring(0,1).equals(" ")) { - if (answer.length()==1) { - return ""; - } - answer=answer.substring(1, answer.length()); - } - return answer; - } + if (s.length()<1) { + return s; + } + String a = ""; + String answer = ""; + boolean flag=true; + for (int i = s.length() - 1; i >= 0; i--) { + String k = s.substring(i, i + 1); + if (flag) { + if (k.equals(" ")) { + + }else { + flag=false; + a=k+a; + } + }else { + if (k.equals(" ")) { + flag=true; + answer=answer+" "+a; + a=""; + }else { + a=k+a; + } + } + } + if (a.length()!=0) { + answer=answer+" "+a; + } + if (answer.length()==0) { + return ""; + } + while (answer.substring(0,1).equals(" ")) { + if (answer.length()==1) { + return ""; + } + answer=answer.substring(1, answer.length()); + } + return answer; + } } diff --git a/java/152. Maximum Product Subarray.java b/java/152. Maximum Product Subarray.java index 455108e..caa9601 100644 --- a/java/152. Maximum Product Subarray.java +++ b/java/152. Maximum Product Subarray.java @@ -4,70 +4,70 @@ public int maxProduct(int[] nums) { List list=new ArrayList(); boolean flag=false; for (int i = 0; i < nums.length; i++) { - if (nums[i]!=0) { - list.add(nums[i]); - }else { - flag=true; - if (list.size()!=0) { - totalList.add(list); - } - list=new ArrayList(); - } - } + if (nums[i]!=0) { + list.add(nums[i]); + }else { + flag=true; + if (list.size()!=0) { + totalList.add(list); + } + list=new ArrayList(); + } + } if (list.size()!=0) { - totalList.add(list); - } + totalList.add(list); + } if (totalList.size()==0) { - return 0; - } + return 0; + } int ans=Integer.MIN_VALUE; - for (int i = 0; i < totalList.size(); i++) { - int f=max(totalList.get(i)); - if (f>ans) { - ans=f; - } - } - if (flag&&ans<0) { - return 0; - } - return ans; + for (int i = 0; i < totalList.size(); i++) { + int f=max(totalList.get(i)); + if (f>ans) { + ans=f; + } + } + if (flag&&ans<0) { + return 0; + } + return ans; } public int max(List list){ - if (list.size()==1) { - return list.get(0); - } - int s=-1; - int e=-1; - int sum=0; - for (int i = 0; i =e; i--) { - right*=list.get(i); - } - return left>right?(ans/left):(ans/right); + if (list.size()==1) { + return list.get(0); + } + int s=-1; + int e=-1; + int sum=0; + for (int i = 0; i =e; i--) { + right*=list.get(i); + } + return left>right?(ans/left):(ans/right); } } \ No newline at end of file diff --git a/java/153. Find Minimum in Rotated Sorted Array.java b/java/153. Find Minimum in Rotated Sorted Array.java index bd04554..8ce0764 100644 --- a/java/153. Find Minimum in Rotated Sorted Array.java +++ b/java/153. Find Minimum in Rotated Sorted Array.java @@ -4,14 +4,14 @@ public int findMin(int[] nums) { int end=nums.length-1; int half=(begin+end)/2; while (end-begin>1) { - if (nums[half]>nums[begin]&&nums[half]>nums[end]) { - begin=half; - half=(begin+end)/2; - }else { - end=half; - half=(begin+end)/2; - } - } - return nums[begin]>nums[end]?nums[end]:nums[begin]; + if (nums[half]>nums[begin]&&nums[half]>nums[end]) { + begin=half; + half=(begin+end)/2; + }else { + end=half; + half=(begin+end)/2; + } + } + return nums[begin]>nums[end]?nums[end]:nums[begin]; } } \ No newline at end of file diff --git a/java/154. Find Minimum in Rotated Sorted Array II.java b/java/154. Find Minimum in Rotated Sorted Array II.java index 0102d52..da52486 100644 --- a/java/154. Find Minimum in Rotated Sorted Array II.java +++ b/java/154. Find Minimum in Rotated Sorted Array II.java @@ -1,11 +1,11 @@ public class Solution { public int findMin(int[] nums) { - int a=nums[0]; - for (int i = 1; i < nums.length; i++) { - if (nums[i]b) { - return compare(headA.next, a-1, headB, b); - }else if (ab) { + return compare(headA.next, a-1, headB, b); + }else if (a1) { - if (nums[m]>nums[m+1]) { - e=m; - }else { - s=m; - } - m=(s+e)/2; - } + if (nums[m]>nums[m+1]) { + e=m; + }else { + s=m; + } + m=(s+e)/2; + } return nums[s]>nums[e]?s:e; } } \ No newline at end of file diff --git a/java/164. Maximum Gap.java b/java/164. Maximum Gap.java index e0b6c78..80faca5 100644 --- a/java/164. Maximum Gap.java +++ b/java/164. Maximum Gap.java @@ -1,15 +1,15 @@ public class Solution { public int maximumGap(int[] nums) { - if (nums.length<2) { - return 0; - } + if (nums.length<2) { + return 0; + } Arrays.sort(nums); int ans=0; for (int i = 1; i < nums.length; i++) { - if (ans<(nums[i]-nums[i-1])) { - ans=nums[i]-nums[i-1]; - } - } + if (ans<(nums[i]-nums[i-1])) { + ans=nums[i]-nums[i-1]; + } + } return ans; } } \ No newline at end of file diff --git a/java/165. Compare Version Numbers.java b/java/165. Compare Version Numbers.java index bca8bec..fffa6ae 100644 --- a/java/165. Compare Version Numbers.java +++ b/java/165. Compare Version Numbers.java @@ -5,28 +5,28 @@ public int compareVersion(String version1, String version2) { int l1=v1.length; int l2=v2.length; for (int i = 0; i < Math.min(l1, l2); i++) { - if (Integer.valueOf(v1[i])Integer.valueOf(v2[i])) { - return 1; - } - } + if (Integer.valueOf(v1[i])Integer.valueOf(v2[i])) { + return 1; + } + } if (l1==l2) { - return 0; - }else if (l1>l2) { - for (int i = l2; i l2) { + for (int i = l2; i map=new HashMap(); StringBuffer sb=new StringBuffer(); sb.append(String.valueOf(n/d)).append("."); @@ -27,21 +27,21 @@ public String fractionToDecimal(int numerator, int denominator) { int i=1; map.put(n, i); while (true) { - n*=10; - sb.append(String.valueOf(n/d)); - n%=d; - if (n==0) { - return flag?sb.toString():"-"+sb.toString(); - } - i++; - if (map.get(n)==null) { - map.put(n, i); - }else { - String str=sb.toString(); - String circle=str.substring(str.length()-(i-Integer.valueOf(map.get(n))), str.length()); - String answer=str.substring(0,str.length()-(i-Integer.valueOf(map.get(n))))+"("+circle+")"; - return flag?answer:"-"+answer; - } - } + n*=10; + sb.append(String.valueOf(n/d)); + n%=d; + if (n==0) { + return flag?sb.toString():"-"+sb.toString(); + } + i++; + if (map.get(n)==null) { + map.put(n, i); + }else { + String str=sb.toString(); + String circle=str.substring(str.length()-(i-Integer.valueOf(map.get(n))), str.length()); + String answer=str.substring(0,str.length()-(i-Integer.valueOf(map.get(n))))+"("+circle+")"; + return flag?answer:"-"+answer; + } + } } } \ No newline at end of file diff --git a/java/168. Excel Sheet Column Title.java b/java/168. Excel Sheet Column Title.java index b9c1051..051c841 100644 --- a/java/168. Excel Sheet Column Title.java +++ b/java/168. Excel Sheet Column Title.java @@ -1,48 +1,48 @@ public class Solution { public String convertToTitle(int n) { - List list=new ArrayList(); + List list=new ArrayList(); while (n!=0) { - list.add(n%26); - n/=26; - } + list.add(n%26); + n/=26; + } boolean flag=false; List l=new ArrayList(); for (int i = 0; i 1) { - l.add(k-1); - flag=false; - }else if(k==1){ - l.add(26); - flag=true; - }else { - l.add(25); - flag=true; - } - }else { - if (k!=0) { - l.add(k); - flag=false; - } - else { - l.add(26); - flag=true; - } - } - } + int k=list.get(i); + if(flag){ + if (k>1) { + l.add(k-1); + flag=false; + }else if(k==1){ + l.add(26); + flag=true; + }else { + l.add(25); + flag=true; + } + }else { + if (k!=0) { + l.add(k); + flag=false; + } + else { + l.add(26); + flag=true; + } + } + } int m=l.size(); if (flag) { - m--; - } + m--; + } String answer=""; for (int i = m-1; i >=0; i--) { - int k=l.get(i); - k+=64; - char c=(char) k; - String s=String.valueOf(c); - answer+=s; - } - return answer; + int k=l.get(i); + k+=64; + char c=(char) k; + String s=String.valueOf(c); + answer+=s; + } + return answer; } } \ No newline at end of file diff --git a/java/169. Majority Element.java b/java/169. Majority Element.java index edb843d..fe8e6da 100644 --- a/java/169. Majority Element.java +++ b/java/169. Majority Element.java @@ -1,6 +1,6 @@ public class Solution { public int majorityElement(int[] nums) { - Arrays.sort(nums); - return nums[nums.length/2]; + Arrays.sort(nums); + return nums[nums.length/2]; } } \ No newline at end of file diff --git a/java/171. Excel Sheet Column Number.java b/java/171. Excel Sheet Column Number.java index fcb3e16..f23c3e0 100644 --- a/java/171. Excel Sheet Column Number.java +++ b/java/171. Excel Sheet Column Number.java @@ -1,32 +1,32 @@ public class Solution { public int titleToNumber(String s) { - int[] a=new int[s.length()]; - boolean flag=false; + int[] a=new int[s.length()]; + boolean flag=false; for (int i = s.length()-1; i>=0; i--) { - String k=s.substring(i,i+1); - int ik= (int)(k.toCharArray()[0])-64; - if (flag) { - ik++; - } - if (ik!=26) { - a[s.length()-1-i]=ik; - flag=false; - }else { - a[s.length()-1-i]=0; - flag=true; - } - } + String k=s.substring(i,i+1); + int ik= (int)(k.toCharArray()[0])-64; + if (flag) { + ik++; + } + if (ik!=26) { + a[s.length()-1-i]=ik; + flag=false; + }else { + a[s.length()-1-i]=0; + flag=true; + } + } int p=1; int sum=0; for (int i = 0; i < a.length; i++) { - if (i!=0) { - p*=26; - } - sum+=a[i]*p; - } - if (flag) { - return sum+=p*26; - } - return sum; + if (i!=0) { + p*=26; + } + sum+=a[i]*p; + } + if (flag) { + return sum+=p*26; + } + return sum; } } \ No newline at end of file diff --git a/java/172. Factorial Trailing Zeroes.java b/java/172. Factorial Trailing Zeroes.java index ad0e0e5..131dd7d 100644 --- a/java/172. Factorial Trailing Zeroes.java +++ b/java/172. Factorial Trailing Zeroes.java @@ -1,10 +1,10 @@ public class Solution { public int trailingZeroes(int n) { - int sum=0; - while (n!=0) { - n/=5; - sum+=n; - } - return sum; + int sum=0; + while (n!=0) { + n/=5; + sum+=n; + } + return sum; } } \ No newline at end of file diff --git a/java/179. Largest Number.java b/java/179. Largest Number.java index a8ae40b..cf7993c 100644 --- a/java/179. Largest Number.java +++ b/java/179. Largest Number.java @@ -1,39 +1,39 @@ public class Solution { - public String largestNumber(int[] nums) { - if (nums.length == 1) { - return String.valueOf(nums[0]); - } - int temp = 0; - for (int i = nums.length - 1; i > 0; i--) { - for (int j = 0; j < i; ++j) { - if (new Solution().compare(nums[j], nums[j + 1])) { - temp = nums[j]; - nums[j] = nums[j + 1]; - nums[j + 1] = temp; - } - } - } - StringBuffer sb = new StringBuffer(); - if (nums[nums.length - 1] == 0) { - return "0"; - } - for (int i = nums.length - 1; i >= 0; i--) { - sb.append(String.valueOf(nums[i])); - } - return sb.toString(); - } + public String largestNumber(int[] nums) { + if (nums.length == 1) { + return String.valueOf(nums[0]); + } + int temp = 0; + for (int i = nums.length - 1; i > 0; i--) { + for (int j = 0; j < i; ++j) { + if (new Solution().compare(nums[j], nums[j + 1])) { + temp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = temp; + } + } + } + StringBuffer sb = new StringBuffer(); + if (nums[nums.length - 1] == 0) { + return "0"; + } + for (int i = nums.length - 1; i >= 0; i--) { + sb.append(String.valueOf(nums[i])); + } + return sb.toString(); + } - public boolean compare(int a, int b) { - char[] ca=(String.valueOf(a)+String.valueOf(b)).toCharArray(); - char[] cb=(String.valueOf(b)+String.valueOf(a)).toCharArray(); - for (int i = 0; i < ca.length; i++) { - if (ca[i]>cb[i]) { - return true; - } - if (ca[i]cb[i]) { + return true; + } + if (ca[i] findRepeatedDnaSequences(String s) { Map trash=new HashMap(); List ans=new ArrayList(); for (int i = 0; i < s.length()-9; i++) { - String k=s.substring(i,i+10); - if (map.get(k)==null) { - map.put(k, 1); - }else if(trash.get(k)==null){ - trash.put(k, 1); - ans.add(k); - } - } - return ans; + String k=s.substring(i,i+10); + if (map.get(k)==null) { + map.put(k, 1); + }else if(trash.get(k)==null){ + trash.put(k, 1); + ans.add(k); + } + } + return ans; } } \ No newline at end of file diff --git a/java/189. Rotate Array.java b/java/189. Rotate Array.java index 200ee84..e3f976a 100644 --- a/java/189. Rotate Array.java +++ b/java/189. Rotate Array.java @@ -1,15 +1,15 @@ public class Solution { public void rotate(int[] nums, int k) { - k=k%nums.length; + k=k%nums.length; List list=new ArrayList(); for (int i = nums.length-k; i < nums.length; i++) { - list.add(nums[i]); - } + list.add(nums[i]); + } for (int i = nums.length-1; i >=k; i--) { - nums[i]=nums[i-k]; - } + nums[i]=nums[i-k]; + } for (int i = 0; i < k; i++) { - nums[i]=list.get(i); - } + nums[i]=list.get(i); + } } } \ No newline at end of file diff --git a/java/191. Number of 1 Bits.java b/java/191. Number of 1 Bits.java index 0f79941..1825ea5 100644 --- a/java/191. Number of 1 Bits.java +++ b/java/191. Number of 1 Bits.java @@ -3,9 +3,9 @@ public class Solution { public int hammingWeight(int n) { int ans=0; while (n!=0) { - ans+=n&1; - n>>>=1; - } + ans+=n&1; + n>>>=1; + } return ans; } } \ No newline at end of file diff --git a/java/198. House Robber.java b/java/198. House Robber.java index bf3653b..c91212d 100644 --- a/java/198. House Robber.java +++ b/java/198. House Robber.java @@ -1,15 +1,15 @@ public class Solution { public int rob(int[] nums) { int n=nums.length; - if (n==0) { - return 0; - } + if (n==0) { + return 0; + } int[] ans=new int[n+1]; ans[0]=0; ans[1]=nums[0]; for (int i = 2; i < n+1; i++) { - ans[i]=Math.max(nums[i-1]+ans[i-2], ans[i-1]); - } - return ans[n]; + ans[i]=Math.max(nums[i-1]+ans[i-2], ans[i-1]); + } + return ans[n]; } } \ No newline at end of file diff --git a/java/199. Binary Tree Right Side View.java b/java/199. Binary Tree Right Side View.java index aa294be..0457586 100644 --- a/java/199. Binary Tree Right Side View.java +++ b/java/199. Binary Tree Right Side View.java @@ -9,28 +9,28 @@ */ public class Solution { public List rightSideView(TreeNode root) { - List answer=new ArrayList(); - if (root==null) { - return answer; - } - List list=new ArrayList(); - list.add(root); - int k=list.size(); - while (k>0) { - answer.add(list.get(list.size()-1).val); - for (int i = 0; i < k; i++) { - if (list.get(i).left!=null) { - list.add(list.get(i).left); - } - if (list.get(i).right!=null) { - list.add(list.get(i).right); - } - list.remove(i); - i--; - k--; - } - k=list.size(); - } - return answer; + List answer=new ArrayList(); + if (root==null) { + return answer; + } + List list=new ArrayList(); + list.add(root); + int k=list.size(); + while (k>0) { + answer.add(list.get(list.size()-1).val); + for (int i = 0; i < k; i++) { + if (list.get(i).left!=null) { + list.add(list.get(i).left); + } + if (list.get(i).right!=null) { + list.add(list.get(i).right); + } + list.remove(i); + i--; + k--; + } + k=list.size(); + } + return answer; } } \ No newline at end of file diff --git a/java/200. Number of Islands.java b/java/200. Number of Islands.java index b543fdc..14dd7c9 100644 --- a/java/200. Number of Islands.java +++ b/java/200. Number of Islands.java @@ -1,41 +1,41 @@ public class Solution { public int numIslands(char[][] grid) { - int il = 0; - Map map = new HashMap(); - for (int i = 0; i < grid.length; i++) { - for (int j = 0; j < grid[i].length; j++) { - if (map.get("(" + i + "," + j + ")") == null) { - if (grid[i][j] == '1') { - il++; - map = new Solution().ss(map, i, j, grid); - } - } - } - } - return il; + int il = 0; + Map map = new HashMap(); + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[i].length; j++) { + if (map.get("(" + i + "," + j + ")") == null) { + if (grid[i][j] == '1') { + il++; + map = new Solution().ss(map, i, j, grid); + } + } + } + } + return il; } public Map ss(Map map,int i,int j,char[][] grid){ - if (map.get("(" + i + "," + j + ")") == null && grid[i][j] == '1') { - int LI = grid.length; - int LJ = grid[i].length; - map.put("(" + i + "," + j + ")", "1"); - if (i > 0) { - // 上边检测 - map = ss(map, i - 1, j, grid); - } - if (i < LI - 1) { - // 下边检测 - map = ss(map, i + 1, j, grid); - } - if (j > 0) { - // 左边检测 - map = ss(map, i, j - 1, grid); - } - if (j < LJ - 1) { - // 右边检测 - map = ss(map, i, j + 1, grid); - } - } - return map; - } + if (map.get("(" + i + "," + j + ")") == null && grid[i][j] == '1') { + int LI = grid.length; + int LJ = grid[i].length; + map.put("(" + i + "," + j + ")", "1"); + if (i > 0) { + // 上边检测 + map = ss(map, i - 1, j, grid); + } + if (i < LI - 1) { + // 下边检测 + map = ss(map, i + 1, j, grid); + } + if (j > 0) { + // 左边检测 + map = ss(map, i, j - 1, grid); + } + if (j < LJ - 1) { + // 右边检测 + map = ss(map, i, j + 1, grid); + } + } + return map; + } } \ No newline at end of file diff --git a/java/201. Bitwise AND of Numbers Range.java b/java/201. Bitwise AND of Numbers Range.java index 3122357..0d98873 100644 --- a/java/201. Bitwise AND of Numbers Range.java +++ b/java/201. Bitwise AND of Numbers Range.java @@ -1,7 +1,7 @@ public class Solution { public int rangeBitwiseAnd(int m, int n) { if(m==n){ - return n; + return n; } int s=(int) Math.pow(2,(int) (Math.log(m^n)/Math.log(2))+1); return ((m&n)/s)*s; diff --git a/java/202. Happy Number.java b/java/202. Happy Number.java index fa01f58..8147634 100644 --- a/java/202. Happy Number.java +++ b/java/202. Happy Number.java @@ -5,21 +5,21 @@ public boolean isHappy(int n) { map.put(s, "存在"); int sum=0; while (true) { - for (int i = 0; i < s.length(); i++) { - int k=Integer.parseInt(s.substring(i,i+1)); - sum+=k*k; - } - if (sum==1) { - return true; - } - if (map.get(String.valueOf(sum))!=null) { - return false; - }else { - s=String.valueOf(sum); - map.put(s, "存在"); - sum=0; - } - - } + for (int i = 0; i < s.length(); i++) { + int k=Integer.parseInt(s.substring(i,i+1)); + sum+=k*k; + } + if (sum==1) { + return true; + } + if (map.get(String.valueOf(sum))!=null) { + return false; + }else { + s=String.valueOf(sum); + map.put(s, "存在"); + sum=0; + } + + } } } \ No newline at end of file diff --git a/java/203. Remove Linked List Elements.java b/java/203. Remove Linked List Elements.java index 83d0862..532bd5b 100644 --- a/java/203. Remove Linked List Elements.java +++ b/java/203. Remove Linked List Elements.java @@ -8,24 +8,24 @@ */ public class Solution { public ListNode removeElements(ListNode head,int val) { - List list=zuhe(head, new ArrayList(),val); - if (list.size()==0) { - return null; - } - for (int i = 0; i < list.size()-1; i++) { - list.get(i).next=list.get(i+1); - } - list.get(list.size()-1).next=null; - return list.get(0); + List list=zuhe(head, new ArrayList(),val); + if (list.size()==0) { + return null; + } + for (int i = 0; i < list.size()-1; i++) { + list.get(i).next=list.get(i+1); + } + list.get(list.size()-1).next=null; + return list.get(0); } public List zuhe(ListNode head,List list, int val){ - if (head!=null) { - if (head.val!=val) { - list.add(head); - } - list=zuhe(head.next,list,val); - } - return list; + if (head!=null) { + if (head.val!=val) { + list.add(head); + } + list=zuhe(head.next,list,val); + } + return list; } } \ No newline at end of file diff --git a/java/204. Count Primes.java b/java/204. Count Primes.java index b57e72c..845f46d 100644 --- a/java/204. Count Primes.java +++ b/java/204. Count Primes.java @@ -1,15 +1,15 @@ public class Solution { public int countPrimes(int n) { - boolean[] flag=new boolean[n]; - int ans=0; - for (int i = 2; i < flag.length; i++) { - if (!flag[i]) { - ans++; - for (int j = i; j < flag.length; j+=i) { - flag[j]=true; - } - } - } - return ans; + boolean[] flag=new boolean[n]; + int ans=0; + for (int i = 2; i < flag.length; i++) { + if (!flag[i]) { + ans++; + for (int j = i; j < flag.length; j+=i) { + flag[j]=true; + } + } + } + return ans; } } \ No newline at end of file diff --git a/java/205. Isomorphic Strings.java b/java/205. Isomorphic Strings.java index a913a60..958ce24 100644 --- a/java/205. Isomorphic Strings.java +++ b/java/205. Isomorphic Strings.java @@ -1,25 +1,25 @@ public class Solution { public boolean isIsomorphic(String s, String t) { if (s.length()!=t.length()) { - return false; - } + return false; + } Map sMap=new HashMap(); Map tMap=new HashMap(); for (int i = 0; i < s.length(); i++) { - String sk=s.substring(i,i+1); - String tk=t.substring(i,i+1); - if ((sMap.get(sk)==null)!=((tMap.get(tk)==null))) { - return false; - }else if (sMap.get(sk)==null) { - sMap.put(sk, i); - tMap.put(tk, i); - }else if(!sMap.get(sk).equals(tMap.get(tk))){ - return false; - }else { - sMap.put(sk, i); - tMap.put(tk, i); - } - } + String sk=s.substring(i,i+1); + String tk=t.substring(i,i+1); + if ((sMap.get(sk)==null)!=((tMap.get(tk)==null))) { + return false; + }else if (sMap.get(sk)==null) { + sMap.put(sk, i); + tMap.put(tk, i); + }else if(!sMap.get(sk).equals(tMap.get(tk))){ + return false; + }else { + sMap.put(sk, i); + tMap.put(tk, i); + } + } return true; } } \ No newline at end of file diff --git a/java/206. Reverse Linked List.java b/java/206. Reverse Linked List.java index bded3af..b102816 100644 --- a/java/206. Reverse Linked List.java +++ b/java/206. Reverse Linked List.java @@ -9,21 +9,21 @@ public class Solution { public ListNode reverseList(ListNode head) { if (head==null) { - return head; - } - List list=zuhe(head, new ArrayList()); + return head; + } + List list=zuhe(head, new ArrayList()); for (int i = list.size()-1; i >0; i--) { - list.get(i).next=list.get(i-1); - } + list.get(i).next=list.get(i-1); + } list.get(0).next=null; return list.get(list.size()-1); } public List zuhe(ListNode head,List list){ - if (head!=null) { - list.add(head); - list=zuhe(head.next,list); - } - return list; + if (head!=null) { + list.add(head); + list=zuhe(head.next,list); + } + return list; } } \ No newline at end of file diff --git a/java/208. Implement Trie (Prefix Tree).java b/java/208. Implement Trie (Prefix Tree).java index a60cffb..a47caa3 100644 --- a/java/208. Implement Trie (Prefix Tree).java +++ b/java/208. Implement Trie (Prefix Tree).java @@ -18,7 +18,7 @@ public Trie() { // Inserts a word into the trie. public void insert(String word) { if(!search(word)){ - TrieNode tt=root; + TrieNode tt=root; char[] ac=word.toCharArray(); for(int i=0;i sons=root.sons; @@ -42,7 +42,7 @@ public void insert(String word) { // Returns if the word is in the trie. public boolean search(String word) { - TrieNode tt=root; + TrieNode tt=root; char[] ac=word.toCharArray(); for(int i=0;i sons=root.sons; @@ -60,7 +60,7 @@ public boolean search(String word) { // Returns if there is any word in the trie // that starts with the given prefix. public boolean startsWith(String prefix) { - TrieNode tt=root; + TrieNode tt=root; char[] ac=prefix.toCharArray(); for(int i=0;i sons=root.sons; diff --git a/java/212. Word Search II.java b/java/212. Word Search II.java index c0d4312..7001f64 100644 --- a/java/212. Word Search II.java +++ b/java/212. Word Search II.java @@ -1,17 +1,17 @@ public class Solution { - Map> map=new HashMap>(); - Map finish=new HashMap(); + Map> map=new HashMap>(); + Map finish=new HashMap(); public List findWords(char[][] board, String[] words) { - List ans=new ArrayList(); - init(board); - for (int i = 0; i < words.length; i++) { - if (finish.get(words[i])==null&&pre(words[i])&&exist(board, words[i])) { - ans.add(words[i]); - } - //验证过的字符串不再次验证 - finish.put(words[i], 1); - } - return ans; + List ans=new ArrayList(); + init(board); + for (int i = 0; i < words.length; i++) { + if (finish.get(words[i])==null&&pre(words[i])&&exist(board, words[i])) { + ans.add(words[i]); + } + //验证过的字符串不再次验证 + finish.put(words[i], 1); + } + return ans; } /** * 粗略判断,判断字符串中的每个字符是否出现在表格上以及下一个字符是否在该字符周围可以找到 @@ -21,44 +21,44 @@ public List findWords(char[][] board, String[] words) { * @return */ public boolean pre(String word){ - char[] c=word.toCharArray(); - for (int i = 0; i < c.length-1; i++) { - char b=c[i]; - char e=c[i+1]; - if (map.get(b)==null||map.get(b).get(e)==null) { - return false; - } - } - return true; + char[] c=word.toCharArray(); + for (int i = 0; i < c.length-1; i++) { + char b=c[i]; + char e=c[i+1]; + if (map.get(b)==null||map.get(b).get(e)==null) { + return false; + } + } + return true; } /** * 预处理,先把每个字符周围的值写进map * @param board */ public void init(char[][] board){ - int iL=board.length; - int jL=board[0].length; - for (int i = 0; i < iL; i++) { - for (int j = 0; j < jL; j++) { - Map m=map.get(board[i][j]); - if (m==null) { - m=new HashMap(); - } - if (i-1>=0) { - m.put(board[i-1][j], 1); - } - if (i+1=0) { - m.put(board[i][j-1], 1); - } - if (j+1 m=map.get(board[i][j]); + if (m==null) { + m=new HashMap(); + } + if (i-1>=0) { + m.put(board[i-1][j], 1); + } + if (i+1=0) { + m.put(board[i][j-1], 1); + } + if (j+1> combinationSum3(int k, int n) { - List> answerList = new ArrayList>(); - for (int i = 1; i < 10; i++) { - List list = new ArrayList(); - list.add(i); - answerList.add(list); - } - for (int i = 0; i < answerList.size(); i++) { - List list = answerList.get(i); - int length=list.size(); - int value=n-sum(list); - if (length== k && value==0) { - continue; - } else { - if (value>0) { - for (int j = list.get(length- 1) + 1; j <=(value>9?9:value); j++) { - if (length newList = new ArrayList(); - newList.addAll(list); - newList.add(j); - answerList.add(newList); - } - } - } - answerList.remove(i); - i--; - } - } - return answerList; - } + List> answerList = new ArrayList>(); + for (int i = 1; i < 10; i++) { + List list = new ArrayList(); + list.add(i); + answerList.add(list); + } + for (int i = 0; i < answerList.size(); i++) { + List list = answerList.get(i); + int length=list.size(); + int value=n-sum(list); + if (length== k && value==0) { + continue; + } else { + if (value>0) { + for (int j = list.get(length- 1) + 1; j <=(value>9?9:value); j++) { + if (length newList = new ArrayList(); + newList.addAll(list); + newList.add(j); + answerList.add(newList); + } + } + } + answerList.remove(i); + i--; + } + } + return answerList; + } public int sum(List list){ - int sum=0; - for (int i = 0; i < list.size(); i++) { - sum+=list.get(i); - } - return sum; + int sum=0; + for (int i = 0; i < list.size(); i++) { + sum+=list.get(i); + } + return sum; } } \ No newline at end of file diff --git a/java/219. Contains Duplicate II.java b/java/219. Contains Duplicate II.java index 8e8515b..bdb1a1d 100644 --- a/java/219. Contains Duplicate II.java +++ b/java/219. Contains Duplicate II.java @@ -3,17 +3,17 @@ public boolean containsNearbyDuplicate(int[] nums, int k) { if(nums.length<=1){ return false; } - Map map = new HashMap(); - for (int i = 0; i < nums.length; i++) { - if (map.get(nums[i]) == null) { - map.put(nums[i], ""); - if (i >= k) { - map.remove(nums[i - k]); - } - } else { - return true; - } - } - return false; + Map map = new HashMap(); + for (int i = 0; i < nums.length; i++) { + if (map.get(nums[i]) == null) { + map.put(nums[i], ""); + if (i >= k) { + map.remove(nums[i - k]); + } + } else { + return true; + } + } + return false; } } \ No newline at end of file diff --git a/java/221. Maximal Square.java b/java/221. Maximal Square.java index 0144a48..7a9e718 100644 --- a/java/221. Maximal Square.java +++ b/java/221. Maximal Square.java @@ -1,32 +1,32 @@ public class Solution { public int maximalSquare(char[][] matrix) { - int cross=matrix.length; - if (cross==0) { - return 0; - } - int vertical=matrix[0].length; - int max=0; + int cross=matrix.length; + if (cross==0) { + return 0; + } + int vertical=matrix[0].length; + int max=0; int[][] size=new int[cross][vertical]; for (int i = 0; i < cross; i++) { - size[i][0]=matrix[i][0]=='1'?1:0; - if (size[i][0]>max) { - max=size[i][0]; - } - } + size[i][0]=matrix[i][0]=='1'?1:0; + if (size[i][0]>max) { + max=size[i][0]; + } + } for (int i = 0; i < vertical; i++) { - size[0][i]=matrix[0][i]=='1'?1:0; - if (size[0][i]>max) { - max=size[0][i]; - } - } + size[0][i]=matrix[0][i]=='1'?1:0; + if (size[0][i]>max) { + max=size[0][i]; + } + } for (int i = 1; i < cross; i++) { - for (int j = 1; j < vertical; j++) { - size[i][j]=matrix[i][j]=='1'?Math.min(Math.min(size[i-1][j], size[i][j-1]), size[i-1][j-1])+1:0; - if (size[i][j]>max) { - max=size[i][j]; - } - } - } + for (int j = 1; j < vertical; j++) { + size[i][j]=matrix[i][j]=='1'?Math.min(Math.min(size[i-1][j], size[i][j-1]), size[i-1][j-1])+1:0; + if (size[i][j]>max) { + max=size[i][j]; + } + } + } return max*max; } } \ No newline at end of file diff --git a/java/222. Count Complete Tree Nodes.java b/java/222. Count Complete Tree Nodes.java index 5d66e11..ff70cc2 100644 --- a/java/222. Count Complete Tree Nodes.java +++ b/java/222. Count Complete Tree Nodes.java @@ -9,30 +9,30 @@ */ public class Solution { public int countNodes(TreeNode root) { - if (root==null) { - return 0; - } + if (root==null) { + return 0; + } int l=getLeft(root); int r=getRight(root); if (l==r) { - return (2<<(l-1)) - 1; - } + return (2<<(l-1)) - 1; + } return 1+countNodes(root.left) + countNodes(root.right) ; } public int getLeft(TreeNode root){ - int h=0; - while(root!=null){ - h++; - root=root.left; - } - return h; + int h=0; + while(root!=null){ + h++; + root=root.left; + } + return h; } public int getRight(TreeNode root){ int h=0; - while(root!=null){ - h++; - root=root.right; - } - return h; + while(root!=null){ + h++; + root=root.right; + } + return h; } } \ No newline at end of file diff --git a/java/226. Invert Binary Tree.java b/java/226. Invert Binary Tree.java index 300eeda..a4d14bc 100644 --- a/java/226. Invert Binary Tree.java +++ b/java/226. Invert Binary Tree.java @@ -9,13 +9,13 @@ */ public class Solution { public TreeNode invertTree(TreeNode root) { - if (root != null) { - TreeNode node = root.left; - root.left = root.right; - root.right = node; - root.left = invertTree(root.left); - root.right = invertTree(root.right); - } + if (root != null) { + TreeNode node = root.left; + root.left = root.right; + root.right = node; + root.left = invertTree(root.left); + root.right = invertTree(root.right); + } return root; } } \ No newline at end of file diff --git a/java/228. Summary Ranges.java b/java/228. Summary Ranges.java index d114229..021a207 100644 --- a/java/228. Summary Ranges.java +++ b/java/228. Summary Ranges.java @@ -1,31 +1,31 @@ -public class Solution { +public class Solution { public List summaryRanges(int[] nums) { - List list=new ArrayList(); - if (nums.length==0) { - return list; - } + List list=new ArrayList(); + if (nums.length==0) { + return list; + } int k=0; if (nums.length==1) { - list.add(""+nums[k]); - return list; - } - for (int i = 1; i < nums.length; i++) { - if(nums[i]==nums[i-1]+1){ - continue; - }else { - if (i-1==k) { - list.add(""+nums[k]); - }else { - list.add(""+nums[k]+"->"+nums[i-1]); - } - k=i; - } - } - if (k==nums.length-1) { - list.add(""+nums[k]); - }else { - list.add(""+nums[k]+"->"+nums[nums.length-1]); - } - return list; + list.add(""+nums[k]); + return list; + } + for (int i = 1; i < nums.length; i++) { + if(nums[i]==nums[i-1]+1){ + continue; + }else { + if (i-1==k) { + list.add(""+nums[k]); + }else { + list.add(""+nums[k]+"->"+nums[i-1]); + } + k=i; + } + } + if (k==nums.length-1) { + list.add(""+nums[k]); + }else { + list.add(""+nums[k]+"->"+nums[nums.length-1]); + } + return list; } } \ No newline at end of file diff --git a/java/229. Majority Element II.java b/java/229. Majority Element II.java index a690b12..628ec8d 100644 --- a/java/229. Majority Element II.java +++ b/java/229. Majority Element II.java @@ -1,36 +1,36 @@ public class Solution { public List majorityElement(int[] nums) { - List ans=new ArrayList(); - if (nums.length==0) { - return ans; - } - if (nums.length==1) { - ans.add(nums[0]); - return ans; - } - if (nums.length==2) { - ans.add(nums[0]); - if (nums[1]!=nums[0]) { - ans.add(nums[1]); - } - return ans; - } - Arrays.sort(nums); - int l=nums.length/3; + List ans=new ArrayList(); + if (nums.length==0) { + return ans; + } + if (nums.length==1) { + ans.add(nums[0]); + return ans; + } + if (nums.length==2) { + ans.add(nums[0]); + if (nums[1]!=nums[0]) { + ans.add(nums[1]); + } + return ans; + } + Arrays.sort(nums); + int l=nums.length/3; for (int i = 0; i < nums.length-l;) { - if (nums[i+l]==nums[i]) { - ans.add(nums[i]); - i+=l; - }else { - i++; - } - } + if (nums[i+l]==nums[i]) { + ans.add(nums[i]); + i+=l; + }else { + i++; + } + } for (int i = 1; i < ans.size(); i++) { - if (ans.get(i)==ans.get(i-1)) { - ans.remove(i); - i--; - } - } + if (ans.get(i)==ans.get(i-1)) { + ans.remove(i); + i--; + } + } return ans; } } \ No newline at end of file diff --git a/java/230. Kth Smallest Element in a BST.java b/java/230. Kth Smallest Element in a BST.java index 2e582c2..4e0a7b0 100644 --- a/java/230. Kth Smallest Element in a BST.java +++ b/java/230. Kth Smallest Element in a BST.java @@ -11,17 +11,17 @@ public class Solution { public int kthSmallest(TreeNode root, int k) { int left=f(root.left); if (k==left+1) { - return root.val; - } + return root.val; + } if (k<=left) { - return kthSmallest(root.left,k); - } + return kthSmallest(root.left,k); + } return kthSmallest(root.right,k-left-1); } public int f(TreeNode root){ - if (root!=null) { - return 1+f(root.left)+f(root.right); - } - return 0; + if (root!=null) { + return 1+f(root.left)+f(root.right); + } + return 0; } } \ No newline at end of file diff --git a/java/234. Palindrome Linked List.java b/java/234. Palindrome Linked List.java index ddc3a0a..b65369c 100644 --- a/java/234. Palindrome Linked List.java +++ b/java/234. Palindrome Linked List.java @@ -9,39 +9,39 @@ public class Solution { public boolean isPalindrome(ListNode head) { if (head==null||head.next==null) { - return true; - } + return true; + } ListNode slow=head; ListNode fast=head; while (fast!=null&&fast.next!=null) { - slow=slow.next; - fast=fast.next.next; - } + slow=slow.next; + fast=fast.next.next; + } ListNode test=f(slow); while (test!=null) { - if (test.val!=head.val) { - return false; - } - test=test.next; - head=head.next; - } + if (test.val!=head.val) { + return false; + } + test=test.next; + head=head.next; + } return true; } //原地翻转链表 public ListNode f(ListNode node){ - if (node==null||node.next==null) { - return node; - } - ListNode l=node.next; - ListNode h; - node.next=null; - while (l!=null) { - h=l.next; - l.next=node; - node=l; - l=h; - } - return node; + if (node==null||node.next==null) { + return node; + } + ListNode l=node.next; + ListNode h; + node.next=null; + while (l!=null) { + h=l.next; + l.next=node; + node=l; + l=h; + } + return node; } } \ No newline at end of file diff --git a/java/235. Lowest Common Ancestor of a Binary Search Tree.java b/java/235. Lowest Common Ancestor of a Binary Search Tree.java index 3b7793c..73804e7 100644 --- a/java/235. Lowest Common Ancestor of a Binary Search Tree.java +++ b/java/235. Lowest Common Ancestor of a Binary Search Tree.java @@ -9,22 +9,22 @@ */ public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { - if (root==p) { - return p; - } - if (root==q) { - return q; - } + if (root==p) { + return p; + } + if (root==q) { + return q; + } int ip=root.val-p.val; int iq=root.val-q.val; if (ip>0!=iq>0) { - return root; - }else { - if (ip>0) { - return lowestCommonAncestor(root.left,p,q); - }else { - return lowestCommonAncestor(root.right,p,q); - } - } + return root; + }else { + if (ip>0) { + return lowestCommonAncestor(root.left,p,q); + }else { + return lowestCommonAncestor(root.right,p,q); + } + } } } \ No newline at end of file diff --git a/java/236. Lowest Common Ancestor of a Binary Tree.java b/java/236. Lowest Common Ancestor of a Binary Tree.java index cf02705..e8a42b1 100644 --- a/java/236. Lowest Common Ancestor of a Binary Tree.java +++ b/java/236. Lowest Common Ancestor of a Binary Tree.java @@ -14,33 +14,33 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { String sq=f(root,q); sq=sq.substring(0,sq.length()-1); for (int i = 0; i < Math.min(sp.length(), sq.length()); i++) { - String kp=sp.substring(i,i+1); - String kq=sq.substring(i,i+1); - if (kp.equals(kq)) { - if (kp.equals("0")) { - root=root.left; - }else { - root=root.right; - } - }else { - return root; - } - } - return root; + String kp=sp.substring(i,i+1); + String kq=sq.substring(i,i+1); + if (kp.equals(kq)) { + if (kp.equals("0")) { + root=root.left; + }else { + root=root.right; + } + }else { + return root; + } + } + return root; } public String f(TreeNode root, TreeNode p){ - if (root==null) { - return "3"; - } - if (root==p) { - return "2"; - } - String left=f(root.left,p); - String right=f(root.right,p); - if (left.substring(left.length()-1,left.length()).equals("2")) { - return "0"+left; - }else { - return "1"+right; - } + if (root==null) { + return "3"; + } + if (root==p) { + return "2"; + } + String left=f(root.left,p); + String right=f(root.right,p); + if (left.substring(left.length()-1,left.length()).equals("2")) { + return "0"+left; + }else { + return "1"+right; + } } } \ No newline at end of file diff --git a/java/238. Product of Array Except Self.java b/java/238. Product of Array Except Self.java index dc8f9f9..c6e7332 100644 --- a/java/238. Product of Array Except Self.java +++ b/java/238. Product of Array Except Self.java @@ -1,33 +1,33 @@ public class Solution { public int[] productExceptSelf(int[] nums) { - int length=nums.length; - int k=0; - boolean flag=true; - int product=1; + int length=nums.length; + int k=0; + boolean flag=true; + int product=1; for (int i = 0; i < length; i++) { - if (nums[i]==0) { - if (flag) { - k=i; - flag=false; - }else { - int[] answer=new int[length]; - for (int j = 0; j maxList=new ArrayList(); for (int i = 0; i < k; i++) { - while (maxList.size()!=0) { - if (maxList.get(maxList.size()-1)=0&&ytarget) { - x--; - }else { - y++; - } - } - return false; + int x=matrix.length-1; + int y=0; + while (x>=0&&ytarget) { + x--; + }else { + y++; + } + } + return false; } } \ No newline at end of file diff --git a/java/242. Valid Anagram.java b/java/242. Valid Anagram.java index f118af1..707d51e 100644 --- a/java/242. Valid Anagram.java +++ b/java/242. Valid Anagram.java @@ -1,29 +1,29 @@ public class Solution { public boolean isAnagram(String s, String t) { if (s.length()!=t.length()) { - return false; - } + return false; + } Map smap=new HashMap(); Map tmap=new HashMap(); char[] cs=s.toCharArray(); char[] ct=t.toCharArray(); for (int i = 0; i < ct.length; i++) { - if (smap.get(cs[i])!=null) { - smap.put(cs[i], smap.get(cs[i])+1); - }else { - smap.put(cs[i], 1); - } - if (tmap.get(ct[i])!=null) { - tmap.put(ct[i], tmap.get(ct[i])+1); - }else { - tmap.put(ct[i], 1); - } - } + if (smap.get(cs[i])!=null) { + smap.put(cs[i], smap.get(cs[i])+1); + }else { + smap.put(cs[i], 1); + } + if (tmap.get(ct[i])!=null) { + tmap.put(ct[i], tmap.get(ct[i])+1); + }else { + tmap.put(ct[i], 1); + } + } for (int i = 0; i < ct.length; i++) { - if (!tmap.get(ct[i]).equals(smap.get(ct[i]))) { - return false; - } - } + if (!tmap.get(ct[i]).equals(smap.get(ct[i]))) { + return false; + } + } return true; } } \ No newline at end of file diff --git a/java/257. Binary Tree Paths.java b/java/257. Binary Tree Paths.java index c214ba5..2de102c 100644 --- a/java/257. Binary Tree Paths.java +++ b/java/257. Binary Tree Paths.java @@ -9,44 +9,44 @@ */ public class Solution { public List binaryTreePaths(TreeNode root) { - List ansList=new ArrayList(); + List ansList=new ArrayList(); if (root!=null) { - List> totalList=new ArrayList>(); - List initList=new ArrayList(); - initList.add(root); - totalList.add(initList); - for (int i = 0; i < totalList.size(); i++) { - List list=totalList.get(i); - TreeNode node=list.get(list.size()-1); - boolean flag=false; - if (node.left!=null) { - List leftList=new ArrayList(); - leftList.addAll(list); - leftList.add(node.left); - totalList.add(leftList); - flag=true; - } - if (node.right!=null) { - List rightList=new ArrayList(); - rightList.addAll(list); - rightList.add(node.right); - totalList.add(rightList); - flag=true; - } - if (flag) { - totalList.remove(i); - i--; - } - } - for (int i = 0; i < totalList.size(); i++) { - List list=totalList.get(i); - String ans=""+list.get(0).val; - for (int j = 1; j < list.size(); j++) { - ans+=("->"+list.get(j).val); - } - ansList.add(ans); - } - } - return ansList; + List> totalList=new ArrayList>(); + List initList=new ArrayList(); + initList.add(root); + totalList.add(initList); + for (int i = 0; i < totalList.size(); i++) { + List list=totalList.get(i); + TreeNode node=list.get(list.size()-1); + boolean flag=false; + if (node.left!=null) { + List leftList=new ArrayList(); + leftList.addAll(list); + leftList.add(node.left); + totalList.add(leftList); + flag=true; + } + if (node.right!=null) { + List rightList=new ArrayList(); + rightList.addAll(list); + rightList.add(node.right); + totalList.add(rightList); + flag=true; + } + if (flag) { + totalList.remove(i); + i--; + } + } + for (int i = 0; i < totalList.size(); i++) { + List list=totalList.get(i); + String ans=""+list.get(0).val; + for (int j = 1; j < list.size(); j++) { + ans+=("->"+list.get(j).val); + } + ansList.add(ans); + } + } + return ansList; } } \ No newline at end of file diff --git a/java/263. Ugly Number.java b/java/263. Ugly Number.java index baa9b62..1745bf5 100644 --- a/java/263. Ugly Number.java +++ b/java/263. Ugly Number.java @@ -1,19 +1,19 @@ public class Solution { public boolean isUgly(int num) { - if (num==0) { - return false; - } + if (num==0) { + return false; + } while (num!=1) { - if (num%2==0) { - num/=2; - }else if (num%3==0) { - num/=3; - }else if (num%5==0) { - num/=5; - }else { - return false; - } - } - return true; + if (num%2==0) { + num/=2; + }else if (num%3==0) { + num/=3; + }else if (num%5==0) { + num/=5; + }else { + return false; + } + } + return true; } } \ No newline at end of file diff --git a/java/264. Ugly Number II.java b/java/264. Ugly Number II.java index 3eaff55..49cb7b7 100644 --- a/java/264. Ugly Number II.java +++ b/java/264. Ugly Number II.java @@ -1,51 +1,51 @@ public class Solution { public int nthUglyNumber(int n) { if (n<7) { - return n; - } + return n; + } List list=new ArrayList(); list.add(1); list.add(2); list.add(3); while (list.size() a(List list){ - int end=list.get(list.size()-1); - int a2=0; - int a3=0; - int a5=0; - int k=0; - for (int i = 0; i < list.size(); i++) { - if (list.get(i)*5>end) { - a5=list.get(i)*5; - k=i; - break; - } - } - for (int i = k; i < list.size(); i++) { - if (list.get(i)*3>end) { - a3=list.get(i)*3; - k=i; - break; - } - } - for (int i = k; i < list.size(); i++) { - if (list.get(i)*2>end) { - a2=list.get(i)*2; - k=i; - break; - } - } - int a=0; - if (a2end) { + a5=list.get(i)*5; + k=i; + break; + } + } + for (int i = k; i < list.size(); i++) { + if (list.get(i)*3>end) { + a3=list.get(i)*3; + k=i; + break; + } + } + for (int i = k; i < list.size(); i++) { + if (list.get(i)*2>end) { + a2=list.get(i)*2; + k=i; + break; + } + } + int a=0; + if (a2 map=new HashMap(); - map.put("1", "One"); - map.put("2", "Two"); - map.put("3", "Three"); - map.put("4", "Four"); - map.put("5", "Five"); - map.put("6", "Six"); - map.put("7", "Seven"); - map.put("8", "Eight"); - map.put("9", "Nine"); - map.put("10", "Ten"); - map.put("11", "Eleven"); - map.put("12", "Twelve"); - map.put("13", "Thirteen"); - map.put("14", "Fourteen"); - map.put("15", "Fifteen"); - map.put("16", "Sixteen"); - map.put("17", "Seventeen"); - map.put("18", "Eighteen"); - map.put("19", "Nineteen"); - map.put("20", "Twenty"); - map.put("30", "Thirty"); - map.put("40", "Forty"); - map.put("50", "Fifty"); - map.put("60", "Sixty"); - map.put("70", "Seventy"); - map.put("80", "Eighty"); - map.put("90", "Ninety"); + return "Zero"; + } + Map map=new HashMap(); + map.put("1", "One"); + map.put("2", "Two"); + map.put("3", "Three"); + map.put("4", "Four"); + map.put("5", "Five"); + map.put("6", "Six"); + map.put("7", "Seven"); + map.put("8", "Eight"); + map.put("9", "Nine"); + map.put("10", "Ten"); + map.put("11", "Eleven"); + map.put("12", "Twelve"); + map.put("13", "Thirteen"); + map.put("14", "Fourteen"); + map.put("15", "Fifteen"); + map.put("16", "Sixteen"); + map.put("17", "Seventeen"); + map.put("18", "Eighteen"); + map.put("19", "Nineteen"); + map.put("20", "Twenty"); + map.put("30", "Thirty"); + map.put("40", "Forty"); + map.put("50", "Fifty"); + map.put("60", "Sixty"); + map.put("70", "Seventy"); + map.put("80", "Eighty"); + map.put("90", "Ninety"); String str=String.valueOf(num); int length=str.length(); List list=new ArrayList(); while (str.length()>3) { - list.add(str.substring(length-3,length)); - str=str.substring(0,length-3); - length=str.length(); - } + list.add(str.substring(length-3,length)); + str=str.substring(0,length-3); + length=str.length(); + } list.add(str); System.out.println("list.size()="+list.size()); List ansList=new ArrayList(); for (int i = 0; i < list.size(); i++) { - ansList.add(f(list.get(i),map)); - } + ansList.add(f(list.get(i),map)); + } String ans=""; if (ansList.size()!=0) { - ans=ansList.get(0); - ansList.remove(0); - } + ans=ansList.get(0); + ansList.remove(0); + } if (ansList.size()!=0) { - if (ansList.get(0).length()!=0) { - if (ans.length()==0) { - ans=ansList.get(0)+" Thousand"; - }else { - ans=ansList.get(0)+" Thousand "+ans; - } - } - ansList.remove(0); - } + if (ansList.get(0).length()!=0) { + if (ans.length()==0) { + ans=ansList.get(0)+" Thousand"; + }else { + ans=ansList.get(0)+" Thousand "+ans; + } + } + ansList.remove(0); + } if (ansList.size()!=0) { - if (ansList.get(0).length()!=0) { - if (ans.length()==0) { - ans=ansList.get(0)+" Million"; - }else { - ans=ansList.get(0)+" Million "+ans; - } - } - ansList.remove(0); - } + if (ansList.get(0).length()!=0) { + if (ans.length()==0) { + ans=ansList.get(0)+" Million"; + }else { + ans=ansList.get(0)+" Million "+ans; + } + } + ansList.remove(0); + } if (ansList.size()!=0) { - if (ansList.get(0).length()!=0){ - if (ans.length()==0) { - ans=ansList.get(0)+" Billion"; - }else { - ans=ansList.get(0)+" Billion "+ans; - } - } - ansList.remove(0); - } - return ans; + if (ansList.get(0).length()!=0){ + if (ans.length()==0) { + ans=ansList.get(0)+" Billion"; + }else { + ans=ansList.get(0)+" Billion "+ans; + } + } + ansList.remove(0); + } + return ans; } public String f(String str,Map map){ - StringBuffer sb=new StringBuffer(); - int i=Integer.parseInt(str); - if (i>=100) { - sb.append(map.get(""+i/100)); - sb.append(" Hundred"); - } - i=i%100; - if (i!=0) { - if (sb.length()!=0) { - sb.append(" "); - } - if (i%100<20) { - sb.append(map.get(""+i)); - }else { - sb.append(map.get(""+(i-i%10))); - if (i%10!=0) { - sb.append(" "); - sb.append(map.get(""+i%10)); - } - } - } - return sb.toString(); + StringBuffer sb=new StringBuffer(); + int i=Integer.parseInt(str); + if (i>=100) { + sb.append(map.get(""+i/100)); + sb.append(" Hundred"); + } + i=i%100; + if (i!=0) { + if (sb.length()!=0) { + sb.append(" "); + } + if (i%100<20) { + sb.append(map.get(""+i)); + }else { + sb.append(map.get(""+(i-i%10))); + if (i%10!=0) { + sb.append(" "); + sb.append(map.get(""+i%10)); + } + } + } + return sb.toString(); } } \ No newline at end of file diff --git a/java/274. H-Index.java b/java/274. H-Index.java index 5e4339d..30a06e9 100644 --- a/java/274. H-Index.java +++ b/java/274. H-Index.java @@ -3,12 +3,12 @@ public int hIndex(int[] citations) { Arrays.sort(citations); int ans=0; for (int i = citations.length-1; i >=0 ; i--) { - if (citations[i]>ans) { - ans++; - }else { - return ans; - } - } - return ans; + if (citations[i]>ans) { + ans++; + }else { + return ans; + } + } + return ans; } } \ No newline at end of file diff --git a/java/275. H-Index II.java b/java/275. H-Index II.java index 2d08e2b..aacb20a 100644 --- a/java/275. H-Index II.java +++ b/java/275. H-Index II.java @@ -1,33 +1,33 @@ public class Solution { public int hIndex(int[] citations) { - if (citations.length==0) { - return 0; - } + if (citations.length==0) { + return 0; + } Arrays.sort(citations); int s=0; int e=citations.length-1; int m=(s+e)/2; int l=citations.length; while (e-s>1) { - if (lcitations[m]+m) { - s=m; - }else { - e=m; - } - m=(s+e)/2; - } + if (lcitations[m]+m) { + s=m; + }else { + e=m; + } + m=(s+e)/2; + } if (l>citations[e]+e) { - return l-e-1; - }else if(l>citations[s]+s){ - return l-e; - }else{ - return l-s; - } + return l-e-1; + }else if(l>citations[s]+s){ + return l-e; + }else{ + return l-s; + } } } \ No newline at end of file diff --git a/java/279. Perfect Squares.java b/java/279. Perfect Squares.java index 44ec35f..14a479b 100644 --- a/java/279. Perfect Squares.java +++ b/java/279. Perfect Squares.java @@ -1,24 +1,24 @@ public class Solution { - Map map=new HashMap(); + Map map=new HashMap(); public int numSquares(int n) { - if (n<4) { - return n; - } + if (n<4) { + return n; + } if (map.get(n)==null) { - int s=(int) Math.sqrt(n); - if (s*s==n) { - return 1; - } - int ans=numSquares(n-s*s); - //凭感觉处理了一下测试的最小值 - for (int i = s-1; i >=Math.max(s/2, 1); i--) { - int t=numSquares(n-i*i); - if (t=Math.max(s/2, 1); i--) { + int t=numSquares(n-i*i); + if (t { public List list=new ArrayList(); - public PeekingIterator(Iterator iterator) { - // initialize any member here. - while(iterator.hasNext()){ - list.add(iterator.next()); - } - } + public PeekingIterator(Iterator iterator) { + // initialize any member here. + while(iterator.hasNext()){ + list.add(iterator.next()); + } + } // Returns the next element in the iteration without advancing the iterator. - public Integer peek() { + public Integer peek() { return list.get(0); - } + } - // hasNext() and next() should behave the same as in the Iterator interface. - // Override them if needed. - @Override - public Integer next() { - Integer i=list.get(0); - list.remove(0); - return i; - } + // hasNext() and next() should behave the same as in the Iterator interface. + // Override them if needed. + @Override + public Integer next() { + Integer i=list.get(0); + list.remove(0); + return i; + } - @Override - public boolean hasNext() { - return list.size()>0; - } + @Override + public boolean hasNext() { + return list.size()>0; + } } \ No newline at end of file diff --git a/java/287. Find the Duplicate Number.java b/java/287. Find the Duplicate Number.java index a3d6b83..87c5b5b 100644 --- a/java/287. Find the Duplicate Number.java +++ b/java/287. Find the Duplicate Number.java @@ -1,33 +1,33 @@ public class Solution { public int findDuplicate(int[] nums) { - return f(nums,0,nums.length-1); + return f(nums,0,nums.length-1); } public int f(int[] nums,int s,int e){ - if (s==e) { - return 0; - } - if (s+1==e) { - if (nums[s]==nums[e]) { - return nums[s]; - } - return 0; - } - int m=(s+e)/2; - int a=f(nums,s,m); - int b=f(nums,m+1,e); - if (a!=0) { - return a; - } - if (b!=0) { - return b; - } - for (int i = s; i <=m; i++) { - for (int j = m+1; j <=e; j++) { - if (nums[i]==nums[j]) { - return nums[j]; - } - } - } - return 0; + if (s==e) { + return 0; + } + if (s+1==e) { + if (nums[s]==nums[e]) { + return nums[s]; + } + return 0; + } + int m=(s+e)/2; + int a=f(nums,s,m); + int b=f(nums,m+1,e); + if (a!=0) { + return a; + } + if (b!=0) { + return b; + } + for (int i = s; i <=m; i++) { + for (int j = m+1; j <=e; j++) { + if (nums[i]==nums[j]) { + return nums[j]; + } + } + } + return 0; } } \ No newline at end of file diff --git a/java/289. Game of Life.java b/java/289. Game of Life.java index 79325d0..e28dc84 100644 --- a/java/289. Game of Life.java +++ b/java/289. Game of Life.java @@ -2,29 +2,29 @@ public class Solution { public void gameOfLife(int[][] board) { int[][] newboard=new int[board.length][board[0].length]; for (int i = 0; i < board.length; i++) { - for (int j = 0; j < board[i].length; j++) { - int k=board[i][j]; - int v=f(board,i,j); - System.out.println("v="+v); - if (v==3||(v==2&&k==1)) { - System.out.println("true"); - newboard[i][j]=1; - } - } - } + for (int j = 0; j < board[i].length; j++) { + int k=board[i][j]; + int v=f(board,i,j); + System.out.println("v="+v); + if (v==3||(v==2&&k==1)) { + System.out.println("true"); + newboard[i][j]=1; + } + } + } for (int i = 0; i < board.length; i++) { - for (int j = 0; j < board[i].length; j++) { - board[i][j]=newboard[i][j]; - } - } + for (int j = 0; j < board[i].length; j++) { + board[i][j]=newboard[i][j]; + } + } } public int f(int[][] board,int x,int y){ - return g(board,x-1,y-1)+g(board,x-1,y)+g(board,x-1,y+1)+g(board,x,y-1)+g(board,x,y+1)+g(board,x+1,y-1)+g(board,x+1,y)+g(board,x+1,y+1); + return g(board,x-1,y-1)+g(board,x-1,y)+g(board,x-1,y+1)+g(board,x,y-1)+g(board,x,y+1)+g(board,x+1,y-1)+g(board,x+1,y)+g(board,x+1,y+1); } public int g(int[][] board,int x,int y){ - if (x>=0&&x=0&&y=0&&x=0&&y m=new HashMap(); //反向匹配 Map n=new HashMap(); for (int i = 0; i < strs.length; i++) { - String p=pattern.substring(i,i+1); - if (m.get(p)==null) { - m.put(p, strs[i]); - }else if (!m.get(p).equals(strs[i])) { - return false; - } - if (n.get(strs[i])==null) { - n.put(strs[i], p); - }else if (!n.get(strs[i]).equals(p)) { - return false; - } - } + String p=pattern.substring(i,i+1); + if (m.get(p)==null) { + m.put(p, strs[i]); + }else if (!m.get(p).equals(strs[i])) { + return false; + } + if (n.get(strs[i])==null) { + n.put(strs[i], p); + }else if (!n.get(strs[i]).equals(p)) { + return false; + } + } return true; } } \ No newline at end of file diff --git a/java/295. Find Median from Data Stream.java b/java/295. Find Median from Data Stream.java index a00beae..2bde8a8 100644 --- a/java/295. Find Median from Data Stream.java +++ b/java/295. Find Median from Data Stream.java @@ -23,9 +23,9 @@ public void addNum(int num) { if(list.get(e)num){ - list.add(s,num); + list.add(s,num); }else{ - list.add(e,num); + list.add(e,num); } } } diff --git a/java/299. Bulls and Cows.java b/java/299. Bulls and Cows.java index 1a90f89..0ab319a 100644 --- a/java/299. Bulls and Cows.java +++ b/java/299. Bulls and Cows.java @@ -1,29 +1,29 @@ public class Solution { public String getHint(String secret, String guess) { - int length=secret.length(); - if (length==0) { - return "0A0B"; - } - char[] cs=secret.toCharArray(); - char[] cg=guess.toCharArray(); - boolean[] f=new boolean[length]; - int a=0; - for (int i = 0; i < length; i++) { - boolean flag=cs[i]==cg[i]; - f[i]=flag; - a+=flag?1:0; - } - boolean[] g=new boolean[length]; - int b=0; - for (int i = 0; i < length; i++) { - for (int j = 0; j < length; j++) { - if (i!=j&&!g[j]&&!f[i]&&!f[j]&&cg[i]==cs[j]) { - b++; - g[j]=true; - break; - } - } - } - return a+"A"+b+"B"; + int length=secret.length(); + if (length==0) { + return "0A0B"; + } + char[] cs=secret.toCharArray(); + char[] cg=guess.toCharArray(); + boolean[] f=new boolean[length]; + int a=0; + for (int i = 0; i < length; i++) { + boolean flag=cs[i]==cg[i]; + f[i]=flag; + a+=flag?1:0; + } + boolean[] g=new boolean[length]; + int b=0; + for (int i = 0; i < length; i++) { + for (int j = 0; j < length; j++) { + if (i!=j&&!g[j]&&!f[i]&&!f[j]&&cg[i]==cs[j]) { + b++; + g[j]=true; + break; + } + } + } + return a+"A"+b+"B"; } } \ No newline at end of file diff --git a/java/300. Longest Increasing Subsequence.java b/java/300. Longest Increasing Subsequence.java index b5fbc4d..cf90735 100644 --- a/java/300. Longest Increasing Subsequence.java +++ b/java/300. Longest Increasing Subsequence.java @@ -1,37 +1,37 @@ public class Solution { - int[] ans; - int length=0; + int[] ans; + int length=0; public int lengthOfLIS(int[] nums) { - ans=new int[nums.length]; - for (int i = 0; i < nums.length; i++) { - f(nums[i]); - } - return length; + ans=new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + f(nums[i]); + } + return length; } public void f(int n){ - if (length==0||ans[length-1]1) { - if(ans[m]==n){ - return; - } - if (ans[m]>n) { - e=m; - }else { - s=m; - } - m=(s+e)/2; - } - if (ans[s]>n) { - ans[s]=n; - }else { - ans[e]=n; - } - } + if (length==0||ans[length-1]1) { + if(ans[m]==n){ + return; + } + if (ans[m]>n) { + e=m; + }else { + s=m; + } + m=(s+e)/2; + } + if (ans[s]>n) { + ans[s]=n; + }else { + ans[e]=n; + } + } } } \ No newline at end of file diff --git a/java/306. Additive Number.java b/java/306. Additive Number.java index 401cf75..19e970d 100644 --- a/java/306. Additive Number.java +++ b/java/306. Additive Number.java @@ -3,80 +3,80 @@ public boolean isAdditiveNumber(String num) { int length=num.length(); int l=Math.min(length, length*2/3+3); for (int i = 0; i < l-2; i++) { - String first=num.substring(0,i+1); - for (int j = i+1; j < l-1; j++) { - int f=i+1; - int s=j-i; - int t=length-j-1; - if (t>=s&&t>=f) { - String second=num.substring(i+1,j+1); - String third=num.substring(j+1,length); - if (f(first, second, third)) { - return true; - } - } - } - } + String first=num.substring(0,i+1); + for (int j = i+1; j < l-1; j++) { + int f=i+1; + int s=j-i; + int t=length-j-1; + if (t>=s&&t>=f) { + String second=num.substring(i+1,j+1); + String third=num.substring(j+1,length); + if (f(first, second, third)) { + return true; + } + } + } + } return false; } public boolean f(String first,String second,String third){ - if (third.equals("")) { - return true; - } - int f=first.length(); - int s=second.length(); - int t=third.length(); - if (tt) { - return false; - } - String test=third.substring(0,sum.length()); - if (sum.equals(test)) { - return f(second, sum, third.substring(sum.length(),t)); - } - return false; + if (third.equals("")) { + return true; + } + int f=first.length(); + int s=second.length(); + int t=third.length(); + if (tt) { + return false; + } + String test=third.substring(0,sum.length()); + if (sum.equals(test)) { + return f(second, sum, third.substring(sum.length(),t)); + } + return false; } public String addString(String sa,String sb){ - int al=sa.length(); - int bl=sb.length(); - if (al=10) { - sum[al-i-1]-=10; - flag=true; - }else{ - flag=false; - } - } - for (int i = 0; i < al-bl; i++) { - sum[i]=Integer.parseInt(""+a[i]); - } - StringBuffer sbf=new StringBuffer(); - if (!flag||al==bl) { - for (int i = 0; i < sum.length; i++) { - sbf.append(String.valueOf(sum[i])); - } - if (flag) { - return "1"+sbf.toString(); - }else { - return sbf.toString(); - } - }else { - sum[al-bl-1]++; - for (int i = 0; i < sum.length; i++) { - sbf.append(String.valueOf(sum[i])); - } - return sbf.toString(); - } + int al=sa.length(); + int bl=sb.length(); + if (al=10) { + sum[al-i-1]-=10; + flag=true; + }else{ + flag=false; + } + } + for (int i = 0; i < al-bl; i++) { + sum[i]=Integer.parseInt(""+a[i]); + } + StringBuffer sbf=new StringBuffer(); + if (!flag||al==bl) { + for (int i = 0; i < sum.length; i++) { + sbf.append(String.valueOf(sum[i])); + } + if (flag) { + return "1"+sbf.toString(); + }else { + return sbf.toString(); + } + }else { + sum[al-bl-1]++; + for (int i = 0; i < sum.length; i++) { + sbf.append(String.valueOf(sum[i])); + } + return sbf.toString(); + } } } \ No newline at end of file diff --git a/java/315. Count of Smaller Numbers After Self.java b/java/315. Count of Smaller Numbers After Self.java index 9d356ef..39e416b 100644 --- a/java/315. Count of Smaller Numbers After Self.java +++ b/java/315. Count of Smaller Numbers After Self.java @@ -1,35 +1,35 @@ public class Solution { public List countSmaller(int[] nums) { - List list=new ArrayList(); - int length=nums.length; + List list=new ArrayList(); + int length=nums.length; if (length==0) { - return list; - } + return list; + } List ars=new ArrayList(); ars.add(nums[length-1]); list.add(0); for (int i = length-2; i >=0; i--) { - int test=nums[i]; - int end=ars.size()-1; - int start=0; - int mid=(start+end)/2; - while (end-start>1) { - if (test<=ars.get(mid)) { - end=mid; - }else { - start=mid; - } - mid=(start+end)/2; - } - int res=end+1; - if (test<=ars.get(start)) { - res=start; - }else if (test<=ars.get(end)) { - res=end; - } - ars.add(res,test); - list.add(0,res); - } + int test=nums[i]; + int end=ars.size()-1; + int start=0; + int mid=(start+end)/2; + while (end-start>1) { + if (test<=ars.get(mid)) { + end=mid; + }else { + start=mid; + } + mid=(start+end)/2; + } + int res=end+1; + if (test<=ars.get(start)) { + res=start; + }else if (test<=ars.get(end)) { + res=end; + } + ars.add(res,test); + list.add(0,res); + } return list; } } \ No newline at end of file diff --git a/java/316. Remove Duplicate Letters.java b/java/316. Remove Duplicate Letters.java index f9a401e..c68bdc8 100644 --- a/java/316. Remove Duplicate Letters.java +++ b/java/316. Remove Duplicate Letters.java @@ -1,63 +1,63 @@ public class Solution { - List> totalList=new ArrayList>(); - int index=-1; + List> totalList=new ArrayList>(); + int index=-1; public String removeDuplicateLetters(String s) { if (s==null||s.length()==0) { - return ""; - } + return ""; + } char[] chars=s.toCharArray(); for (int i = 0; i < 26; i++) { - List list=new ArrayList(); - totalList.add(list); - } + List list=new ArrayList(); + totalList.add(list); + } for (int i = 0; i < chars.length; i++) { - int k=chars[i]-97; - totalList.get(k).add(i); - } + int k=chars[i]-97; + totalList.get(k).add(i); + } int count=0; for (int i = 0; i < 26; i++) { - if (totalList.get(i).size()!=0) { - count++; - } - } + if (totalList.get(i).size()!=0) { + count++; + } + } char[] ans=new char[count]; for (int i = 0; i < count; i++) { - ans[i]=f(); - } + ans[i]=f(); + } return String.valueOf(ans); } public char f(){ - int min=Integer.MAX_VALUE; - for (int i = 0; i < 26; i++) { - if (totalList.get(i).size()!=0) { - int k=totalList.get(i).get(totalList.get(i).size()-1); - if (kindex) { - totalList.set(i, new ArrayList()); - index=val; - for (int j = 0; j < 26; j++) { - if (totalList.get(j).size()!=0) { - List list=totalList.get(j); - for (int k = 0; k < list.size();) { - if (list.get(0)index) { + totalList.set(i, new ArrayList()); + index=val; + for (int j = 0; j < 26; j++) { + if (totalList.get(j).size()!=0) { + List list=totalList.get(j); + for (int k = 0; k < list.size();) { + if (list.get(0)> map=new HashMap>(); - //装初始字符串数组 - String[] initStrs=null; - //装处理过的字符串数组 - String[] nowStrs=null; + Map> map=new HashMap>(); + //装初始字符串数组 + String[] initStrs=null; + //装处理过的字符串数组 + String[] nowStrs=null; public int maxProduct(String[] words) { - int length=words.length; - if (length<2) { - return 0; - } - initStrs=words; - nowStrs=new String[length]; - Map cond=null; - StringBuffer sb=null; - //对原数组进行处理,保证处理后的字符串中没有重复字母,缩减判断时间 - for (int i = 0; i < length; i++) { - cond=new HashMap(); - sb=new StringBuffer(); - for (int j = 0; j < words[i].length(); j++) { - String k=words[i].substring(j,j+1); - if (cond.get(k)==null) { - sb.append(k); - cond.put(k,1); - } - } - nowStrs[i]=sb.toString(); - } - //遍历原有数组,按字符写进map - for (int i = 0; i < length; i++) { - char[] cs=nowStrs[i].toCharArray(); - for (int j = 0; j < cs.length; j++) { - if (map.get(cs[j])==null) { - Map m=new HashMap(); - m.put(i, 1); - map.put(cs[j], m); - }else { - map.get(cs[j]).put(i, 1); - } - } - } - int max=0; - for (int i = 0; i < length; i++) { - for (int j = i+1; j < length; j++) { - int test=f(i,j); - if (test>max) { - max=test; - } - } - } - return max; + int length=words.length; + if (length<2) { + return 0; + } + initStrs=words; + nowStrs=new String[length]; + Map cond=null; + StringBuffer sb=null; + //对原数组进行处理,保证处理后的字符串中没有重复字母,缩减判断时间 + for (int i = 0; i < length; i++) { + cond=new HashMap(); + sb=new StringBuffer(); + for (int j = 0; j < words[i].length(); j++) { + String k=words[i].substring(j,j+1); + if (cond.get(k)==null) { + sb.append(k); + cond.put(k,1); + } + } + nowStrs[i]=sb.toString(); + } + //遍历原有数组,按字符写进map + for (int i = 0; i < length; i++) { + char[] cs=nowStrs[i].toCharArray(); + for (int j = 0; j < cs.length; j++) { + if (map.get(cs[j])==null) { + Map m=new HashMap(); + m.put(i, 1); + map.put(cs[j], m); + }else { + map.get(cs[j]).put(i, 1); + } + } + } + int max=0; + for (int i = 0; i < length; i++) { + for (int j = i+1; j < length; j++) { + int test=f(i,j); + if (test>max) { + max=test; + } + } + } + return max; } public int f(int i,int j){ - char[] csi=nowStrs[i].toCharArray(); - //选择一个字符串开始读取,如果该字符串包含的任意一个另外字符串拥有的字符,则返回0 - for (int k = 0; k < csi.length; k++) { - if (map.get(csi[k]).get(j)!=null) { - return 0; - } - } - return initStrs[i].length()*initStrs[j].length(); + char[] csi=nowStrs[i].toCharArray(); + //选择一个字符串开始读取,如果该字符串包含的任意一个另外字符串拥有的字符,则返回0 + for (int k = 0; k < csi.length; k++) { + if (map.get(csi[k]).get(j)!=null) { + return 0; + } + } + return initStrs[i].length()*initStrs[j].length(); } diff --git a/java/322. Coin Change.java b/java/322. Coin Change.java index 5eda1a9..588c007 100644 --- a/java/322. Coin Change.java +++ b/java/322. Coin Change.java @@ -1,37 +1,37 @@ public class Solution { - int[] array; - int minValue; - Map map=new HashMap(); + int[] array; + int minValue; + Map map=new HashMap(); public int coinChange(int[] coins, int amount) { - Arrays.sort(coins); - minValue=coins[0]; - array=coins; - return f(amount); + Arrays.sort(coins); + minValue=coins[0]; + array=coins; + return f(amount); } public int f(int amount){ - if (map.get(amount)!=null) { - return map.get(amount); - } - if (amount==0) { - return 0; - } - if (amount0&&matrix[i-1][j]>matrix[i][j])||(j>0&&matrix[i][j-1]>matrix[i][j])||(imatrix[i][j])||(jmatrix[i][j])) { - int test=f(i, j,0); - if (test>res) { - res=test; - } - } - } - } + for (int j = 0; j < cols; j++) { + if ((i>0&&matrix[i-1][j]>matrix[i][j])||(j>0&&matrix[i][j-1]>matrix[i][j])||(imatrix[i][j])||(jmatrix[i][j])) { + int test=f(i, j,0); + if (test>res) { + res=test; + } + } + } + } return res; } public int f(int x,int y,int d){ - int nx=x+(d%2); - int ny=y+(d/2); - if(nx<0||nx>=rows||ny<0||ny>=cols){ - return 0; - }else if (d!=0&&nums[nx][ny]<=nums[x][y]) { - return 0; - }else if(flag[nx][ny]){ - return ans[nx][ny]; - } else { - int res=0; - for (int i = -2; i <=2; i++) { - if (i!=0) { - int test=f(nx,ny,i); - if (test>res) { - res=test; - } - } - } - flag[nx][ny]=true; - ans[nx][ny]=res+1; - return res+1; - } + int nx=x+(d%2); + int ny=y+(d/2); + if(nx<0||nx>=rows||ny<0||ny>=cols){ + return 0; + }else if (d!=0&&nums[nx][ny]<=nums[x][y]) { + return 0; + }else if(flag[nx][ny]){ + return ans[nx][ny]; + } else { + int res=0; + for (int i = -2; i <=2; i++) { + if (i!=0) { + int test=f(nx,ny,i); + if (test>res) { + res=test; + } + } + } + flag[nx][ny]=true; + ans[nx][ny]=res+1; + return res+1; + } } } \ No newline at end of file diff --git a/java/331. Verify Preorder Serialization of a Binary Tree.java b/java/331. Verify Preorder Serialization of a Binary Tree.java index b3fb49d..f1a926e 100644 --- a/java/331. Verify Preorder Serialization of a Binary Tree.java +++ b/java/331. Verify Preorder Serialization of a Binary Tree.java @@ -1,30 +1,30 @@ public class Solution { public boolean isValidSerialization(String preorder) { - String[] strs=preorder.split(","); - List list=new ArrayList(); - for (int i = 0; i < strs.length; i++) { - list.add(strs[i]); - } - return isValid(list); + String[] strs=preorder.split(","); + List list=new ArrayList(); + for (int i = 0; i < strs.length; i++) { + list.add(strs[i]); + } + return isValid(list); } public boolean isValid(List list) { - int len=list.size(); - if (len==1) { - return "#".equals(list.get(0)); - } - for (int i = 0; i < len-2; i++) { - if ("#".equals(list.get(i+1))&&"#".equals(list.get(i+2))) { - if ("#".equals(list.get(i))) { - return false; - }else{ - list.remove(i+2); - list.remove(i+1); - list.remove(i); - list.add(i,"#"); - return isValid(list); - } - } - } - return false; + int len=list.size(); + if (len==1) { + return "#".equals(list.get(0)); + } + for (int i = 0; i < len-2; i++) { + if ("#".equals(list.get(i+1))&&"#".equals(list.get(i+2))) { + if ("#".equals(list.get(i))) { + return false; + }else{ + list.remove(i+2); + list.remove(i+1); + list.remove(i); + list.add(i,"#"); + return isValid(list); + } + } + } + return false; } } \ No newline at end of file diff --git a/java/341. Flatten Nested List Iterator.java b/java/341. Flatten Nested List Iterator.java index ad19328..6b03414 100644 --- a/java/341. Flatten Nested List Iterator.java +++ b/java/341. Flatten Nested List Iterator.java @@ -17,9 +17,9 @@ */ public class NestedIterator implements Iterator { private List ans; - private int index; - private int length; - + private int index; + private int length; + public NestedIterator(List nestedList) { ans=init(nestedList); index=0; @@ -28,24 +28,24 @@ public NestedIterator(List nestedList) { @Override public Integer next() { - return index init(List nestedList){ - List list=new ArrayList(); - for (NestedInteger nestedInteger : nestedList) { - if (nestedInteger.isInteger()) { - list.add(nestedInteger.getInteger()); - }else{ - list.addAll(init(nestedInteger.getList())); - } - } - return list; + List list=new ArrayList(); + for (NestedInteger nestedInteger : nestedList) { + if (nestedInteger.isInteger()) { + list.add(nestedInteger.getInteger()); + }else{ + list.addAll(init(nestedInteger.getList())); + } + } + return list; } } diff --git a/java/343. Integer Break.java b/java/343. Integer Break.java index 78d24c6..d68c3be 100644 --- a/java/343. Integer Break.java +++ b/java/343. Integer Break.java @@ -1,29 +1,29 @@ public class Solution { - Map map = new HashMap() { - { - put(2, 2); - put(3, 3); - } - }; - public int integerBreak(int n) { - if (n == 2) - return 1; - if (n == 3) - return 2; - return f(n); - } + Map map = new HashMap() { + { + put(2, 2); + put(3, 3); + } + }; + public int integerBreak(int n) { + if (n == 2) + return 1; + if (n == 3) + return 2; + return f(n); + } - public int f(int n) { - if (map.get(n) == null) { - int sum = 0; - for (int i = 2; i < n - 1; i++) { - int test = f(i) * f(n - i); - if (test > sum) { - sum = test; - } - } - map.put(n, sum); - } - return map.get(n); - } + public int f(int n) { + if (map.get(n) == null) { + int sum = 0; + for (int i = 2; i < n - 1; i++) { + int test = f(i) * f(n - i); + if (test > sum) { + sum = test; + } + } + map.put(n, sum); + } + return map.get(n); + } } \ No newline at end of file diff --git a/java/345. Reverse Vowels of a String.java b/java/345. Reverse Vowels of a String.java index ffe4dce..c8fff4f 100644 --- a/java/345. Reverse Vowels of a String.java +++ b/java/345. Reverse Vowels of a String.java @@ -4,20 +4,20 @@ public String reverseVowels(String s) { int i=0; int j=cs.length-1; while (i