From 82cc0b4be921c184e88855c0171e58de75d54ba0 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 19:03:52 +0800 Subject: [PATCH 001/116] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Array/908.java | 16 -------------- leetcode/Array/No01.java | 3 ++- leetcode/Array/No121.java | 2 +- leetcode/Array/No122.java | 2 +- leetcode/Array/No14.java | 45 +++++++++++++++++++++------------------ leetcode/Array/No217.java | 24 ++++++++++++--------- leetcode/Array/No26.java | 2 +- leetcode/Array/No27.java | 2 +- leetcode/Array/No35.java | 38 +++++++++++++++++---------------- leetcode/Array/No53.java | 42 ++++++++++++++++++------------------ leetcode/Array/No561.java | 22 +++++++++++-------- leetcode/Array/No66.java | 2 +- leetcode/Array/No88.java | 32 ++++++++++++++-------------- leetcode/Array/No908.java | 20 +++++++++++++++++ 14 files changed, 135 insertions(+), 117 deletions(-) delete mode 100644 leetcode/Array/908.java create mode 100644 leetcode/Array/No908.java diff --git a/leetcode/Array/908.java b/leetcode/Array/908.java deleted file mode 100644 index d8a4c29..0000000 --- a/leetcode/Array/908.java +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { - public int smallestRangeI(int[] A, int K) { - if(A.length == 0 || A.length == 1){ - return 0; - } - //[2,7,2] [2,2,7] K = 1 - //[1,3,6] 3 - int len = A.length; - Arrays.sort(A); - if(A[len - 1] - A[0] <= 2*K){ - return 0; - }else{ - return A[len-1] - 2*K - A[0]; - } - } -} diff --git a/leetcode/Array/No01.java b/leetcode/Array/No01.java index e106d70..14aaaa2 100644 --- a/leetcode/Array/No01.java +++ b/leetcode/Array/No01.java @@ -1,4 +1,5 @@ -package Array; +package Algorithm.leetcode.Array; + import java.util.HashMap; public class No01 { diff --git a/leetcode/Array/No121.java b/leetcode/Array/No121.java index e24b9c5..d9d6da4 100644 --- a/leetcode/Array/No121.java +++ b/leetcode/Array/No121.java @@ -1,4 +1,4 @@ -package Array; +package Algorithm.leetcode.Array; public class No121 { public static void main(String[] args) { diff --git a/leetcode/Array/No122.java b/leetcode/Array/No122.java index 48c9fc0..315756d 100644 --- a/leetcode/Array/No122.java +++ b/leetcode/Array/No122.java @@ -1,4 +1,4 @@ -package Array; +package Algorithm.leetcode.Array; public class No122 { public int maxProfit(int[] prices) { diff --git a/leetcode/Array/No14.java b/leetcode/Array/No14.java index 3042ffb..89a153f 100644 --- a/leetcode/Array/No14.java +++ b/leetcode/Array/No14.java @@ -1,22 +1,25 @@ -class Solution { - public String longestCommonPrefix(String[] strs) { - Arrays.sort(strs); - int i = 0; - if(strs.length == 0){ - return ""; - } - if(strs == null){ - return null; - } - while(i < strs[0].length() && strs[0].toCharArray()[i] == (strs[strs.length - 1].toCharArray()[i])){ - i++; - } - if(i == 0){ - return ""; - }else{ - return strs[0].substring(0,i); - } - - } -} +package Algorithm.leetcode.Array; + +import java.util.Arrays; +class No14 { + public String longestCommonPrefix(String[] strs) { + Arrays.sort(strs); + int i = 0; + if (strs.length == 0) { + return ""; + } + if (strs == null) { + return null; + } + while (i < strs[0].length() && strs[0].toCharArray()[i] == (strs[strs.length - 1].toCharArray()[i])) { + i++; + } + if (i == 0) { + return ""; + } else { + return strs[0].substring(0, i); + } + + } +} diff --git a/leetcode/Array/No217.java b/leetcode/Array/No217.java index 7c89da7..3806d20 100644 --- a/leetcode/Array/No217.java +++ b/leetcode/Array/No217.java @@ -1,11 +1,15 @@ -class Solution { - public boolean containsDuplicate(int[] nums) { - Arrays.sort(nums); - for(int i = 0;i < nums.length-1;i++){ - if(nums[i] == nums[i+1]){ - return true; - } - } - return false; - } +package Algorithm.leetcode.Array; + +import java.util.Arrays; + +public class No217 { + public boolean containsDuplicate(int[] nums) { + Arrays.sort(nums); + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] == nums[i + 1]) { + return true; + } + } + return false; + } } diff --git a/leetcode/Array/No26.java b/leetcode/Array/No26.java index 7d9dc9e..002dde1 100644 --- a/leetcode/Array/No26.java +++ b/leetcode/Array/No26.java @@ -1,4 +1,4 @@ -package Array; +package Algorithm.leetcode.Array; public class No26 { public int removeDuplicates(int[] nums) { diff --git a/leetcode/Array/No27.java b/leetcode/Array/No27.java index 66a3d08..8e21d10 100644 --- a/leetcode/Array/No27.java +++ b/leetcode/Array/No27.java @@ -1,4 +1,4 @@ -package Array; +package Algorithm.leetcode.Array; public class No27 { public int removeElement(int[] nums, int val) { diff --git a/leetcode/Array/No35.java b/leetcode/Array/No35.java index 1bfebfa..f03fdab 100644 --- a/leetcode/Array/No35.java +++ b/leetcode/Array/No35.java @@ -1,4 +1,4 @@ -package Array; +package Algorithm.leetcode.Array; public class No35 { public int searchInsert(int[] nums, int target) { @@ -10,21 +10,23 @@ public int searchInsert(int[] nums, int target) { } return k; } + + public int searchInsertOther(int[] nums, int target) { + for (int i = 0; i < nums.length; i++) { + if (nums[i] == target) { + return i; + } + if (nums[0] > target) { + return 0; + } + if (nums[nums.length - 1] < target) { + return nums.length; + } + if (nums[i] < target && target < nums[i + 1]) { + return i + 1; + } + } + return -1; + } + } -public int searchInsert(int[] nums, int target) { - for(int i = 0;i < nums.length; i++){ - if(nums[i] == target){ - return i; - } - if(nums[0] > target){ - return 0; - } - if(nums[nums.length - 1] < target){ - return nums.length; - } - if(nums[i] < target && target < nums[i+1]){ - return i + 1; - } - } - return -1; - } diff --git a/leetcode/Array/No53.java b/leetcode/Array/No53.java index 3b35eee..d1bd9a8 100644 --- a/leetcode/Array/No53.java +++ b/leetcode/Array/No53.java @@ -1,6 +1,7 @@ -package Array; +package Algorithm.leetcode.Array; public class No53 { + public int maxSubArray(int[] nums) { int mid = 0; int max = 0; @@ -30,25 +31,24 @@ public int maxSubArray01(int[] nums) { } return max; } -public int maxSubArray(int[] nums) { -if(nums.length == 0){ - return 0; - } - int sum = nums[0]; - int max = nums[0]; - for(int i = 1; i < nums.length; i++){ - if(sum > 0){ - sum += nums[i]; - }else{ - sum = nums[i]; - } - if(max < sum){ - - max = sum; - } - } - return max; - } -} + public int maxSubArray02(int[] nums) { + if (nums.length == 0) { + return 0; + } + int sum = nums[0]; + int max = nums[0]; + for (int i = 1; i < nums.length; i++) { + if (sum > 0) { + sum += nums[i]; + } else { + sum = nums[i]; + } + if (max < sum) { + max = sum; + } + } + return max; + } +} diff --git a/leetcode/Array/No561.java b/leetcode/Array/No561.java index 295d3b5..3cdb2ec 100644 --- a/leetcode/Array/No561.java +++ b/leetcode/Array/No561.java @@ -1,10 +1,14 @@ -class Solution { - public int arrayPairSum(int[] nums) { - int result = 0; - Arrays.sort(nums); - for(int i = 0;i < nums.length;i += 2){ - result += nums[i]; - } - return result; - } +package Algorithm.leetcode.Array; + +import java.util.Arrays; + +public class No561 { + public int arrayPairSum(int[] nums) { + int result = 0; + Arrays.sort(nums); + for (int i = 0; i < nums.length; i += 2) { + result += nums[i]; + } + return result; + } } diff --git a/leetcode/Array/No66.java b/leetcode/Array/No66.java index 6fae6e0..bc8af87 100644 --- a/leetcode/Array/No66.java +++ b/leetcode/Array/No66.java @@ -1,4 +1,4 @@ -package Array; +package Algorithm.leetcode.Array; public class No66 { public int[] plusOne(int[] digits) { diff --git a/leetcode/Array/No88.java b/leetcode/Array/No88.java index a5d32fa..3f25087 100644 --- a/leetcode/Array/No88.java +++ b/leetcode/Array/No88.java @@ -1,4 +1,5 @@ -package Array; +package Algorithm.leetcode.Array; + import java.util.Arrays; public class No88 { @@ -10,19 +11,18 @@ public void merge(int[] nums1, int m, int[] nums2, int n) { } Arrays.sort(nums1); } -} -class Solution { - public void merge(int[] nums1, int m, int[] nums2, int n) { - int i = m -1,j = n - 1, k = m + n - 1; - while(i > -1 && j > -1){ - if(nums1[i] > nums2[j]){ - nums1[k--] = nums1[i--]; - }else{ - nums1[k--] = nums2[j--]; - } - } - while(j > -1){ - nums1[k--] = nums2[j--]; - } - } + + public void merge01(int[] nums1, int m, int[] nums2, int n) { + int i = m - 1, j = n - 1, k = m + n - 1; + while (i > -1 && j > -1) { + if (nums1[i] > nums2[j]) { + nums1[k--] = nums1[i--]; + } else { + nums1[k--] = nums2[j--]; + } + } + while (j > -1) { + nums1[k--] = nums2[j--]; + } + } } diff --git a/leetcode/Array/No908.java b/leetcode/Array/No908.java new file mode 100644 index 0000000..0a2eb08 --- /dev/null +++ b/leetcode/Array/No908.java @@ -0,0 +1,20 @@ +package Algorithm.leetcode.Array; + +import java.util.Arrays; + +public class No908 { + public int smallestRangeI(int[] A, int K) { + if (A.length == 0 || A.length == 1) { + return 0; + } + // [2,7,2] [2,2,7] K = 1 + // [1,3,6] 3 + int len = A.length; + Arrays.sort(A); + if (A[len - 1] - A[0] <= 2 * K) { + return 0; + } else { + return A[len - 1] - 2 * K - A[0]; + } + } +} From ed0f44d14d22c58548f9aa3ea9ff12f9075693a6 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 19:04:58 +0800 Subject: [PATCH 002/116] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/DP/121.java | 28 ---------------------------- leetcode/DP/No121.java | 29 +++++++++++++++++++++++++++++ leetcode/DP/No122.java | 28 +++++++++++++++------------- leetcode/DP/No198.java | 4 +++- leetcode/DP/No70.java | 26 ++++++++++++++------------ 5 files changed, 61 insertions(+), 54 deletions(-) delete mode 100644 leetcode/DP/121.java create mode 100644 leetcode/DP/No121.java diff --git a/leetcode/DP/121.java b/leetcode/DP/121.java deleted file mode 100644 index ebf308a..0000000 --- a/leetcode/DP/121.java +++ /dev/null @@ -1,28 +0,0 @@ -class Solution { - public int maxProfit(int[] prices){ - if(prices.length == 0){ - return 0; - } - int sum = 0; - int mid = prices[0]; - for(int i = 1;i < prices.length;i++){ - if(mid < prices[i]){ - sum = Math.max(sum,prices[i] - mid); - }else{ - mid = prices[i]; - } - } - return sum; - } -} -class Solution { - public int maxProfit(int[] prices) { - int sum = 0; - for(int i = 0;i < prices.length;i++){ - for(int j = i + 1;j < prices.length;j++){ - sum = Math.max(prices[j] - prices[i],sum); - } - } - return sum; - } -} diff --git a/leetcode/DP/No121.java b/leetcode/DP/No121.java new file mode 100644 index 0000000..43f36fb --- /dev/null +++ b/leetcode/DP/No121.java @@ -0,0 +1,29 @@ +package Algorithm.leetcode.DP; + +public class No121 { + public int maxProfit(int[] prices) { + if (prices.length == 0) { + return 0; + } + int sum = 0; + int mid = prices[0]; + for (int i = 1; i < prices.length; i++) { + if (mid < prices[i]) { + sum = Math.max(sum, prices[i] - mid); + } else { + mid = prices[i]; + } + } + return sum; + } + + public int maxProfit01(int[] prices) { + int sum = 0; + for (int i = 0; i < prices.length; i++) { + for (int j = i + 1; j < prices.length; j++) { + sum = Math.max(prices[j] - prices[i], sum); + } + } + return sum; + } +} diff --git a/leetcode/DP/No122.java b/leetcode/DP/No122.java index 33bef31..badbfaf 100644 --- a/leetcode/DP/No122.java +++ b/leetcode/DP/No122.java @@ -1,14 +1,16 @@ -class Solution { - public int maxProfit(int[] prices){ - if(prices.length == 0 || prices.length == 1){ - return 0; - } - int sum = 0; - for(int i = 0;i < prices.length - 1;i++){ - if(prices[i+1] > prices[i]){ - sum += prices[i+1] - prices[i]; - } - } - return sum; - } +package Algorithm.leetcode.DP; + +public class No122 { + public int maxProfit(int[] prices) { + if (prices.length == 0 || prices.length == 1) { + return 0; + } + int sum = 0; + for (int i = 0; i < prices.length - 1; i++) { + if (prices[i + 1] > prices[i]) { + sum += prices[i + 1] - prices[i]; + } + } + return sum; + } } diff --git a/leetcode/DP/No198.java b/leetcode/DP/No198.java index 32dc196..d979ed4 100644 --- a/leetcode/DP/No198.java +++ b/leetcode/DP/No198.java @@ -1,4 +1,6 @@ -class No198 { +package Algorithm.leetcode.DP; + +public class No198 { public int rob(int[] nums) { int a = 0; int b = 0; diff --git a/leetcode/DP/No70.java b/leetcode/DP/No70.java index 132699b..c2250f4 100644 --- a/leetcode/DP/No70.java +++ b/leetcode/DP/No70.java @@ -1,14 +1,16 @@ +package Algorithm.leetcode.DP; + class Solution { - public int climbStairs(int n) { - if(n == 0 || n == 1 || n == 2){ - return n; - } - int[] arr = new int[n]; - arr[0] = 1; - arr[1] = 2; - for(int i = 2;i < n;i++){ - arr[i] = arr[i-1] + arr[i-2]; - } - return arr[n-1]; - } + public int climbStairs(int n) { + if (n == 0 || n == 1 || n == 2) { + return n; + } + int[] arr = new int[n]; + arr[0] = 1; + arr[1] = 2; + for (int i = 2; i < n; i++) { + arr[i] = arr[i - 1] + arr[i - 2]; + } + return arr[n - 1]; + } } From b014446c0f131edb505a440cba762832d3f126d7 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 19:05:25 +0800 Subject: [PATCH 003/116] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/LinkedList/No106.java | 2 +- leetcode/LinkedList/No141.java | 2 +- leetcode/LinkedList/No203.java | 40 ++++++++++++++------------------ leetcode/LinkedList/No206.java | 42 +++++++++++++++++----------------- leetcode/LinkedList/No21.java | 2 +- leetcode/LinkedList/No234.java | 2 +- leetcode/LinkedList/No237.java | 2 +- leetcode/LinkedList/No83.java | 38 +++++++++++++++--------------- 8 files changed, 61 insertions(+), 69 deletions(-) diff --git a/leetcode/LinkedList/No106.java b/leetcode/LinkedList/No106.java index 8d42eca..4eae59f 100644 --- a/leetcode/LinkedList/No106.java +++ b/leetcode/LinkedList/No106.java @@ -1,4 +1,4 @@ -package LinkedList; +package Algorithm.leetcode.LinkedList; public class No106 { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { diff --git a/leetcode/LinkedList/No141.java b/leetcode/LinkedList/No141.java index 82113ff..261eb3f 100644 --- a/leetcode/LinkedList/No141.java +++ b/leetcode/LinkedList/No141.java @@ -1,4 +1,4 @@ -package LinkedList; +package Algorithm.leetcode.LinkedList; public class No141 { public boolean hasCycle(ListNode head) { diff --git a/leetcode/LinkedList/No203.java b/leetcode/LinkedList/No203.java index 8ed6b88..21dffb9 100644 --- a/leetcode/LinkedList/No203.java +++ b/leetcode/LinkedList/No203.java @@ -1,24 +1,18 @@ -/** - * Definition for singly-linked list. - * public class ListNode { - * int val; - * ListNode next; - * ListNode(int x) { val = x; } - * } - */ -class Solution { - public ListNode removeElements(ListNode head, int val) { - ListNode prehead = new ListNode(-1); - prehead.next = head; - ListNode cur = head,pre = prehead; - while(cur != null){ - if(cur.val == val){ - pre.next = cur.next; - }else{ - pre = pre.next; - } - cur = cur.next; - } - return prehead.next; - } +package Algorithm.leetcode.LinkedList; + +public class No203 { + public ListNode removeElements(ListNode head, int val) { + ListNode prehead = new ListNode(-1); + prehead.next = head; + ListNode cur = head, pre = prehead; + while (cur != null) { + if (cur.val == val) { + pre.next = cur.next; + } else { + pre = pre.next; + } + cur = cur.next; + } + return prehead.next; + } } diff --git a/leetcode/LinkedList/No206.java b/leetcode/LinkedList/No206.java index 43a2be3..966a45f 100644 --- a/leetcode/LinkedList/No206.java +++ b/leetcode/LinkedList/No206.java @@ -1,8 +1,8 @@ -package LinkedList; +package Algorithm.leetcode.LinkedList; public class No206 { // 迭代 - public ListNode reverseList(ListNode head) { + public ListNode reverseList02(ListNode head) { if (head == null || head.next == null) { return head; } @@ -27,25 +27,25 @@ public ListNode reverseList01(ListNode head) { head.next = null; return newList; } - //三指针解决、 + + // 三指针解决、 public ListNode reverseList(ListNode head) { - if(head == null){ - return null; - } - ListNode current = head; - ListNode pre = null; - ListNode newHead = null; - while(current != null){ - ListNode next = current.next; - if(next == null){ - newHead = current; - } - current.next = pre; - pre = current; - current = next; - } - return newHead; - } + if (head == null) { + return null; + } + ListNode current = head; + ListNode pre = null; + ListNode newHead = null; + while (current != null) { + ListNode next = current.next; + if (next == null) { + newHead = current; + } + current.next = pre; + pre = current; + current = next; + } + return newHead; + } - } diff --git a/leetcode/LinkedList/No21.java b/leetcode/LinkedList/No21.java index ac559e9..4673db9 100644 --- a/leetcode/LinkedList/No21.java +++ b/leetcode/LinkedList/No21.java @@ -1,4 +1,4 @@ -package LinkedList; +package Algorithm.leetcode.LinkedList; class ListNode { int val; diff --git a/leetcode/LinkedList/No234.java b/leetcode/LinkedList/No234.java index b1fe6cd..139e99a 100644 --- a/leetcode/LinkedList/No234.java +++ b/leetcode/LinkedList/No234.java @@ -1,4 +1,4 @@ -package LinkedList; +package Algorithm.leetcode.LinkedList; //回文链表1->2->2->1 public class No234 { diff --git a/leetcode/LinkedList/No237.java b/leetcode/LinkedList/No237.java index f988b36..00a9c0c 100644 --- a/leetcode/LinkedList/No237.java +++ b/leetcode/LinkedList/No237.java @@ -1,4 +1,4 @@ -package LinkedList; +package Algorithm.leetcode.LinkedList; public class No237 { public void deleteNode(ListNode node) { diff --git a/leetcode/LinkedList/No83.java b/leetcode/LinkedList/No83.java index ee2d9a0..44079bc 100644 --- a/leetcode/LinkedList/No83.java +++ b/leetcode/LinkedList/No83.java @@ -1,24 +1,22 @@ +package Algorithm.leetcode.LinkedList; + /** - * Definition for singly-linked list. - * public class ListNode { - * int val; - * ListNode next; - * ListNode(int x) { val = x; } - * } + * Definition for singly-linked list. public class ListNode { int val; ListNode + * next; ListNode(int x) { val = x; } } */ class Solution { - public ListNode deleteDuplicates(ListNode head) { - ListNode list = head; - while(list != null){ - if(list.next == null){ - break; - } - if(list.val == list.next.val){ - list.next = list.next.next; - }else{ - list = list.next; - } - } - return head; - } + public ListNode deleteDuplicates(ListNode head) { + ListNode list = head; + while (list != null) { + if (list.next == null) { + break; + } + if (list.val == list.next.val) { + list.next = list.next.next; + } else { + list = list.next; + } + } + return head; + } } From 8e6c63e5b31492c30abd60610537c449fe99c8c8 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 19:05:47 +0800 Subject: [PATCH 004/116] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/math/No07.java | 28 ++++++++++-------- leetcode/math/No09.java | 36 +++++++++++----------- leetcode/math/No13.java | 60 +++++++++++++++++++++++++------------ leetcode/math/No136.java | 64 +++++++++++++++++++++------------------- leetcode/math/No168.java | 4 ++- leetcode/math/No171.java | 7 ++--- leetcode/math/No172.java | 17 ++++++----- 7 files changed, 123 insertions(+), 93 deletions(-) diff --git a/leetcode/math/No07.java b/leetcode/math/No07.java index 0c86623..f731806 100644 --- a/leetcode/math/No07.java +++ b/leetcode/math/No07.java @@ -1,14 +1,16 @@ -class Solution { - public int reverse(int x) { - //321 - long a = 0; - while(x != 0){ - a = a*10 + x%10; - x = x/10; - } - if(a > Integer.MAX_VALUE || a < Integer.MIN_VALUE){ - return 0; - } - return (int)a; - } +package Algorithm.leetcode.math; + +public class No07 { + public int reverse(int x) { + // 321 + long a = 0; + while (x != 0) { + a = a * 10 + x % 10; + x = x / 10; + } + if (a > Integer.MAX_VALUE || a < Integer.MIN_VALUE) { + return 0; + } + return (int) a; + } } diff --git a/leetcode/math/No09.java b/leetcode/math/No09.java index 6fdba34..33d7cc2 100644 --- a/leetcode/math/No09.java +++ b/leetcode/math/No09.java @@ -1,18 +1,20 @@ -class Solution { - public boolean isPalindrome(int x) { - if(x < 0){ - return false; - } - int b = x; - int a = 0; - while(x != 0){ - a = a*10 + x%10; - x = x /10; - } - if(a == b){ - return true; - }else{ - return false; - } - } +package Algorithm.leetcode.math; + +public class No09 { + public boolean isPalindrome(int x) { + if (x < 0) { + return false; + } + int b = x; + int a = 0; + while (x != 0) { + a = a * 10 + x % 10; + x = x / 10; + } + if (a == b) { + return true; + } else { + return false; + } + } } diff --git a/leetcode/math/No13.java b/leetcode/math/No13.java index ed00aef..be1e0a2 100644 --- a/leetcode/math/No13.java +++ b/leetcode/math/No13.java @@ -1,20 +1,42 @@ -class Solution { - public int romanToInt(String s) { - int sum = 0; - if(s.indexOf("IV") != -1 || s.indexOf("IX") != -1){sum -= 2;} - if(s.indexOf("XL") != -1 || s.indexOf("XC") != -1){sum -= 20;} - if(s.indexOf("CD") != -1 || s.indexOf("CM") != -1){sum -= 200;} - char[] ch = s.toCharArray(); - - for(int i = 0; i < ch.length; i++){ - if(ch[i] == 'I'){sum += 1;} - if(ch[i] == 'V'){sum += 5;} - if(ch[i] == 'X'){sum += 10;} - if(ch[i] == 'L'){sum += 50;} - if(ch[i] == 'C'){sum += 100;} - if(ch[i] == 'D'){sum += 500;} - if(ch[i] == 'M'){sum += 1000;} - } - return sum; - } +package Algorithm.leetcode.math; + +public class No13 { + public int romanToInt(String s) { + int sum = 0; + if (s.indexOf("IV") != -1 || s.indexOf("IX") != -1) { + sum -= 2; + } + if (s.indexOf("XL") != -1 || s.indexOf("XC") != -1) { + sum -= 20; + } + if (s.indexOf("CD") != -1 || s.indexOf("CM") != -1) { + sum -= 200; + } + char[] ch = s.toCharArray(); + + for (int i = 0; i < ch.length; i++) { + if (ch[i] == 'I') { + sum += 1; + } + if (ch[i] == 'V') { + sum += 5; + } + if (ch[i] == 'X') { + sum += 10; + } + if (ch[i] == 'L') { + sum += 50; + } + if (ch[i] == 'C') { + sum += 100; + } + if (ch[i] == 'D') { + sum += 500; + } + if (ch[i] == 'M') { + sum += 1000; + } + } + return sum; + } } diff --git a/leetcode/math/No136.java b/leetcode/math/No136.java index 9a264a0..6b1d43e 100644 --- a/leetcode/math/No136.java +++ b/leetcode/math/No136.java @@ -1,31 +1,35 @@ -class Solution { - public int singleNumber(int[] nums) { - if(nums.length == 1){ - return nums[0]; - } - Arrays.sort(nums); - for(int i = 0;i < nums.length;i++){ - if(i == 0){ - if(nums[0] != nums[1]){ - return nums[0]; - } - }else if(i == nums.length - 1){ - if(nums[nums.length - 2] != nums[nums.length - 1]){ - return nums[nums.length - 1]; - } - }else if(nums[i] != nums[i-1] && nums[i] != nums[i+1]){ - return nums[i]; - } - } - return 0; - } -} -class OtherSolution { - public int singleNumber(int[] nums) { - int sum = 0; - for(int i = 0;i < nums.length;i++){ - sum ^= nums[i]; - } - return sum; - } +package Algorithm.leetcode.math; + +import java.util.Arrays; + +public class No136 { + + public int singleNumber02(int[] nums) { + int sum = 0; + for (int i = 0; i < nums.length; i++) { + sum ^= nums[i]; + } + return sum; + } + + public int singleNumber(int[] nums) { + if (nums.length == 1) { + return nums[0]; + } + Arrays.sort(nums); + for (int i = 0; i < nums.length; i++) { + if (i == 0) { + if (nums[0] != nums[1]) { + return nums[0]; + } + } else if (i == nums.length - 1) { + if (nums[nums.length - 2] != nums[nums.length - 1]) { + return nums[nums.length - 1]; + } + } else if (nums[i] != nums[i - 1] && nums[i] != nums[i + 1]) { + return nums[i]; + } + } + return 0; + } } diff --git a/leetcode/math/No168.java b/leetcode/math/No168.java index 8e0dbe2..16407e0 100644 --- a/leetcode/math/No168.java +++ b/leetcode/math/No168.java @@ -1,4 +1,6 @@ -class No168 { +package Algorithm.leetcode.math; + +public class No168 { public String convertToTitle(int n) { if (n < 1) { return ""; diff --git a/leetcode/math/No171.java b/leetcode/math/No171.java index f9599dd..81885b8 100644 --- a/leetcode/math/No171.java +++ b/leetcode/math/No171.java @@ -1,8 +1,5 @@ -/** - * - * @author tujietg - * - */ +package Algorithm.leetcode.math; + public class No171 { public int titleToNumber(String s) { diff --git a/leetcode/math/No172.java b/leetcode/math/No172.java index 9c272de..6f0b609 100644 --- a/leetcode/math/No172.java +++ b/leetcode/math/No172.java @@ -1,11 +1,12 @@ +package Algorithm.leetcode.math; public class No172 { - public int trailingZeroes(int n) { - int res = 0; - while(n != 0){ - res+=n/5; - n=n/5; - } - return res; - } + public int trailingZeroes(int n) { + int res = 0; + while (n != 0) { + res += n / 5; + n = n / 5; + } + return res; + } } From b5242f5a3cd3aa901f2216625e51d8bcdc57af7e Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 19:06:24 +0800 Subject: [PATCH 005/116] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/String/No125.java | 52 ++++++++++++++++++----------------- leetcode/String/No20.java | 36 ++++++++++++++----------- leetcode/String/No205.java | 55 +++++++++++++++++++++----------------- leetcode/String/No28.java | 55 +++++++++++++++++++++----------------- leetcode/String/No58.java | 18 +++++++------ 5 files changed, 117 insertions(+), 99 deletions(-) diff --git a/leetcode/String/No125.java b/leetcode/String/No125.java index 8884d1f..b15333f 100644 --- a/leetcode/String/No125.java +++ b/leetcode/String/No125.java @@ -1,26 +1,28 @@ -class Solution { - public boolean isPalindrome(String s) { - if(s.isEmpty()){ - return true; - } - int head = 0, end = s.length() - 1; - char chead ; - char cend ; - while(head <= end){ - chead = s.charAt(head); - cend = s.charAt(end); - if(!Character.isLetterOrDigit(chead)){ - head++; - }else if(!Character.isLetterOrDigit(cend)){ - end--; - }else{ - if(Character.toLowerCase(chead) != Character.toLowerCase(cend)){ - return false; - } - head++; - end--; - } - } - return true; - } +package Algorithm.leetcode.String; + +public class No125 { + public boolean isPalindrome(String s) { + if (s.isEmpty()) { + return true; + } + int head = 0, end = s.length() - 1; + char chead; + char cend; + while (head <= end) { + chead = s.charAt(head); + cend = s.charAt(end); + if (!Character.isLetterOrDigit(chead)) { + head++; + } else if (!Character.isLetterOrDigit(cend)) { + end--; + } else { + if (Character.toLowerCase(chead) != Character.toLowerCase(cend)) { + return false; + } + head++; + end--; + } + } + return true; + } } diff --git a/leetcode/String/No20.java b/leetcode/String/No20.java index 680cd8d..185d144 100644 --- a/leetcode/String/No20.java +++ b/leetcode/String/No20.java @@ -1,17 +1,21 @@ -class Solution { - public boolean isValid(String s) { - Stack stack = new Stack(); - for(char c : s.toCharArray()){ - if(c == '('){ - stack.push(')'); - }else if(c == '{'){ - stack.push('}'); - }else if(c == '['){ - stack.push(']'); - }else if(stack.isEmpty() || stack.pop() != c){ - return false; - } - } - return stack.isEmpty(); - } +package Algorithm.leetcode.String; + +import java.util.Stack; + +public class No20 { + public boolean isValid(String s) { + Stack stack = new Stack(); + for (char c : s.toCharArray()) { + if (c == '(') { + stack.push(')'); + } else if (c == '{') { + stack.push('}'); + } else if (c == '[') { + stack.push(']'); + } else if (stack.isEmpty() || stack.pop() != c) { + return false; + } + } + return stack.isEmpty(); + } } diff --git a/leetcode/String/No205.java b/leetcode/String/No205.java index e6a159e..3f3b163 100644 --- a/leetcode/String/No205.java +++ b/leetcode/String/No205.java @@ -1,26 +1,31 @@ -class Solution { - public boolean isIsomorphic(String s, String t) { - if(s.length() <= 1 || s == null){ - return true; - } - Map hashMap = new HashMap(); - for(int i = 0;i < s.length();i++){ - char a = s.charAt(i); - char b = t.charAt(i); - if(hashMap.containsKey(a)){ - if(hashMap.get(a).equals(b)){ - continue; - }else{ - return false; - } - }else{ - if(!hashMap.containsValue(b)){ - hashMap.put(a,b); - }else{ - return false; - } - } - } - return true; - } +package Algorithm.leetcode.String; + +import java.util.HashMap; +import java.util.Map; + +public class No205 { + public boolean isIsomorphic(String s, String t) { + if (s.length() <= 1 || s == null) { + return true; + } + Map hashMap = new HashMap(); + for (int i = 0; i < s.length(); i++) { + char a = s.charAt(i); + char b = t.charAt(i); + if (hashMap.containsKey(a)) { + if (hashMap.get(a).equals(b)) { + continue; + } else { + return false; + } + } else { + if (!hashMap.containsValue(b)) { + hashMap.put(a, b); + } else { + return false; + } + } + } + return true; + } } diff --git a/leetcode/String/No28.java b/leetcode/String/No28.java index 9f0ca90..c42ca29 100644 --- a/leetcode/String/No28.java +++ b/leetcode/String/No28.java @@ -1,26 +1,31 @@ -class Solution { - public int strStr(String haystack, String needle) { - if(needle == "" || needle == null){ - return 0; - } - if(haystack.contains(needle)){ - return haystack.indexOf(needle); - } - return -1; - } +package Algorithm.leetcode.String; + +public class No28 { + + public int strStr(String haystack, String needle) { + if (needle == "" || needle == null) { + return 0; + } + if (haystack.contains(needle)) { + return haystack.indexOf(needle); + } + return -1; + } + + public int strStr02(String haystack, String needle) { + int hayLen = haystack.length(), needLen = needle.length(); + if (hayLen < needLen) { + return -1; + } else if (needLen == 0) { + return 0; + } + int dum = hayLen - needLen; + for (int i = 0; i <= dum; i++) { + if ((haystack.substring(i, i + needLen)).equals(needle)) { + return i; + } + } + return -1; + } + } -public int strStr(String haystack, String needle) { - int hayLen = haystack.length() , needLen = needle.length(); - if(hayLen < needLen){ - return -1; - }else if(needLen == 0){ - return 0; - } - int dum = hayLen - needLen; - for(int i = 0; i <= dum ; i++){ - if((haystack.substring(i,i+needLen)).equals(needle)) { - return i; - } - } - return -1; - } diff --git a/leetcode/String/No58.java b/leetcode/String/No58.java index 67ee3b7..0125033 100644 --- a/leetcode/String/No58.java +++ b/leetcode/String/No58.java @@ -1,9 +1,11 @@ -class Solution { - public int lengthOfLastWord(String s) { - String[] arr = s.split(" "); - if(arr == null || arr.length == 0){ - return 0; - } - return arr[arr.length - 1].length(); - } +package Algorithm.leetcode.String; + +public class No58 { + public int lengthOfLastWord(String s) { + String[] arr = s.split(" "); + if (arr == null || arr.length == 0) { + return 0; + } + return arr[arr.length - 1].length(); + } } From 022f436f04bba45da7ecec7b7f46e5f158f02b5e Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 19:06:42 +0800 Subject: [PATCH 006/116] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Tree/No100.java | 46 +++++++++++++++++++++------------------- leetcode/Tree/No101.java | 44 ++++++++++++++++++-------------------- leetcode/Tree/No104.java | 36 +++++++++++++++++++------------ 3 files changed, 67 insertions(+), 59 deletions(-) diff --git a/leetcode/Tree/No100.java b/leetcode/Tree/No100.java index 7ccf6b3..7b9b5f1 100644 --- a/leetcode/Tree/No100.java +++ b/leetcode/Tree/No100.java @@ -1,24 +1,26 @@ -/** - * Definition for a binary tree node. - * public class TreeNode { - * int val; - * TreeNode left; - * TreeNode right; - * TreeNode(int x) { val = x; } - * } - */ -class Solution { - public boolean isSameTree(TreeNode p, TreeNode q) { - if(p == null && q == null){ - return true; - } - if(p == null || q == null){ - return false; - } - if(p.val == q.val){ - return isSameTree(p.left,q.left) && isSameTree(p.right,q.right); - } - return false; - } +package Algorithm.leetcode.Tree; + +class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } } +public class No100 { + public boolean isSameTree(TreeNode p, TreeNode q) { + if (p == null && q == null) { + return true; + } + if (p == null || q == null) { + return false; + } + if (p.val == q.val) { + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); + } + return false; + } +} diff --git a/leetcode/Tree/No101.java b/leetcode/Tree/No101.java index 40befb2..eae209d 100644 --- a/leetcode/Tree/No101.java +++ b/leetcode/Tree/No101.java @@ -1,26 +1,24 @@ +package Algorithm.leetcode.Tree; + /** - * Definition for a binary tree node. - * public class TreeNode { - * int val; - * TreeNode left; - * TreeNode right; - * TreeNode(int x) { val = x; } - * } + * Definition for a binary tree node. public class TreeNode { int val; TreeNode + * left; TreeNode right; TreeNode(int x) { val = x; } } */ -class Solution { - public boolean isSymmetric(TreeNode root) { - if(root == null){ - return true; - } - return isM(root.left,root.right); - } - public boolean isM(TreeNode r1,TreeNode r2){ - if(r1 == null && r2 == null){ - return true; - } - if((r1 == null && r2 != null) || (r1 != null && r2 == null) ){ - return false; - } - return (r1.val == r2.val && isM(r1.left,r2.right) && isM(r1.right,r2.left)); - } +public class No101 { + public boolean isSymmetric(TreeNode root) { + if (root == null) { + return true; + } + return isM(root.left, root.right); + } + + public boolean isM(TreeNode r1, TreeNode r2) { + if (r1 == null && r2 == null) { + return true; + } + if ((r1 == null && r2 != null) || (r1 != null && r2 == null)) { + return false; + } + return (r1.val == r2.val && isM(r1.left, r2.right) && isM(r1.right, r2.left)); + } } diff --git a/leetcode/Tree/No104.java b/leetcode/Tree/No104.java index 4d175d2..f9faa1b 100644 --- a/leetcode/Tree/No104.java +++ b/leetcode/Tree/No104.java @@ -1,17 +1,25 @@ +package Algorithm.leetcode.Tree; + /** - * Definition for a binary tree node. - * public class TreeNode { - * int val; - * TreeNode left; - * TreeNode right; - * TreeNode(int x) { val = x; } - * } + * Definition for a binary tree node. public class TreeNode { int val; TreeNode + * left; TreeNode right; TreeNode(int x) { val = x; } } */ -class Solution { - public int maxDepth(TreeNode root) { - if(root == null){ - return 0; - } - return 1 + Math.max(maxDepth(root.left),(root.right)); - } + +class TreeNodeInt { + int val; + TreeNodeInt left; + TreeNodeInt right; + + TreeNodeInt(int x) { + val = x; + } +} + +public class No104 { + public int maxDepth(TreeNodeInt root) { + if (root == null) { + return 0; + } + return 1 + Math.max(maxDepth(root.left), (root.right)); + } } From 6fc58e134acb96dd17ece2c22bba1a79f00ef88b Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 19:08:17 +0800 Subject: [PATCH 007/116] =?UTF-8?q?=E6=95=B4=E7=90=86LeetCode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c3429b2..b4ae4f7 100755 --- a/README.md +++ b/README.md @@ -1,10 +1,12 @@ # Algorithm -I like to brush leetcode, it is a way of my pastime. +🚴‍♀️I like to brush leetcode, it is a way of my pastime. -I really **enjoy** it, I will always update it. +🚴‍I really **enjoy** it, I will always update it. + +### LeetCode + +---- -🎉🎉🎉 Started at 2018.9.11 -🙈🙈🙈Restart at 2019.01.14 From 9dd54813baf91d122cd47e32772ba0bd6f66c52b Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 21:03:37 +0800 Subject: [PATCH 008/116] =?UTF-8?q?=E6=99=9A=E4=B8=8Areadme.me?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index b4ae4f7..9aa585a 100755 --- a/README.md +++ b/README.md @@ -4,9 +4,19 @@ 🚴‍I really **enjoy** it, I will always update it. +刷题进阶时间线 + +#### Note + +算法复杂度和时间复杂度 + ### LeetCode ---- +#### Array +| # | Title | Solution | Time | Space | Note | +| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ----- | ---- | +| No01 | Two Sum | java | O(n) | O(n) | | From 69026bc98d0ecb56408defd1b5b09ef727d43144 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 21:05:43 +0800 Subject: [PATCH 009/116] =?UTF-8?q?=E6=99=9A=E4=B8=8Areadme.me?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9aa585a..3796330 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 🚴‍I really **enjoy** it, I will always update it. -刷题进阶时间线 +刷题进阶时间线 #### Note -算法复杂度和时间复杂度 +算法复杂度和时间复杂度 ### LeetCode From ffb2b35e6b1b744bd8a79f8527d08123962778ac Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 21:08:08 +0800 Subject: [PATCH 010/116] =?UTF-8?q?=E6=99=9A=E4=B8=8Areadme.me?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3796330..979ce31 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 🚴‍I really **enjoy** it, I will always update it. -刷题进阶时间线 +[刷题进阶时间线](https://gist.github.com/tujietg/60f72073a1c7945350abb8b09e2d6455) #### Note -算法复杂度和时间复杂度 +[算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) ### LeetCode @@ -16,7 +16,7 @@ #### Array -| # | Title | Solution | Time | Space | Note | -| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ----- | ---- | -| No01 | Two Sum | java | O(n) | O(n) | | +| # | Title | Solution | Time | Space | Note | +| ---- | ------------------------------------------------- | ------------------------------------------------------------ | ---- | ----- | ---- | +| No01 | [Two Sum](https://leetcode.com/problems/two-sum/) | [java](https://github.com/tujietg/Algorithm/blob/master/leetcode/Array/No01.java) | O(n) | O(n) | | From 3a7adb0bdb2d0ddbeb1007a5d7e7a72b03c347fc Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 21:09:14 +0800 Subject: [PATCH 011/116] =?UTF-8?q?=E8=A1=A5=E5=85=85readme.me?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 979ce31..0e5c944 100755 --- a/README.md +++ b/README.md @@ -6,7 +6,9 @@ [刷题进阶时间线](https://gist.github.com/tujietg/60f72073a1c7945350abb8b09e2d6455) -#### Note +### Note + +----- [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) From c2b0de7d1cc26ce1c20e9be51c3f570465b2a482 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 3 Jun 2019 21:24:18 +0800 Subject: [PATCH 012/116] =?UTF-8?q?=E8=A1=A5=E5=85=85readme.me?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0e5c944..842bb6b 100755 --- a/README.md +++ b/README.md @@ -4,13 +4,13 @@ 🚴‍I really **enjoy** it, I will always update it. -[刷题进阶时间线](https://gist.github.com/tujietg/60f72073a1c7945350abb8b09e2d6455) +- [刷题进阶时间线](https://gist.github.com/tujietg/60f72073a1c7945350abb8b09e2d6455) ### Note ----- -[算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) +- [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) ### LeetCode From fce248b4916196ae77b383adf01595c7f7472bef Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 4 Jun 2019 09:35:53 +0800 Subject: [PATCH 013/116] =?UTF-8?q?=E8=A1=A5=E5=85=85=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 842bb6b..52cd940 100755 --- a/README.md +++ b/README.md @@ -8,17 +8,13 @@ ### Note ------ - - [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) ### LeetCode ----- - #### Array -| # | Title | Solution | Time | Space | Note | -| ---- | ------------------------------------------------- | ------------------------------------------------------------ | ---- | ----- | ---- | -| No01 | [Two Sum](https://leetcode.com/problems/two-sum/) | [java](https://github.com/tujietg/Algorithm/blob/master/leetcode/Array/No01.java) | O(n) | O(n) | | +| # | Title | Solution | Time | Space | Note | +| ---- | ------------------------------------------------- | ------------------------------------------------------------ | ---- | ----- | ------------------------------------------------- | +| No01 | [Two Sum](https://leetcode.com/problems/two-sum/) | [java](https://github.com/tujietg/Algorithm/blob/master/leetcode/Array/No01.java) | O(n) | O(n) | 采用map存储值和下标,然后再次遍历,判断得到结果。 | From 4d6e3afbd60b1b766566ea080b14b35cb7b1d0ae Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 4 Jun 2019 09:37:21 +0800 Subject: [PATCH 014/116] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 52cd940..9c96a2a 100755 --- a/README.md +++ b/README.md @@ -1,14 +1,12 @@ # Algorithm -🚴‍♀️I like to brush leetcode, it is a way of my pastime. +🚴‍♀️ I like to brush leetcode, it is a way of my pastime. -🚴‍I really **enjoy** it, I will always update it. +🚴‍ I really **enjoy** it, I will always update it. -- [刷题进阶时间线](https://gist.github.com/tujietg/60f72073a1c7945350abb8b09e2d6455) +⌚️ [刷题进阶时间线](https://gist.github.com/tujietg/60f72073a1c7945350abb8b09e2d6455) -### Note - -- [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) +📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) ### LeetCode From 729de51aef914a69a500d4cf6df848624cac712f Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 4 Jun 2019 09:50:56 +0800 Subject: [PATCH 015/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No26?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9c96a2a..62fffb2 100755 --- a/README.md +++ b/README.md @@ -12,7 +12,8 @@ #### Array -| # | Title | Solution | Time | Space | Note | -| ---- | ------------------------------------------------- | ------------------------------------------------------------ | ---- | ----- | ------------------------------------------------- | -| No01 | [Two Sum](https://leetcode.com/problems/two-sum/) | [java](https://github.com/tujietg/Algorithm/blob/master/leetcode/Array/No01.java) | O(n) | O(n) | 采用map存储值和下标,然后再次遍历,判断得到结果。 | +| # | Title | Solution | Time | Space | Note | +| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ----- | ------------------------------------------------------------ | +| No01 | [Two Sum](https://leetcode.com/problems/two-sum/) | [java](https://github.com/tujietg/Algorithm/blob/master/leetcode/Array/No01.java) | O(n) | O(n) | 采用map存储值和下标,然后再次遍历,判断得到结果。 | +| No26 | [Remove Duplicates from Sorted Array]() | [java]() | O(n) | O(1) | 定义i来记录不相等的个数,因为记录从下标0开始,最后长度需要加1. | From 1c57eb4a53501d95cf5b1fbad5fa7d573afe66bf Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 4 Jun 2019 11:53:18 +0800 Subject: [PATCH 016/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A027?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 62fffb2..a202761 100755 --- a/README.md +++ b/README.md @@ -16,4 +16,5 @@ | ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ----- | ------------------------------------------------------------ | | No01 | [Two Sum](https://leetcode.com/problems/two-sum/) | [java](https://github.com/tujietg/Algorithm/blob/master/leetcode/Array/No01.java) | O(n) | O(n) | 采用map存储值和下标,然后再次遍历,判断得到结果。 | | No26 | [Remove Duplicates from Sorted Array]() | [java]() | O(n) | O(1) | 定义i来记录不相等的个数,因为记录从下标0开始,最后长度需要加1. | +| No27 | [Remove Element]() | [java]() | O(n) | O(1) | 采用变量来记录相同的值。 | From 88777e306b8dff514e60c6bda727ff6eb33e3a64 Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 4 Jun 2019 14:53:44 +0800 Subject: [PATCH 017/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A035?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a202761..4f6f825 100755 --- a/README.md +++ b/README.md @@ -17,4 +17,5 @@ | No01 | [Two Sum](https://leetcode.com/problems/two-sum/) | [java](https://github.com/tujietg/Algorithm/blob/master/leetcode/Array/No01.java) | O(n) | O(n) | 采用map存储值和下标,然后再次遍历,判断得到结果。 | | No26 | [Remove Duplicates from Sorted Array]() | [java]() | O(n) | O(1) | 定义i来记录不相等的个数,因为记录从下标0开始,最后长度需要加1. | | No27 | [Remove Element]() | [java]() | O(n) | O(1) | 采用变量来记录相同的值。 | +| No35 | [Search Insert Position]() | [java]() | O(n) | O(1) | 1⃣ 找到给定数大于数组的元素。2⃣ 遍历,一个一个条件的判断。 | From 7c5160faa9cead8c3c1c23eadfc4090781e339af Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 4 Jun 2019 15:11:16 +0800 Subject: [PATCH 018/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No53?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4f6f825..87d62b8 100755 --- a/README.md +++ b/README.md @@ -18,4 +18,5 @@ | No26 | [Remove Duplicates from Sorted Array]() | [java]() | O(n) | O(1) | 定义i来记录不相等的个数,因为记录从下标0开始,最后长度需要加1. | | No27 | [Remove Element]() | [java]() | O(n) | O(1) | 采用变量来记录相同的值。 | | No35 | [Search Insert Position]() | [java]() | O(n) | O(1) | 1⃣ 找到给定数大于数组的元素。2⃣ 遍历,一个一个条件的判断。 | +| No53 | [Maximum Subarray]() | [java]() | O(n) | O(1) | 设置max字段的初始值为最小的整数。 | From b7190365ceeb587837b2a2ddddee1a17c56965f0 Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 11 Jun 2019 21:16:09 +0800 Subject: [PATCH 019/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 87d62b8..3bd7b54 100755 --- a/README.md +++ b/README.md @@ -19,4 +19,8 @@ | No27 | [Remove Element]() | [java]() | O(n) | O(1) | 采用变量来记录相同的值。 | | No35 | [Search Insert Position]() | [java]() | O(n) | O(1) | 1⃣ 找到给定数大于数组的元素。2⃣ 遍历,一个一个条件的判断。 | | No53 | [Maximum Subarray]() | [java]() | O(n) | O(1) | 设置max字段的初始值为最小的整数。 | +| No66 | [Plus One]() | [java]() | O(n) | O(n) | 该算法解法中做了合适的小于9的判断。 | +| | | | | | | +| | | | | | | +| | | | | | | From 6b5ad1c0ac736df84c08b28864d75a320a62f4d8 Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 12 Jun 2019 20:14:00 +0800 Subject: [PATCH 020/116] =?UTF-8?q?=E5=AE=8C=E6=88=9010=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3bd7b54..0325756 100755 --- a/README.md +++ b/README.md @@ -12,15 +12,15 @@ #### Array -| # | Title | Solution | Time | Space | Note | -| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ----- | ------------------------------------------------------------ | -| No01 | [Two Sum](https://leetcode.com/problems/two-sum/) | [java](https://github.com/tujietg/Algorithm/blob/master/leetcode/Array/No01.java) | O(n) | O(n) | 采用map存储值和下标,然后再次遍历,判断得到结果。 | -| No26 | [Remove Duplicates from Sorted Array]() | [java]() | O(n) | O(1) | 定义i来记录不相等的个数,因为记录从下标0开始,最后长度需要加1. | -| No27 | [Remove Element]() | [java]() | O(n) | O(1) | 采用变量来记录相同的值。 | -| No35 | [Search Insert Position]() | [java]() | O(n) | O(1) | 1⃣ 找到给定数大于数组的元素。2⃣ 遍历,一个一个条件的判断。 | -| No53 | [Maximum Subarray]() | [java]() | O(n) | O(1) | 设置max字段的初始值为最小的整数。 | -| No66 | [Plus One]() | [java]() | O(n) | O(n) | 该算法解法中做了合适的小于9的判断。 | -| | | | | | | -| | | | | | | -| | | | | | | +| # | Title | Solution | Time | Space | Note | +| ----- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ----- | ------------------------------------------------------------ | +| No01 | [Two Sum](https://leetcode.com/problems/two-sum/) | [java](https://github.com/tujietg/Algorithm/blob/master/leetcode/Array/No01.java) | O(n) | O(n) | 采用map存储值和下标,然后再次遍历,判断得到结果。 | +| No26 | [Remove Duplicates from Sorted Array]() | [java]() | O(n) | O(1) | 定义i来记录不相等的个数,因为记录从下标0开始,最后长度需要加1. | +| No27 | [Remove Element]() | [java]() | O(n) | O(1) | 采用变量来记录相同的值。 | +| No35 | [Search Insert Position]() | [java]() | O(n) | O(1) | 1⃣ 找到给定数大于数组的元素。2⃣ 遍历,一个一个条件的判断。 | +| No53 | [Maximum Subarray]() | [java]() | O(n) | O(1) | 设置max字段的初始值为最小的整数。 | +| No66 | [Plus One]() | [java]() | O(n) | O(n) | 该算法解法中做了合适的小于9的判断。 | +| No88 | [Merge Sorted Array]() | [java]() | O(n) | O(1) | 插入之后采用Arrays.sort()排序。 | +| No121 | [Best Time to Buy and Sell Stock]() | [java]() | O(n) | O(1) | 一次死循环,利用中间值做判断。 | +| No122 | [Best Time to Buy and Sell Stock II]() | [java]() | O(n) | O(1) | 一次循环,判断后值比前值大。 | From ee054e9bd3f3cde435c90ec1b660949d00ea74ef Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 29 Jul 2019 15:46:13 +0800 Subject: [PATCH 021/116] dp --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 0325756..102bca5 100755 --- a/README.md +++ b/README.md @@ -24,3 +24,11 @@ | No121 | [Best Time to Buy and Sell Stock]() | [java]() | O(n) | O(1) | 一次死循环,利用中间值做判断。 | | No122 | [Best Time to Buy and Sell Stock II]() | [java]() | O(n) | O(1) | 一次循环,判断后值比前值大。 | + + +#### Dynamic Programming + +| # | Title | Solution | Time | Space | Note | +| ----- | ------------------------------------------------------------ | -------- | ---- | ----- | ---------------------- | +| No303 | [Range Sum Query](https://leetcode.com/problems/range-sum-query-immutable/) | [java]() | O(1) | O(n) | sumRange被频繁的调用。 | + From 40b02e7c287e756b32b72bb630e173168c72d086 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 29 Jul 2019 15:46:55 +0800 Subject: [PATCH 022/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0303?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Array/No01.java | 2 +- leetcode/Array/No66.java | 2 +- leetcode/DP/No303.java | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 leetcode/DP/No303.java diff --git a/leetcode/Array/No01.java b/leetcode/Array/No01.java index 14aaaa2..3a1d76c 100644 --- a/leetcode/Array/No01.java +++ b/leetcode/Array/No01.java @@ -4,7 +4,7 @@ public class No01 { public int[] twoSum(int[] nums, int target) { - HashMap map = new HashMap(); + HashMap map = new HashMap(); for (int i = 0; i < nums.length; i++) { map.put(nums[i], i); } diff --git a/leetcode/Array/No66.java b/leetcode/Array/No66.java index bc8af87..40f3f9c 100644 --- a/leetcode/Array/No66.java +++ b/leetcode/Array/No66.java @@ -1,7 +1,7 @@ package Algorithm.leetcode.Array; public class No66 { - public int[] plusOne(int[] digits) { + public static int[] plusOne(int[] digits) { for (int i = digits.length - 1; i >= 0; i--) { if (digits[i] < 9) { digits[i]++; diff --git a/leetcode/DP/No303.java b/leetcode/DP/No303.java new file mode 100644 index 0000000..e7ce9e9 --- /dev/null +++ b/leetcode/DP/No303.java @@ -0,0 +1,30 @@ +package Algorithm.leetcode.DP; + +/** + * 分析:sumRange被频繁的调用,这里采用构造方法时候就算法0到每个元素的值时间复杂度为O(n),这样sumRange方法每次调用的时间复杂度为常数。 + * + * Created by tujietg on Jul 29, 2019 + */ +public class No303 { + + class NumArray { + + private int[] nums; + + public NumArray(int[] nums) { + // 计算0到每个位数的和 + for (int i = 1; i < nums.length; i++) { + nums[i] += nums[i - 1]; + } + + this.nums = nums; + } + + public int sumRange(int i, int j) { + if (i == 0) + return nums[j]; + return nums[j] - nums[i - 1]; + } + } + +} From 76c6f0a02b9d583045fc2ce00508c93f4bd8afbc Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 1 Aug 2019 16:16:36 +0800 Subject: [PATCH 023/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0dp=E8=BF=98=E6=9C=89S?= =?UTF-8?q?tring=E7=9A=84=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 102bca5..6beb5a2 100755 --- a/README.md +++ b/README.md @@ -28,7 +28,17 @@ #### Dynamic Programming -| # | Title | Solution | Time | Space | Note | -| ----- | ------------------------------------------------------------ | -------- | ---- | ----- | ---------------------- | -| No303 | [Range Sum Query](https://leetcode.com/problems/range-sum-query-immutable/) | [java]() | O(1) | O(n) | sumRange被频繁的调用。 | +| # | Title | Solution | Time | Space | Note | +| ------ | ------------------------------------------------------------ | -------- | ---- | ----- | ------------------------------------ | +| No303 | [Range Sum Query](https://leetcode.com/problems/range-sum-query-immutable/) | [java]() | O(1) | O(n) | sumRange被频繁的调用。 | +| No746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [java]() | O(N) | O(n) | 先计算到达每个楼梯最小数字,在做减法 | +| No1025 | [Divisor Game](https://leetcode.com/problems/divisor-game/) | [java]() | O(1) | O(1) | | + + + +####String + +| # | Title | Solution | Time | Space | Note | +| ---- | ------------------------------------------------------- | -------- | ---- | ----- | ---------------------- | +| No67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [java]() | O(N) | O(1) | 相当于自己做了加法运算 | From 9f423c6b8c0d71623465b11848659e8807903b11 Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 1 Aug 2019 16:17:02 +0800 Subject: [PATCH 024/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/DP/No1025.java | 11 +++++++++++ leetcode/DP/No746.java | 23 +++++++++++++++++++++++ leetcode/String/No67.java | 28 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 leetcode/DP/No1025.java create mode 100644 leetcode/DP/No746.java create mode 100644 leetcode/String/No67.java diff --git a/leetcode/DP/No1025.java b/leetcode/DP/No1025.java new file mode 100644 index 0000000..155d311 --- /dev/null +++ b/leetcode/DP/No1025.java @@ -0,0 +1,11 @@ +package Algorithm.leetcode.DP; + +/** + * + * Created by tujietg on Aug 1, 2019 + */ +public class No1025 { + public boolean divisorGame(int N) { + return N % 2 == 0; + } +} diff --git a/leetcode/DP/No746.java b/leetcode/DP/No746.java new file mode 100644 index 0000000..57a1f7e --- /dev/null +++ b/leetcode/DP/No746.java @@ -0,0 +1,23 @@ +package Algorithm.leetcode.DP; + +/** + * + * Created by tujietg on Aug 1, 2019 + */ +public class No746 { + public int minCostClimbingStairs(int[] cost) { + if (cost.length == 0) + return 0; + + int[] dp = new int[cost.length + 1]; + + dp[0] = cost[0]; + dp[1] = cost[1]; + for (int i = 2; i < cost.length; i++) { + dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i]; + } + + dp[cost.length] = Math.min(dp[cost.length - 1], dp[cost.length - 2]) + dp[cost.length]; + return dp[cost.length]; + } +} diff --git a/leetcode/String/No67.java b/leetcode/String/No67.java new file mode 100644 index 0000000..796f512 --- /dev/null +++ b/leetcode/String/No67.java @@ -0,0 +1,28 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Aug 1, 2019 + */ +public class No67 { + + public String addBinary(String a, String b) { + + StringBuffer sb = new StringBuffer(); + + int i = a.length() - 1, j = b.length() - 1, carry = 0; + while (i >= 0 || j >= 0) { + int sum = carry; + if (i >= 0) + sum += a.charAt(i--) - '0'; + if (j >= 0) + sum += b.charAt(j--) - '0'; + sb.append(sum % 2); + carry = sum / 2; + } + if (carry != 0) + sb.append(carry); + return sb.reverse().toString(); + } + +} From 173e95a947a5f344dc6c8fe9c7fa706a886f8c4f Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 2 Aug 2019 10:18:08 +0800 Subject: [PATCH 025/116] =?UTF-8?q?=E8=A7=A3=E5=86=B3String=E9=A2=98?= =?UTF-8?q?=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6beb5a2..a932039 100755 --- a/README.md +++ b/README.md @@ -38,7 +38,8 @@ ####String -| # | Title | Solution | Time | Space | Note | -| ---- | ------------------------------------------------------- | -------- | ---- | ----- | ---------------------- | -| No67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [java]() | O(N) | O(1) | 相当于自己做了加法运算 | +| # | Title | Solution | Time | Space | Note | +| ----- | ------------------------------------------------------------ | -------- | ---- | ----- | ---------------------- | +| No67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [java]() | O(N) | O(1) | 相当于自己做了加法运算 | +| No344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [java]() | O(N) | O(1) | 利用中间值进行修改。 | From 3efb45dbcb10b05bfef719aa8d19e1ed4050134f Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 2 Aug 2019 10:18:54 +0800 Subject: [PATCH 026/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Stirng=E7=9A=84?= =?UTF-8?q?=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/String/No344.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 leetcode/String/No344.java diff --git a/leetcode/String/No344.java b/leetcode/String/No344.java new file mode 100644 index 0000000..07ac44f --- /dev/null +++ b/leetcode/String/No344.java @@ -0,0 +1,23 @@ +package Algorithm.leetcode.String; + +/** + * 本题注意 空间复杂度为O(1) + * + * Created by tujietg on Aug 2, 2019 + */ +public class No344 { + public void reverseString(char[] s) { + if (s.length == 0 || s.length == 1) { + return; + } + int midLen = s.length / 2; + for (int i = 0; i < s.length; i++) { + if (i < midLen) { + char cent = '0'; + cent = s[i]; + s[i] = s[s.length - 1 - i]; + s[s.length - 1 - i] = cent; + } + } + } +} From e1b9bb8f954ddf38fd771e3f4e6783677e8680ec Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 2 Aug 2019 10:34:47 +0800 Subject: [PATCH 027/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0readme.md=E6=BF=80?= =?UTF-8?q?=E5=8A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index a932039..b841b72 100755 --- a/README.md +++ b/README.md @@ -8,6 +8,10 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) +✊ 不间断刷题天数:5天 + +🐘 最长连续刷题天数:5天 + ### LeetCode #### Array From 4e56d348ff352a40fd981fb4fac60c989d0bba9e Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 23 Oct 2019 15:23:43 +0800 Subject: [PATCH 028/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=95=BF=E9=94=AE?= =?UTF-8?q?=E6=8C=89=E5=85=A5=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- leetcode/String/No20.java | 4 ++++ leetcode/String/No859.java | 41 ++++++++++++++++++++++++++++++++++++++ leetcode/String/No925.java | 23 +++++++++++++++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 leetcode/String/No859.java create mode 100644 leetcode/String/No925.java diff --git a/README.md b/README.md index b841b72..6254fa8 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:5天 +✊ 不间断刷题天数:1天 🐘 最长连续刷题天数:5天 @@ -28,7 +28,7 @@ | No121 | [Best Time to Buy and Sell Stock]() | [java]() | O(n) | O(1) | 一次死循环,利用中间值做判断。 | | No122 | [Best Time to Buy and Sell Stock II]() | [java]() | O(n) | O(1) | 一次循环,判断后值比前值大。 | - +------- #### Dynamic Programming @@ -38,9 +38,9 @@ | No746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [java]() | O(N) | O(n) | 先计算到达每个楼梯最小数字,在做减法 | | No1025 | [Divisor Game](https://leetcode.com/problems/divisor-game/) | [java]() | O(1) | O(1) | | +----- - -####String +#### String | # | Title | Solution | Time | Space | Note | | ----- | ------------------------------------------------------------ | -------- | ---- | ----- | ---------------------- | diff --git a/leetcode/String/No20.java b/leetcode/String/No20.java index 185d144..fe7e4ea 100644 --- a/leetcode/String/No20.java +++ b/leetcode/String/No20.java @@ -18,4 +18,8 @@ public boolean isValid(String s) { } return stack.isEmpty(); } + + public static void main(String[] args) { + System.out.println(); + } } diff --git a/leetcode/String/No859.java b/leetcode/String/No859.java new file mode 100644 index 0000000..b6bdf17 --- /dev/null +++ b/leetcode/String/No859.java @@ -0,0 +1,41 @@ +package Algorithm.leetcode.String; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; + +/** + * + * Created by tujietg on Sep 24, 2019 + */ +public class No859 { + public static void main(String[] args) { + System.out.println(buddyStrings("ab", "ab")); + } + + public static boolean buddyStrings(String A, String B) { + + if (A.length() != B.length()) { + return false; + } + + if (A.equals(B)) { + Set set = new HashSet(); + char[] arrs = A.toCharArray(); + for (char item : arrs) { + set.add(item); + return set.size() < A.length(); + } + } + + ArrayList array = new ArrayList(); + for (int i = 0; i < A.length(); i++) { + if (A.charAt(i) != B.charAt(i)) { + array.add(i); + } + } + return array.size() == 2 && A.charAt(array.get(0)) == B.charAt(array.get(1)) + && B.charAt(array.get(0)) == A.charAt(array.get(1)); + + } +} diff --git a/leetcode/String/No925.java b/leetcode/String/No925.java new file mode 100644 index 0000000..932cf7a --- /dev/null +++ b/leetcode/String/No925.java @@ -0,0 +1,23 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Oct 23, 2019 + */ +public class No925 { + public boolean isLongPressedName(String name, String typed) { + char[] nameChar = name.toCharArray(); + char[] typedChar = typed.toCharArray(); + int i = 0; + for (int k = 0; k < typed.length(); k++) { + if (i < name.length() && nameChar[i] == typedChar[k]) { + i++; + } else { + if (k == 0 || typedChar[k] != typedChar[k - 1]) { + return false; + } + } + } + return i == name.length(); + } +} From e6affcf77da7e8140b0a7fcc28dfb01a5f1e7126 Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 23 Oct 2019 15:29:52 +0800 Subject: [PATCH 029/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=B7=E9=A2=98?= =?UTF-8?q?=E8=BF=9B=E9=98=B6=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 6254fa8..0c09834 100755 --- a/README.md +++ b/README.md @@ -12,6 +12,10 @@ 🐘 最长连续刷题天数:5天 +🧗‍♂️ 已解题目:94道 + +🧗‍♀️ 参加周赛:0次 + ### LeetCode #### Array From 6d563568a0bca1fbf47ce03dd932c8879c76dd48 Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 23 Oct 2019 17:20:23 +0800 Subject: [PATCH 030/116] =?UTF-8?q?=E4=BC=98=E5=8C=96readme.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0c09834..5797e18 100755 --- a/README.md +++ b/README.md @@ -1,10 +1,6 @@ -# Algorithm +# leetcode -🚴‍♀️ I like to brush leetcode, it is a way of my pastime. - -🚴‍ I really **enjoy** it, I will always update it. - -⌚️ [刷题进阶时间线](https://gist.github.com/tujietg/60f72073a1c7945350abb8b09e2d6455) +🚴‍♀️ I like to brush leetcode, it is a way of my pastime. I really **enjoy** it, I will always update it. 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) @@ -14,9 +10,9 @@ 🧗‍♂️ 已解题目:94道 -🧗‍♀️ 参加周赛:0次 +🧗‍♀️ 参加周赛:1次 -### LeetCode +------- #### Array From f4b6ca124a4a52b244f0350e2ec11f1c2d13c7ea Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 24 Oct 2019 14:24:33 +0800 Subject: [PATCH 031/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No110?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/String/No1108.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 leetcode/String/No1108.java diff --git a/leetcode/String/No1108.java b/leetcode/String/No1108.java new file mode 100644 index 0000000..414ec98 --- /dev/null +++ b/leetcode/String/No1108.java @@ -0,0 +1,24 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Oct 24, 2019 + */ +public class No1108 { + public static String defangIPaddr(String address) { + address = address.replace(".", "}"); + String[] addArr = address.split("}"); + String end = ""; + for (int i = 0; i < addArr.length; i++) { + end = end + addArr[i]; + if (i != 3) { + end = end + "[.]"; + } + } + return end; + } + + public static void main(String[] args) { + System.out.println(defangIPaddr("1.1.1.1")); + } +} From aefa2c29f009ec79379366e213b11318d64eee27 Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 24 Oct 2019 15:01:45 +0800 Subject: [PATCH 032/116] =?UTF-8?q?=E4=BF=AE=E6=94=B9readme.md=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- leetcode/String/No1108.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5797e18..920c3fa 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:1天 +✊ 不间断刷题天数:2天 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:94道 +🧗‍♂️ 已解题目:95道 🧗‍♀️ 参加周赛:1次 diff --git a/leetcode/String/No1108.java b/leetcode/String/No1108.java index 414ec98..fffeed4 100644 --- a/leetcode/String/No1108.java +++ b/leetcode/String/No1108.java @@ -6,8 +6,7 @@ */ public class No1108 { public static String defangIPaddr(String address) { - address = address.replace(".", "}"); - String[] addArr = address.split("}"); + String[] addArr = address.split("\\."); String end = ""; for (int i = 0; i < addArr.length; i++) { end = end + addArr[i]; @@ -20,5 +19,6 @@ public static String defangIPaddr(String address) { public static void main(String[] args) { System.out.println(defangIPaddr("1.1.1.1")); + } } From 57f98c7c97702767f981423299bcf1d501183145 Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 24 Oct 2019 18:49:55 +0800 Subject: [PATCH 033/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A01198?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/String/No1198.java | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 leetcode/String/No1198.java diff --git a/README.md b/README.md index 920c3fa..b684d1a 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:95道 +🧗‍♂️ 已解题目:96道 🧗‍♀️ 参加周赛:1次 diff --git a/leetcode/String/No1198.java b/leetcode/String/No1198.java new file mode 100644 index 0000000..c034b1c --- /dev/null +++ b/leetcode/String/No1198.java @@ -0,0 +1,38 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Oct 24, 2019 + */ +public class No1198 { + public int maxNumberOfBalloons(String text) { + int a = 0; + int b = 0; + int o = 0; + int l = 0; + int n = 0; + for (int i = 0; i < text.length(); i++) { + char item = text.charAt(i); + if (item == 'a') { + a = a + 1; + } else if (item == 'b') { + b++; + } else if (item == 'l') { + l++; + } else if (item == 'o') { + o++; + } else if (item == 'n') { + n++; + } else { + } + } + l = l / 2; + o = o / 2; + int minText = 0; + minText = Math.min(a, b); + minText = Math.min(minText, l); + minText = Math.min(minText, o); + minText = Math.min(minText, n); + return minText; + } +} From 65d922d2c2b5d80c57214667e99d67590b9fbdc8 Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 25 Oct 2019 08:49:53 +0800 Subject: [PATCH 034/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A01221=E9=A2=98?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- leetcode/String/No1221.java | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 leetcode/String/No1221.java diff --git a/README.md b/README.md index b684d1a..8457e1a 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:2天 +✊ 不间断刷题天数:3天 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:96道 +🧗‍♂️ 已解题目:97道 🧗‍♀️ 参加周赛:1次 diff --git a/leetcode/String/No1221.java b/leetcode/String/No1221.java new file mode 100644 index 0000000..cf1acc0 --- /dev/null +++ b/leetcode/String/No1221.java @@ -0,0 +1,23 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Oct 25, 2019 + */ +public class No1221 { + public int balancedStringSplit(String s) { + int count = 0; + int end = 0; + for (char item : s.toCharArray()) { + if (item == 'L') { + count++; + } else { + count--; + } + if (count == 0) { + end++; + } + } + return end; + } +} From e4c7db957dc104025c55622b25fec23bae1e430a Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 28 Oct 2019 12:04:58 +0800 Subject: [PATCH 035/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0929?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- leetcode/String/No929.java | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 leetcode/String/No929.java diff --git a/README.md b/README.md index 8457e1a..a258426 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:3天 +✊ 不间断刷题天数:1天 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:97道 +🧗‍♂️ 已解题目:98道 🧗‍♀️ 参加周赛:1次 diff --git a/leetcode/String/No929.java b/leetcode/String/No929.java new file mode 100644 index 0000000..c8026df --- /dev/null +++ b/leetcode/String/No929.java @@ -0,0 +1,21 @@ +package Algorithm.leetcode.String; + +import java.util.HashSet; +import java.util.Set; + +/** + * + * Created by tujietg on Oct 28, 2019 + */ +public class No929 { + public int numUniqueEmails(String[] emails) { + Set hashSet = new HashSet(); + for (String item : emails) { + String[] end = item.split("@"); + String[] start = end[0].split("\\+"); + hashSet.add(start[0].replace(".", "") + "@" + end[1]); + } + return hashSet.size(); + } + +} From 3c18ded043a0f01384db88b49df8e9689c96942a Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 28 Oct 2019 16:06:08 +0800 Subject: [PATCH 036/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A038=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/String/No38.java | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 leetcode/String/No38.java diff --git a/README.md b/README.md index a258426..717a7aa 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:98道 +🧗‍♂️ 已解题目:99道 🧗‍♀️ 参加周赛:1次 diff --git a/leetcode/String/No38.java b/leetcode/String/No38.java new file mode 100644 index 0000000..4ac8771 --- /dev/null +++ b/leetcode/String/No38.java @@ -0,0 +1,26 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Oct 28, 2019 + */ +public class No38 { + public static String countAndSay(int n) { + if (n == 1) { + return "1"; + } + String str = countAndSay(n - 1) + "*"; + char[] charArr = str.toCharArray(); + int count = 1; + String s = ""; + for (int i = 0; i < charArr.length - 1; i++) { + if (charArr[i] == charArr[i + 1]) { + count++; + } else { + s = s + count + charArr[i]; + count = 1; + } + } + return s; + } +} From 4aa1636da2e6fad1ec08b5c481c848383dda9cd7 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 28 Oct 2019 22:42:36 +0800 Subject: [PATCH 037/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No387?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +--- leetcode/String/No387.java | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 leetcode/String/No387.java diff --git a/README.md b/README.md index 717a7aa..91c7ef7 100755 --- a/README.md +++ b/README.md @@ -8,9 +8,7 @@ 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:99道 - -🧗‍♀️ 参加周赛:1次 +🧗‍♂️ 已解题目:100道 ------- diff --git a/leetcode/String/No387.java b/leetcode/String/No387.java new file mode 100644 index 0000000..83823c3 --- /dev/null +++ b/leetcode/String/No387.java @@ -0,0 +1,32 @@ +package Algorithm.leetcode.String; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * Created by tujietg on Oct 28, 2019 + */ +public class No387 { + public static int firstUniqChar(String s) { + Map map = new HashMap(); + char[] ch = s.toCharArray(); + for (int i = 0; i < ch.length; i++) { + if (!map.containsKey(ch[i])) { + map.put(ch[i], 1); + } else { + int a = map.get(ch[i]); + map.remove(ch[i]); + map.put(ch[i], ++a); + } + } + + for (int i = 0; i < ch.length; i++) { + if (map.get(ch[i]) == 1) { + return i; + } + } + return -1; + } + +} From 4de3773845bf6f5350771ab344bd1c4076b23721 Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 29 Oct 2019 09:43:49 +0800 Subject: [PATCH 038/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No819?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/String/No819.java | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 leetcode/String/No819.java diff --git a/leetcode/String/No819.java b/leetcode/String/No819.java new file mode 100644 index 0000000..0656722 --- /dev/null +++ b/leetcode/String/No819.java @@ -0,0 +1,52 @@ +package Algorithm.leetcode.String; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * Created by tujietg on Oct 29, 2019 + */ +public class No819 { + public static String mostCommonWord(String paragraph, String[] banned) { + Map map = new HashMap(); + paragraph = paragraph.toLowerCase(); + paragraph = paragraph.replaceAll("\\pP", " "); + String[] paragraphChar = paragraph.split(" "); + for (int i = 0; i < paragraphChar.length; i++) { + if (map.containsKey(paragraphChar[i])) { + int a = map.get(paragraphChar[i]); + map.remove(paragraphChar[i]); + map.put(paragraphChar[i], ++a); + } else { + if (paragraphChar[i] != null && paragraphChar[i] != "") { + map.put(paragraphChar[i], 1); + } + } + } + + map.remove(""); + + for (int i = 0; i < banned.length; i++) { + if (map.containsKey(banned[i])) { + map.remove(banned[i]); + } + } + + String end = ""; + int mid = 0; + for (int i = 0; i < paragraphChar.length; i++) { + if (map.get(paragraphChar[i]) != null && map.get(paragraphChar[i]) > mid) { + mid = map.get(paragraphChar[i]); + end = paragraphChar[i]; + } + } + return end; + } + + public static void main(String[] args) { + String[] strArr = new String[] { "bob", "hit" }; + System.out.println(mostCommonWord("Bob. hIt, baLl", strArr)); + + } +} From b0419d0daf7360dea3c460770a91e32a948f3cfd Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 29 Oct 2019 11:27:00 +0800 Subject: [PATCH 039/116] =?UTF-8?q?=E6=9B=B4=E6=96=B0readme.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 91c7ef7..c779ff5 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:1天 +✊ 不间断刷题天数:2天 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:100道 +🧗‍♂️ 已解题目:101道 ------- From 209fe4ca21b406348cd754644cdf4115d72298f7 Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 29 Oct 2019 16:25:58 +0800 Subject: [PATCH 040/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0383?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/String/No383.java | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 leetcode/String/No383.java diff --git a/README.md b/README.md index c779ff5..03d6c64 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:101道 +🧗‍♂️ 已解题目:102道 ------- diff --git a/leetcode/String/No383.java b/leetcode/String/No383.java new file mode 100644 index 0000000..05a82a7 --- /dev/null +++ b/leetcode/String/No383.java @@ -0,0 +1,20 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Oct 29, 2019 + */ +public class No383 { + public boolean canConstruct(String ransomNote, String magazine) { + int[] mid = new int[26]; + for (int i = 0; i < magazine.length(); i++) { + mid[magazine.charAt(i) - 'a']++; + } + for (int i = 0; i < ransomNote.length(); i++) { + if (--mid[ransomNote.charAt(i) - 'a'] < 0) { + return false; + } + } + return true; + } +} From 442e1946950d013093fad0a4a43d925b6384c672 Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 30 Oct 2019 08:49:08 +0800 Subject: [PATCH 041/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E9=81=93?= =?UTF-8?q?=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +-- leetcode/String/No1071.java | 26 +++++++++++++++++ leetcode/String/No415.java | 40 +++++++++++++++++++++++++ leetcode/String/No459.java | 23 +++++++++++++++ leetcode/String/No520.java | 33 +++++++++++++++++++++ leetcode/String/No551.java | 25 ++++++++++++++++ leetcode/String/No557.java | 31 ++++++++++++++++++++ leetcode/String/No606.java | 58 +++++++++++++++++++++++++++++++++++++ leetcode/String/No788.java | 19 ++++++++++++ leetcode/Tree/No104.java | 2 +- 10 files changed, 258 insertions(+), 3 deletions(-) create mode 100644 leetcode/String/No1071.java create mode 100644 leetcode/String/No415.java create mode 100644 leetcode/String/No459.java create mode 100644 leetcode/String/No520.java create mode 100644 leetcode/String/No551.java create mode 100644 leetcode/String/No557.java create mode 100644 leetcode/String/No606.java create mode 100644 leetcode/String/No788.java diff --git a/README.md b/README.md index 03d6c64..69aaf48 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:2天 +✊ 不间断刷题天数:3天 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:102道 +🧗‍♂️ 已解题目:104道 ------- diff --git a/leetcode/String/No1071.java b/leetcode/String/No1071.java new file mode 100644 index 0000000..b44611d --- /dev/null +++ b/leetcode/String/No1071.java @@ -0,0 +1,26 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Oct 30, 2019 + */ +public class No1071 { + public String gcdOfStrings(String str1, String str2) { + if (str2.length() > str1.length()) { + return gcdOfStrings(str2, str1); + } + + if (str2.length() == 0) { + return str1; + } + + if (str1.startsWith(str2)) { + while (str1.startsWith(str2)) { + str1 = str1.substring(str2.length()); + } + return gcdOfStrings(str2, str1); + } else { + return ""; + } + } +} diff --git a/leetcode/String/No415.java b/leetcode/String/No415.java new file mode 100644 index 0000000..765c4f2 --- /dev/null +++ b/leetcode/String/No415.java @@ -0,0 +1,40 @@ +package Algorithm.leetcode.String; + +/** + * @author tujietg + * @date 2019/9/4 10:06 AM + */ +public class No415 { + + public static void main(String[] args) { + System.out.println(addStrings("1", "1")); + } + + public static String addStrings(String num1, String num2) { + + int i = num1.length() - 1; + int j = num2.length() - 1; + + char[] num1Array = num1.toCharArray(); + char[] num2Array = num2.toCharArray(); + + int mid = 0; + + StringBuilder sb = new StringBuilder(); + + while (i >= 0 || j >= 0 || mid == 1) { + int a = i >= 0 ? num1Array[i--] - '0' : 0; + int b = j >= 0 ? num2Array[j--] - '0' : 0; + int sum = a + b + mid; + if (sum > 9) { + sb.insert(0, sum % 10); + } else { + sb.insert(0, sum); + } + + + mid = sum / 10; + } + return sb.toString(); + } +} diff --git a/leetcode/String/No459.java b/leetcode/String/No459.java new file mode 100644 index 0000000..c1bcc66 --- /dev/null +++ b/leetcode/String/No459.java @@ -0,0 +1,23 @@ +package Algorithm.leetcode.String; + +/** + * @author tujietg + * @date 2019/9/5 7:42 PM + */ +public class No459 { + + public static void main(String[] args) { + System.out.println(repeatedSubstringPattern("abab")); + } + + public static boolean repeatedSubstringPattern(String s) { + System.out.println(s); + if (s == null || s.length() == 1) { + return false; + } + String newStr = s + s; + System.out.println(newStr.substring(1, newStr.length() - 1)); + return newStr.substring(1, newStr.length() - 1).contains(s); + } + +} diff --git a/leetcode/String/No520.java b/leetcode/String/No520.java new file mode 100644 index 0000000..8cfd717 --- /dev/null +++ b/leetcode/String/No520.java @@ -0,0 +1,33 @@ +package Algorithm.leetcode.String; + +/** + * @author tujietg + * @date 2019/9/5 9:10 AM + */ +public class No520 { + + public static void main(String[] args) { + System.out.println(detectCapitalUse("uZfa")); + } + + public static boolean detectCapitalUse(String word) { + int sum = 0; + for (int i = 0; i < word.length(); i++) { + char mid = word.charAt(i); + if (mid - 'a' <= -7) { + sum++; + } + } + if (sum == word.length() || sum == 0) { + return true; + } else { + if (sum == 1) { + if (word.charAt(0) - 'a' <= -7) { + return true; + } + } + return false; + } + } +} + diff --git a/leetcode/String/No551.java b/leetcode/String/No551.java new file mode 100644 index 0000000..7853714 --- /dev/null +++ b/leetcode/String/No551.java @@ -0,0 +1,25 @@ +package Algorithm.leetcode.String; + +/** + * @author tujietg + * @date 2019/9/5 2:12 PM + */ +public class No551 { + + public boolean checkRecord(String s) { + if (s.contains("LLL")) { + return false; + } + int sum = 0; + for (char item : s.toCharArray()) { + if (item == 'A') { + sum++; + } + } + if (sum > 1) { + return false; + } + return true; + } + +} diff --git a/leetcode/String/No557.java b/leetcode/String/No557.java new file mode 100644 index 0000000..ae4fedf --- /dev/null +++ b/leetcode/String/No557.java @@ -0,0 +1,31 @@ +package Algorithm.leetcode.String; + +/** + * @author tujietg + * @date 2019/9/6 9:45 AM + */ +public class No557 { + + public static String reverseWords(String s) { + String endString = ""; + String[] strArray = s.split(" "); + for (int i = 0; i < strArray.length; i++) { + String midStr = ""; + for (int j = 0; j < strArray[i].length(); j++) { + char first = strArray[i].charAt(j); + char last = strArray[i].charAt(strArray[i].length() - j - 1); + char mid = first; + if (j < strArray[i].length() - 1 - j) { + first = last; + last = mid; + } + } + strArray[i] = strArray[i] + " "; + } + return endString; + } + + public static void main(String[] args) { + System.out.println(reverseWords("Let's")); + } +} diff --git a/leetcode/String/No606.java b/leetcode/String/No606.java new file mode 100644 index 0000000..15767d4 --- /dev/null +++ b/leetcode/String/No606.java @@ -0,0 +1,58 @@ +package Algorithm.leetcode.String; + +/** + * @author tujietg + * @date 2019/9/10 10:02 AM + */ +public class No606 { + + public static void main(String[] args) { + TreeNode t = new TreeNode(1); + TreeNode t1 = new TreeNode(2); + TreeNode t2 = new TreeNode(3); + TreeNode t3 = new TreeNode(4); + + t.left = t1; + t.right = t2; + t.left.left = t3; + + String result = Solution.tree2str(t); + // System.out.println(result); + + } + + + public static class Solution { + public static String tree2str(TreeNode t) { + if (t == null) { + return ""; + } + String result = t.val + ""; + String left = tree2str(t.left); + String right = tree2str(t.right); + + if (left == "" && right == "") { + return result; + } + if (left == "") { + return result + "()" + "(" + right + ")"; + } + if (right == "") { + return result + "(" + left + ")"; + } + return result + "(" + left + ")" + "(" + right + ")"; + } + } + +} + +class TreeNode { + + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} \ No newline at end of file diff --git a/leetcode/String/No788.java b/leetcode/String/No788.java new file mode 100644 index 0000000..8bf866f --- /dev/null +++ b/leetcode/String/No788.java @@ -0,0 +1,19 @@ +package Algorithm.leetcode.String; + +/** + * @author tujietg + * @date 2019/9/20 2:43 PM + */ +public class No788 { + public static int rotatedDigits(int N) { + int count = 0; + for (int i = 1; i <= N; i++) { + String a = String.valueOf(N); + if ((a.contains("2") || a.contains("5") || a.contains("6") || a.contains("9")) + && (!a.contains("3") && !a.contains("4") && !a.contains("7"))) { + count++; + } + } + return count; + } +} diff --git a/leetcode/Tree/No104.java b/leetcode/Tree/No104.java index f9faa1b..06d1cbc 100644 --- a/leetcode/Tree/No104.java +++ b/leetcode/Tree/No104.java @@ -8,7 +8,7 @@ class TreeNodeInt { int val; TreeNodeInt left; - TreeNodeInt right; + int right; TreeNodeInt(int x) { val = x; From 654fc78b3bebe19760639e77c5d44278854e964c Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 30 Oct 2019 10:34:02 +0800 Subject: [PATCH 042/116] =?UTF-8?q?=E4=BF=AE=E6=94=B9606?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/String/No606.java | 92 +++++++++++++++++++------------------- 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 69aaf48..f22beb4 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:104道 +🧗‍♂️ 已解题目:105道 ------- diff --git a/leetcode/String/No606.java b/leetcode/String/No606.java index 15767d4..4e808a0 100644 --- a/leetcode/String/No606.java +++ b/leetcode/String/No606.java @@ -4,55 +4,55 @@ * @author tujietg * @date 2019/9/10 10:02 AM */ -public class No606 { - public static void main(String[] args) { - TreeNode t = new TreeNode(1); - TreeNode t1 = new TreeNode(2); - TreeNode t2 = new TreeNode(3); - TreeNode t3 = new TreeNode(4); - - t.left = t1; - t.right = t2; - t.left.left = t3; - - String result = Solution.tree2str(t); - // System.out.println(result); - - } - - - public static class Solution { - public static String tree2str(TreeNode t) { - if (t == null) { - return ""; - } - String result = t.val + ""; - String left = tree2str(t.left); - String right = tree2str(t.right); - - if (left == "" && right == "") { - return result; - } - if (left == "") { - return result + "()" + "(" + right + ")"; - } - if (right == "") { - return result + "(" + left + ")"; - } - return result + "(" + left + ")" + "(" + right + ")"; - } - } +class TreeNode { + + int val; + TreeNode left; + TreeNode right; + TreeNode(int x) { + val = x; + } } -class TreeNode { +public class No606 { - int val; - TreeNode left; - TreeNode right; + public static void main(String[] args) { + TreeNode t = new TreeNode(1); + TreeNode t1 = new TreeNode(2); + TreeNode t2 = new TreeNode(3); + TreeNode t3 = new TreeNode(4); + + t.left = t1; + t.right = t2; + t.left.left = t3; + + String result = Solution.tree2str(t); + // System.out.println(result); + + } + + public static class Solution { + public static String tree2str(TreeNode t) { + if (t == null) { + return ""; + } + String result = t.val + ""; + String left = tree2str(t.left); + String right = tree2str(t.right); + + if (left == "" && right == "") { + return result; + } + if (left == "") { + return result + "()" + "(" + right + ")"; + } + if (right == "") { + return result + "(" + left + ")"; + } + return result + "(" + left + ")" + "(" + right + ")"; + } + } - TreeNode(int x) { - val = x; - } -} \ No newline at end of file +} From 9ee6032570474956361d34aa2ed29cd7b798c340 Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 31 Oct 2019 09:50:25 +0800 Subject: [PATCH 043/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=B7=E9=A2=98?= =?UTF-8?q?=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f22beb4..57911f8 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:3天 +✊ 不间断刷题天数:4天 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:105道 +🧗‍♂️ 已解题目:107道 ------- From 0b1063209b89c0b73bec9dd1075c4086726f2d37 Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 31 Oct 2019 21:34:52 +0800 Subject: [PATCH 044/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0268=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/Array/No268.java | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 leetcode/Array/No268.java diff --git a/README.md b/README.md index 57911f8..86af8ea 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:107道 +🧗‍♂️ 已解题目:108道 ------- diff --git a/leetcode/Array/No268.java b/leetcode/Array/No268.java new file mode 100644 index 0000000..9219ff2 --- /dev/null +++ b/leetcode/Array/No268.java @@ -0,0 +1,20 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Oct 31, 2019 + */ +public class No268 { + + public static int missingNumber(int[] nums) { + int sum = 0; + for (int i = 0; i < nums.length; i++) + sum += nums[i] - i; + return nums.length - sum; + } + + public static void main(String[] args) { + System.out.println(missingNumber(new int[] { 0, 2, 3, 4 })); + } + +} From b43a0cb67ccfc083bce37872bb02c58b6e83e703 Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 1 Nov 2019 10:58:56 +0800 Subject: [PATCH 045/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No414?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- leetcode/Array/No414.java | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 leetcode/Array/No414.java diff --git a/README.md b/README.md index 86af8ea..dee94a5 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:4天 +✊ 不间断刷题天数:5天 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:108道 +🧗‍♂️ 已解题目:109道 ------- diff --git a/leetcode/Array/No414.java b/leetcode/Array/No414.java new file mode 100644 index 0000000..73bbf04 --- /dev/null +++ b/leetcode/Array/No414.java @@ -0,0 +1,39 @@ +package Algorithm.leetcode.Array; + +import java.util.HashSet; + +/** + * + * Created by tujietg on Nov 1, 2019 + */ +public class No414 { + public static int thirdMax(int[] nums) { + + int max = Integer.MIN_VALUE; + int second = Integer.MIN_VALUE; + int three = Integer.MIN_VALUE; + HashSet set = new HashSet(); + for (int i = 0; i < nums.length; i++) { + set.add(nums[i]); + if (nums[i] > max) { + three = second; + second = max; + max = nums[i]; + } else if (nums[i] > second && nums[i] < max) { + three = second; + second = nums[i]; + } else if ((nums[i] > three && nums[i] < second)) { + three = nums[i]; + } + } + if (three == Integer.MIN_VALUE && set.size() < 3) { + return max; + } else { + return three; + } + } + + public static void main(String[] args) { + System.out.println(thirdMax(new int[] { 5, 2, 4, 1, 3, 6, 0 })); + } +} From b4985acc045d9dacd7774f7becd16ae8c2839eb4 Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 1 Nov 2019 11:10:59 +0800 Subject: [PATCH 046/116] =?UTF-8?q?=E4=BF=AE=E6=94=B9No414?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Array/No414.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/leetcode/Array/No414.java b/leetcode/Array/No414.java index 73bbf04..e94a41b 100644 --- a/leetcode/Array/No414.java +++ b/leetcode/Array/No414.java @@ -8,7 +8,6 @@ */ public class No414 { public static int thirdMax(int[] nums) { - int max = Integer.MIN_VALUE; int second = Integer.MIN_VALUE; int three = Integer.MIN_VALUE; @@ -26,6 +25,7 @@ public static int thirdMax(int[] nums) { three = nums[i]; } } + if (three == Integer.MIN_VALUE && set.size() < 3) { return max; } else { From ab37a21db8d1d361e1e7f4bb3d915e1be322be0f Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 1 Nov 2019 15:37:28 +0800 Subject: [PATCH 047/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No485=E9=A2=98?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/Array/No485.java | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 leetcode/Array/No485.java diff --git a/README.md b/README.md index dee94a5..86c4983 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数:5天 -🧗‍♂️ 已解题目:109道 +🧗‍♂️ 已解题目:110道 ------- diff --git a/leetcode/Array/No485.java b/leetcode/Array/No485.java new file mode 100644 index 0000000..77490eb --- /dev/null +++ b/leetcode/Array/No485.java @@ -0,0 +1,25 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 1, 2019 + */ +public class No485 { + public int findMaxConsecutiveOnes(int[] nums) { + int max = 0; + int mid = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] == 1) { + mid++; + } else { + max = max > mid ? max : mid; + mid = 0; + } + } + if (max > mid) { + return max; + } else { + return mid; + } + } +} From fa1f7dd1a7c30f89d70292c02e2fac78e1ae8d09 Mon Sep 17 00:00:00 2001 From: tujietg Date: Sat, 2 Nov 2019 23:22:52 +0800 Subject: [PATCH 048/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No509?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- leetcode/Array/No509.java | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 leetcode/Array/No509.java diff --git a/README.md b/README.md index 86c4983..3275623 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:5天 +✊ 不间断刷题天数:6天 -🐘 最长连续刷题天数:5天 +🐘 最长连续刷题天数:6天 -🧗‍♂️ 已解题目:110道 +🧗‍♂️ 已解题目:111道 ------- diff --git a/leetcode/Array/No509.java b/leetcode/Array/No509.java new file mode 100644 index 0000000..c4d0234 --- /dev/null +++ b/leetcode/Array/No509.java @@ -0,0 +1,18 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 2, 2019 + */ +public class No509 { + public int fib(int N) { + if (N == 0) { + return 0; + } + + if (N == 1) { + return 1; + } + return fib(N - 1) + fib(N - 2); + } +} From e6658a5b61b73e159c0148c97e735f9defe2f175 Mon Sep 17 00:00:00 2001 From: tujietg Date: Sun, 3 Nov 2019 18:38:00 +0800 Subject: [PATCH 049/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No905?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/Array/No905.java | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 leetcode/Array/No905.java diff --git a/README.md b/README.md index 3275623..50542a8 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数:6天 -🧗‍♂️ 已解题目:111道 +🧗‍♂️ 已解题目:112道 ------- diff --git a/leetcode/Array/No905.java b/leetcode/Array/No905.java new file mode 100644 index 0000000..ddf1d21 --- /dev/null +++ b/leetcode/Array/No905.java @@ -0,0 +1,21 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 3, 2019 + */ +public class No905 { + public int[] sortArrayByParity(int[] A) { + int a = 0; + int b = A.length - 1; + int[] end = new int[A.length]; + for (int i = 0; i < A.length; i++) { + if (A[i] % 2 == 0) { + end[a++] = A[i]; + } else { + end[b--] = A[i]; + } + } + return end; + } +} From ac9b1cf909915e134f4269cad83e1ef6d8c84c8f Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 4 Nov 2019 11:26:00 +0800 Subject: [PATCH 050/116] =?UTF-8?q?=E4=BF=AE=E6=94=B9Readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- leetcode/Array/No905.java | 4 ++++ leetcode/String/No1247.java | 31 +++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 leetcode/String/No1247.java diff --git a/README.md b/README.md index 50542a8..cbd5860 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:6天 +✊ 不间断刷题天数:8天 -🐘 最长连续刷题天数:6天 +🐘 最长连续刷题天数:8天 -🧗‍♂️ 已解题目:112道 +🧗‍♂️ 已解题目:113道 ------- diff --git a/leetcode/Array/No905.java b/leetcode/Array/No905.java index ddf1d21..a0a51a0 100644 --- a/leetcode/Array/No905.java +++ b/leetcode/Array/No905.java @@ -18,4 +18,8 @@ public int[] sortArrayByParity(int[] A) { } return end; } + + public static void main(String[] args) { + System.out.println(0 / 2); + } } diff --git a/leetcode/String/No1247.java b/leetcode/String/No1247.java new file mode 100644 index 0000000..f94406c --- /dev/null +++ b/leetcode/String/No1247.java @@ -0,0 +1,31 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Nov 4, 2019 + */ +public class No1247 { + public static int minimumSwap(String s1, String s2) { + int a = 0; + int b = 0; + for (int i = 0; i < s1.length(); i++) { + if (s1.charAt(i) != s2.charAt(i) && s1.charAt(i) == 'x') { + ++a; + System.out.println(a); + } + if (s1.charAt(i) != s2.charAt(i) && s1.charAt(i) == 'y') { + ++b; + System.out.println(b); + } + } + if (a % 2 == 0 && b % 2 == 0) { + return a / 2 + b / 2; + } else if (a % 2 == 1 && b % 2 == 1) { + return a / 2 + b / 2 + 2; + } else { + return -1; + } + } + + +} From 093b23ef1895dbecfc6195a3609d671f0c8402d6 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 4 Nov 2019 16:15:28 +0800 Subject: [PATCH 051/116] No832 --- README.md | 2 +- leetcode/Array/No832.java | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 leetcode/Array/No832.java diff --git a/README.md b/README.md index cbd5860..5ae8b7c 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数:8天 -🧗‍♂️ 已解题目:113道 +🧗‍♂️ 已解题目:112道 ------- diff --git a/leetcode/Array/No832.java b/leetcode/Array/No832.java new file mode 100644 index 0000000..852bf60 --- /dev/null +++ b/leetcode/Array/No832.java @@ -0,0 +1,28 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 4, 2019 + */ +public class No832 { + public int[][] flipAndInvertImage(int[][] A) { + for (int i = 0; i < A.length; i++) { + int[] B = A[i]; + for (int j = 0; j < B.length; j++) { + if (j < B.length - j - 1) { + int mid = B[j]; + B[j] = B[B.length - j - 1]; + B[B.length - j - 1] = mid; + } + } + for (int j = 0; j < B.length; j++) { + if (B[j] == 1) { + B[j] = 0; + } else { + B[j] = 1; + } + } + } + return A; + } +} From 853660599d539e0b70093b241488e3470b2a5474 Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 5 Nov 2019 09:50:17 +0800 Subject: [PATCH 052/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No1051?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- leetcode/Array/No1051.java | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 leetcode/Array/No1051.java diff --git a/README.md b/README.md index 5ae8b7c..a38b9aa 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:8天 +✊ 不间断刷题天数:9天 -🐘 最长连续刷题天数:8天 +🐘 最长连续刷题天数:9天 -🧗‍♂️ 已解题目:112道 +🧗‍♂️ 已解题目:113道 ------- diff --git a/leetcode/Array/No1051.java b/leetcode/Array/No1051.java new file mode 100644 index 0000000..c8d6de2 --- /dev/null +++ b/leetcode/Array/No1051.java @@ -0,0 +1,25 @@ +package Algorithm.leetcode.Array; + +import java.util.Arrays; + +/** + * + * Created by tujietg on Nov 5, 2019 + */ +public class No1051 { + public static int heightChecker(int[] heights) { + int[] heightsSort = new int[heights.length]; + for (int i = 0; i < heights.length; i++) { + heightsSort[i] = heights[i]; + } + Arrays.sort(heights); + int sum = 0; + for (int i = 0; i < heights.length; i++) { + if (heights[i] != heightsSort[i]) { + sum++; + } + } + return sum; + } + +} From c2580d4967f37c9c29794425c508c68f5f66444d Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 6 Nov 2019 09:22:32 +0800 Subject: [PATCH 053/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No977?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- leetcode/Array/No977.py | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 leetcode/Array/No977.py diff --git a/README.md b/README.md index a38b9aa..ec60f18 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:9天 +✊ 不间断刷题天数:10天 -🐘 最长连续刷题天数:9天 +🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:113道 +🧗‍♂️ 已解题目:114道 ------- diff --git a/leetcode/Array/No977.py b/leetcode/Array/No977.py new file mode 100644 index 0000000..a6a6d4b --- /dev/null +++ b/leetcode/Array/No977.py @@ -0,0 +1,5 @@ +class Solution(object): + def sortedSquares(self, A): + for i in range(len(A)): + A[i] *= A[i] + return sorted(A) From 0e2fb4527f47142b742ea333e546b3b8d804baef Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 6 Nov 2019 21:00:42 +0800 Subject: [PATCH 054/116] =?UTF-8?q?=E8=A1=A5=E5=85=A8=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E8=BF=87=E7=9A=84=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Array/No167.java | 29 +++++++++++ leetcode/Array/No169.java | 22 ++++++++ leetcode/Array/No189.java | 30 +++++++++++ leetcode/Array/No219.java | 23 +++++++++ leetcode/Array/No283.java | 19 +++++++ leetcode/Array/No695.java | 29 +++++++++++ leetcode/Array/No717.java | 22 ++++++++ leetcode/Array/No724.java | 25 ++++++++++ leetcode/Array/No747.java | 24 +++++++++ leetcode/DP/No392.java | 24 +++++++++ .../LinkedList/{No106.java => No160.java} | 2 +- leetcode/String/{No1198.java => No1189.java} | 2 +- leetcode/String/No345.java | 41 +++++++++++++++ leetcode/String/No434.java | 29 +++++++++++ leetcode/String/No443.java | 29 +++++++++++ leetcode/String/No521.java | 11 ++++ leetcode/String/No541.java | 34 +++++++++++++ leetcode/String/No657.java | 33 ++++++++++++ leetcode/String/No680.java | 26 ++++++++++ leetcode/String/No686.java | 50 +++++++++++++++++++ leetcode/String/No709.java | 11 ++++ leetcode/String/No804.java | 23 +++++++++ leetcode/Tree/No108.java | 23 +++++++++ leetcode/Tree/No110.java | 26 ++++++++++ leetcode/Tree/No111.java | 16 ++++++ leetcode/Tree/No112.java | 17 +++++++ leetcode/Tree/No226.java | 19 +++++++ leetcode/Tree/No617.java | 20 ++++++++ leetcode/binarysearch/No744.java | 25 ++++++++++ leetcode/hashtable/No202.java | 24 +++++++++ leetcode/hashtable/No349.java | 29 +++++++++++ leetcode/hashtable/No389.java | 20 ++++++++ leetcode/hashtable/No409.java | 29 +++++++++++ leetcode/hashtable/No771.java | 23 +++++++++ leetcode/math/No202.java | 24 +++++++++ leetcode/math/No231.java | 11 ++++ leetcode/math/No258.java | 11 ++++ 37 files changed, 853 insertions(+), 2 deletions(-) create mode 100644 leetcode/Array/No167.java create mode 100644 leetcode/Array/No169.java create mode 100644 leetcode/Array/No189.java create mode 100644 leetcode/Array/No219.java create mode 100644 leetcode/Array/No283.java create mode 100644 leetcode/Array/No695.java create mode 100644 leetcode/Array/No717.java create mode 100644 leetcode/Array/No724.java create mode 100644 leetcode/Array/No747.java create mode 100644 leetcode/DP/No392.java rename leetcode/LinkedList/{No106.java => No160.java} (93%) rename leetcode/String/{No1198.java => No1189.java} (96%) create mode 100644 leetcode/String/No345.java create mode 100644 leetcode/String/No434.java create mode 100644 leetcode/String/No443.java create mode 100644 leetcode/String/No521.java create mode 100644 leetcode/String/No541.java create mode 100644 leetcode/String/No657.java create mode 100644 leetcode/String/No680.java create mode 100644 leetcode/String/No686.java create mode 100644 leetcode/String/No709.java create mode 100644 leetcode/String/No804.java create mode 100644 leetcode/Tree/No108.java create mode 100644 leetcode/Tree/No110.java create mode 100644 leetcode/Tree/No111.java create mode 100644 leetcode/Tree/No112.java create mode 100644 leetcode/Tree/No226.java create mode 100644 leetcode/Tree/No617.java create mode 100644 leetcode/binarysearch/No744.java create mode 100644 leetcode/hashtable/No202.java create mode 100644 leetcode/hashtable/No349.java create mode 100644 leetcode/hashtable/No389.java create mode 100644 leetcode/hashtable/No409.java create mode 100644 leetcode/hashtable/No771.java create mode 100644 leetcode/math/No202.java create mode 100644 leetcode/math/No231.java create mode 100644 leetcode/math/No258.java diff --git a/leetcode/Array/No167.java b/leetcode/Array/No167.java new file mode 100644 index 0000000..64cb2e7 --- /dev/null +++ b/leetcode/Array/No167.java @@ -0,0 +1,29 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No167 { + public int[] twoSum(int[] numbers, int target) { + int[] end = new int[2]; + if (numbers.length < 2) { + return end; + } + int left = 0; + int right = numbers.length - 1; + while (left < right) { + int num = numbers[left] + numbers[right]; + if (num == target) { + end[0] = left + 1; + end[1] = right + 1; + return end; + } else if (num > target) { + right--; + } else { + left++; + } + } + return end; + } +} diff --git a/leetcode/Array/No169.java b/leetcode/Array/No169.java new file mode 100644 index 0000000..8dfd251 --- /dev/null +++ b/leetcode/Array/No169.java @@ -0,0 +1,22 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No169 { + public int majorityElement(int[] nums) { + int len = nums.length; + int count = 0; + int end = 0; + for (int i = 0; i < len; i++) { + if (count == 0 || end == nums[i]) { + end = nums[i]; + count++; + } else { + count--; + } + } + return end; + } +} diff --git a/leetcode/Array/No189.java b/leetcode/Array/No189.java new file mode 100644 index 0000000..ea5312a --- /dev/null +++ b/leetcode/Array/No189.java @@ -0,0 +1,30 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No189 { + public void rotate(int[] nums, int k) { + // [1,2,3,4,5,6,7] [5,6,7,1,2,3,4] k = 3; + // 思路:第一步:数组反转,第二步, 把0-k-1 反转 第三步:k nums.length 反转 + k = k % nums.length; + No189 s = new No189(); + s.reverse(nums, 0, nums.length - 1); + s.reverse(nums, 0, k - 1); + s.reverse(nums, k, nums.length - 1); + + } + + // 反轉方法 + public void reverse(int[] arr, int start, int end) { + while (end > start) { + int tem = 0; + tem = arr[start]; + arr[start] = arr[end]; + arr[end] = tem; + end--; + start++; + } + } +} diff --git a/leetcode/Array/No219.java b/leetcode/Array/No219.java new file mode 100644 index 0000000..6ee3d2c --- /dev/null +++ b/leetcode/Array/No219.java @@ -0,0 +1,23 @@ +package Algorithm.leetcode.Array; + +import java.util.HashSet; +import java.util.Set; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No219 { + public boolean containsNearbyDuplicate(int[] nums, int k) { + Set set = new HashSet(); + for (int i = 0; i < nums.length; i++) { + if (i > k) { + set.remove(nums[i - k - 1]); + } + if (!set.add(nums[i])) { + return true; + } + } + return false; + } +} diff --git a/leetcode/Array/No283.java b/leetcode/Array/No283.java new file mode 100644 index 0000000..c96e4e7 --- /dev/null +++ b/leetcode/Array/No283.java @@ -0,0 +1,19 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No283 { + public void moveZeroes(int[] nums) { + int tem = 0; + for (int num : nums) { + if (num != 0) { + nums[tem++] = num; + } + } + while (nums.length > tem) { + nums[tem++] = 0; + } + } +} diff --git a/leetcode/Array/No695.java b/leetcode/Array/No695.java new file mode 100644 index 0000000..cedef3b --- /dev/null +++ b/leetcode/Array/No695.java @@ -0,0 +1,29 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No695 { + public int[] twoSum(int[] numbers, int target) { + int[] end = new int[2]; + if (numbers.length < 2) { + return end; + } + int left = 0; + int right = numbers.length - 1; + while (left < right) { + int num = numbers[left] + numbers[right]; + if (num == target) { + end[0] = left + 1; + end[1] = right + 1; + return end; + } else if (num > target) { + right--; + } else { + left++; + } + } + return end; + } +} diff --git a/leetcode/Array/No717.java b/leetcode/Array/No717.java new file mode 100644 index 0000000..35b7406 --- /dev/null +++ b/leetcode/Array/No717.java @@ -0,0 +1,22 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No717 { + public boolean isOneBitCharacter(int[] bits) { + int len = bits.length; + int i = 0; + while (i < len - 1) { + // 当为零的时候,跳转到下一位。 + if (bits[i] == 0) { + i++; + } else { + // 当不为零的时候,跳转两位。 + i += 2; + } + } + return len - 1 == i; + } +} diff --git a/leetcode/Array/No724.java b/leetcode/Array/No724.java new file mode 100644 index 0000000..768e60f --- /dev/null +++ b/leetcode/Array/No724.java @@ -0,0 +1,25 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No724 { + public int pivotIndex(int[] nums) { + int sum = 0; + int left = 0; + // 计算数组的总和 + for (int i = 0; i < nums.length; i++) { + sum += nums[i]; + } + for (int i = 0; i < nums.length; i++) { + if (i != 0) { + left += nums[i - 1]; + } + if (sum - left - nums[i] == left) { + return i; + } + } + return -1; + } +} diff --git a/leetcode/Array/No747.java b/leetcode/Array/No747.java new file mode 100644 index 0000000..317e0ea --- /dev/null +++ b/leetcode/Array/No747.java @@ -0,0 +1,24 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No747 { + public int dominantIndex(int[] nums) { + // 找出最大的值與第二大的值的雙倍作比較 + int one = 0; + int two = 0; + int index = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] > one) { + two = one; + one = nums[i]; + index = i; + } else if (nums[i] > two) { + two = nums[i]; + } + } + return (one >= two * 2) ? index : -1; + } +} diff --git a/leetcode/DP/No392.java b/leetcode/DP/No392.java new file mode 100644 index 0000000..770fa46 --- /dev/null +++ b/leetcode/DP/No392.java @@ -0,0 +1,24 @@ +package Algorithm.leetcode.DP; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No392 { + public boolean isSubsequence(String s, String t) { + + if (s == null || s.length() == 0) { + return true; + } + int idx = 0; + for (int i = 0; i < t.length(); i++) { + if (t.charAt(i) == s.charAt(idx)) { + idx++; + } + if (idx == s.length()) { + return true; + } + } + return s.length() == idx; + } +} diff --git a/leetcode/LinkedList/No106.java b/leetcode/LinkedList/No160.java similarity index 93% rename from leetcode/LinkedList/No106.java rename to leetcode/LinkedList/No160.java index 4eae59f..e265147 100644 --- a/leetcode/LinkedList/No106.java +++ b/leetcode/LinkedList/No160.java @@ -1,6 +1,6 @@ package Algorithm.leetcode.LinkedList; -public class No106 { +public class No160 { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) { return null; diff --git a/leetcode/String/No1198.java b/leetcode/String/No1189.java similarity index 96% rename from leetcode/String/No1198.java rename to leetcode/String/No1189.java index c034b1c..7ed4d7a 100644 --- a/leetcode/String/No1198.java +++ b/leetcode/String/No1189.java @@ -4,7 +4,7 @@ * * Created by tujietg on Oct 24, 2019 */ -public class No1198 { +public class No1189 { public int maxNumberOfBalloons(String text) { int a = 0; int b = 0; diff --git a/leetcode/String/No345.java b/leetcode/String/No345.java new file mode 100644 index 0000000..9254fcf --- /dev/null +++ b/leetcode/String/No345.java @@ -0,0 +1,41 @@ +package Algorithm.leetcode.String; + +import java.util.HashSet; +import java.util.Set; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No345 { + public String reverseVowels(String s) { + char[] list = s.toCharArray(); + Set newSet = new HashSet(); + newSet.add('a'); + newSet.add('e'); + newSet.add('i'); + newSet.add('o'); + newSet.add('u'); + newSet.add('A'); + newSet.add('E'); + newSet.add('I'); + newSet.add('O'); + newSet.add('U'); + for (int i = 0, j = list.length - 1; i < j;) { + if (!newSet.contains(list[i])) { + i++; + continue; + } + if (!newSet.contains(list[j])) { + j--; + continue; + } + char tmp = list[i]; + list[i] = list[j]; + list[j] = tmp; + j--; + i++; + } + return String.valueOf(list); + } +} diff --git a/leetcode/String/No434.java b/leetcode/String/No434.java new file mode 100644 index 0000000..df90367 --- /dev/null +++ b/leetcode/String/No434.java @@ -0,0 +1,29 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No434 { + public int compress(char[] chars) { + int index = 0; + int endindex = 0; + while (index < chars.length) { + char current = chars[index]; + int count = 0; + while (index < chars.length && current == chars[index]) { + count++; + index++; + } + + chars[endindex++] = current; + + if (count != 1) { + for (char item : Integer.toString(count).toCharArray()) { + chars[endindex++] = item; + } + } + } + return endindex; + } +} diff --git a/leetcode/String/No443.java b/leetcode/String/No443.java new file mode 100644 index 0000000..7bed390 --- /dev/null +++ b/leetcode/String/No443.java @@ -0,0 +1,29 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No443 { + public int compress(char[] chars) { + int index = 0; + int endindex = 0; + while (index < chars.length) { + char current = chars[index]; + int count = 0; + while (index < chars.length && current == chars[index]) { + count++; + index++; + } + + chars[endindex++] = current; + + if (count != 1) { + for (char item : Integer.toString(count).toCharArray()) { + chars[endindex++] = item; + } + } + } + return endindex; + } +} diff --git a/leetcode/String/No521.java b/leetcode/String/No521.java new file mode 100644 index 0000000..d7368c6 --- /dev/null +++ b/leetcode/String/No521.java @@ -0,0 +1,11 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No521 { + public int findLUSlength(String a, String b) { + return a.equals(b) ? -1 : Math.max(a.length(), b.length()); + } +} diff --git a/leetcode/String/No541.java b/leetcode/String/No541.java new file mode 100644 index 0000000..03d8407 --- /dev/null +++ b/leetcode/String/No541.java @@ -0,0 +1,34 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No541 { + public String reverseStr(String s, int k) { + // 隔着K然后反转K + int len = s.length(); + char[] c = s.toCharArray(); + for (int i = 0; i < len; i += 2 * k) { + if (i + k <= len) { + reverse(c, i, i + k - 1); + } else { + reverse(c, i, len - 1); + } + } + // 字符数组转化为字符串 直接new String (c) + return new String(c); + } + + // 反转方法的实现 + public void reverse(char[] c, int start, int end) { + // 从start到end结束 之中进行反转 + while (start < end) { + char temp = c[start]; + c[start] = c[end]; + c[end] = temp; + start++; + end--; + } + } +} diff --git a/leetcode/String/No657.java b/leetcode/String/No657.java new file mode 100644 index 0000000..8c517d2 --- /dev/null +++ b/leetcode/String/No657.java @@ -0,0 +1,33 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No657 { + public boolean judgeCircle(String moves) { + char[] movesChar = moves.toCharArray(); + int r = 0, l = 0, u = 0, d = 0; + for (char item : movesChar) { + switch (item) { + case 'R': + r++; + break; + case 'L': + l++; + break; + case 'U': + u++; + break; + case 'D': + d++; + break; + } + } + + if (r - l == 0 && u - d == 0) { + return true; + } + return false; + } +} diff --git a/leetcode/String/No680.java b/leetcode/String/No680.java new file mode 100644 index 0000000..12606e0 --- /dev/null +++ b/leetcode/String/No680.java @@ -0,0 +1,26 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No680 { + public boolean validPalindrome(String s) { + int start = -1, end = s.length(); + while(++start < --end){ + if(s.charAt(start) != s.charAt(end)){ + return isTrue(s,start - 1, end) || isTrue(s,start, end + 1); + } + } + return true; + } + + public boolean isTrue(String str,int start,int end){ + while(++start < --end){ + if(str.charAt(start) != str.charAt(end)){ + return false; + } + } + return true; + } +} diff --git a/leetcode/String/No686.java b/leetcode/String/No686.java new file mode 100644 index 0000000..c9a3374 --- /dev/null +++ b/leetcode/String/No686.java @@ -0,0 +1,50 @@ +package Algorithm.leetcode.String; + +import java.util.LinkedList; +import java.util.List; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No686 { + public int repeatedStringMatch(String A, String B) { + String newB = reloveString(B); + String newA = reloveString(A); + if (newB.contains(newA)) { + for (int a = 0; a < newB.length(); a++) { + if (!newA.contains(newB.charAt(a) + "")) { + return -1; + } + } + } + String mid = A; + int a = 1; + while (!A.contains(B)) { + A = A + mid; + a++; + if (A.length() > 2 * B.length() && a >= 100) { + return -1; + } + } + return a; + } + + public String reloveString(String de) { + + char[] chars = de.toCharArray(); + List str = new LinkedList<>(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < chars.length; i++) { + if (str.contains(String.valueOf(chars[i]))) { + continue; + } else { + str.add(String.valueOf(chars[i])); + sb.append(String.valueOf(chars[i])); + } + } + return sb.toString(); + + } + +} diff --git a/leetcode/String/No709.java b/leetcode/String/No709.java new file mode 100644 index 0000000..1c03933 --- /dev/null +++ b/leetcode/String/No709.java @@ -0,0 +1,11 @@ +package Algorithm.leetcode.String; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No709 { + public String toLowerCase(String str) { + return str.toLowerCase(); + } +} diff --git a/leetcode/String/No804.java b/leetcode/String/No804.java new file mode 100644 index 0000000..1139393 --- /dev/null +++ b/leetcode/String/No804.java @@ -0,0 +1,23 @@ +package Algorithm.leetcode.String; + +import java.util.HashSet; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No804 { + public int uniqueMorseRepresentations(String[] words) { + String[] strArray = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", + "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." }; + HashSet set = new HashSet(); + for (String item : words) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < item.length(); i++) { + sb.append(strArray[item.charAt(i) - 'a']); + } + set.add(sb.toString()); + } + return set.size(); + } +} diff --git a/leetcode/Tree/No108.java b/leetcode/Tree/No108.java new file mode 100644 index 0000000..205fc56 --- /dev/null +++ b/leetcode/Tree/No108.java @@ -0,0 +1,23 @@ +package Algorithm.leetcode.Tree; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + return toBST(nums, 0, nums.length - 1); + } + + // 方法对数组生成树 每个数组的中间值等于每个树或者子树的根节点 + public static TreeNode toBST(int[] nums, int start, int end) { + if (start > end) { + return null; + } + int mid = (start + end) / 2; + TreeNode node = new TreeNode(nums[mid]); + node.left = toBST(nums, start, mid - 1); + node.right = toBST(nums, mid + 1, end); + return node; + } +} \ No newline at end of file diff --git a/leetcode/Tree/No110.java b/leetcode/Tree/No110.java new file mode 100644 index 0000000..db30c7c --- /dev/null +++ b/leetcode/Tree/No110.java @@ -0,0 +1,26 @@ +package Algorithm.leetcode.Tree; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No110 { + public boolean isBalanced(TreeNode root) { + if (root == null) { + return true; + } + if (depth(root.left) - depth(root.right) > 1 || depth(root.right) - depth(root.left) > 1) { + return false; + } + return isBalanced(root.right) && isBalanced(root.left); + } + + // 求出子树的高度 + public static int depth(TreeNode root) { + if (root == null) { + return 0; + } + // 通过递归来求出每个子树的高度 + return Math.max(depth(root.left), depth(root.right)) + 1; + } +} diff --git a/leetcode/Tree/No111.java b/leetcode/Tree/No111.java new file mode 100644 index 0000000..6b556d1 --- /dev/null +++ b/leetcode/Tree/No111.java @@ -0,0 +1,16 @@ +package Algorithm.leetcode.Tree; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No111 { + public int minDepth(TreeNode root) { + if (root == null) { + return 0; + } + int left = minDepth(root.left); + int right = minDepth(root.right); + return (left == 0 || right == 0) ? left + right + 1 : Math.min(left + 1, right + 1); + } +} diff --git a/leetcode/Tree/No112.java b/leetcode/Tree/No112.java new file mode 100644 index 0000000..8503eca --- /dev/null +++ b/leetcode/Tree/No112.java @@ -0,0 +1,17 @@ +package Algorithm.leetcode.Tree; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No112 { + public boolean hasPathSum(TreeNode root, int sum) { + if (root == null) { + return false; + } + if (root.left == null && root.right == null && sum - root.val == 0) { + return true; + } + return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); + } +} diff --git a/leetcode/Tree/No226.java b/leetcode/Tree/No226.java new file mode 100644 index 0000000..a24524d --- /dev/null +++ b/leetcode/Tree/No226.java @@ -0,0 +1,19 @@ +package Algorithm.leetcode.Tree; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No226 { + public TreeNode invertTree(TreeNode root) { + // 使用遍历交换左子树和右子树 + if (root == null) { + return null; + } + TreeNode left = invertTree(root.left); + TreeNode right = invertTree(root.right); + root.left = right; + root.right = left; + return root; + } +} diff --git a/leetcode/Tree/No617.java b/leetcode/Tree/No617.java new file mode 100644 index 0000000..32f2202 --- /dev/null +++ b/leetcode/Tree/No617.java @@ -0,0 +1,20 @@ +package Algorithm.leetcode.Tree; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No617 { + public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { + if (t1 == null && t2 == null) { + return null; + } + int newval = (t1 == null ? 0 : t1.val) + (t2 == null ? 0 : t2.val); + + TreeNode newtree = new TreeNode(newval); + + newtree.left = mergeTrees(t1 == null ? null : t1.left, t2 == null ? null : t2.left); + newtree.right = mergeTrees(t1 == null ? null : t1.right, t2 == null ? null : t2.right); + return newtree; + } +} diff --git a/leetcode/binarysearch/No744.java b/leetcode/binarysearch/No744.java new file mode 100644 index 0000000..de52845 --- /dev/null +++ b/leetcode/binarysearch/No744.java @@ -0,0 +1,25 @@ +package Algorithm.leetcode.binarysearch; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No744 { + public char nextGreatestLetter(char[] letters, char target) { + int len = letters.length; + int tarInt = target - 'A' + 1; + int lerInt = letters[len - 1] - 'A' + 1; + if (target == 'z' || tarInt >= lerInt) { + return letters[0]; + } + int i; + for (i = 1; i < len; i++) { + int leInt = letters[i - 1] - 'A' + 1; + if (leInt > tarInt) { + break; + } + } + return letters[i - 1]; + } + +} diff --git a/leetcode/hashtable/No202.java b/leetcode/hashtable/No202.java new file mode 100644 index 0000000..3b5dd8b --- /dev/null +++ b/leetcode/hashtable/No202.java @@ -0,0 +1,24 @@ +package Algorithm.leetcode.hashtable; + +import java.util.HashSet; +import java.util.Set; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No202 { + public boolean isHappy(int n) { + Set set = new HashSet(); + while (n != 1 && !set.contains(n)) { + set.add(n); + int temp = 0; + while (n != 0) { + temp += Math.pow(n % 10, 2); + n = n / 10; + } + n = temp; + } + return n == 1; + } +} diff --git a/leetcode/hashtable/No349.java b/leetcode/hashtable/No349.java new file mode 100644 index 0000000..67721e8 --- /dev/null +++ b/leetcode/hashtable/No349.java @@ -0,0 +1,29 @@ +package Algorithm.leetcode.hashtable; + +import java.util.HashSet; +import java.util.Set; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No349 { + public int[] intersection(int[] nums1, int[] nums2) { + Set set = new HashSet<>(); + Set twoset = new HashSet<>(); + for (int i = 0; i < nums1.length; i++) { + set.add(nums1[i]); + } + for (int i = 0; i < nums2.length; i++) { + if (set.contains(nums2[i])) { + twoset.add(nums2[i]); + } + } + int j = 0; + int[] arr = new int[twoset.size()]; + for (Integer num : twoset) { + arr[j++] = num; + } + return arr; + } +} diff --git a/leetcode/hashtable/No389.java b/leetcode/hashtable/No389.java new file mode 100644 index 0000000..93bfa2f --- /dev/null +++ b/leetcode/hashtable/No389.java @@ -0,0 +1,20 @@ +package Algorithm.leetcode.hashtable; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No389 { + public char findTheDifference(String s, String t) { + int snum = 0; + int tnum = 0; + for (int i = 0; i < s.length(); i++) { + snum += (int) s.charAt(i); + } + for (int i = 0; i < t.length(); i++) { + tnum += (int) t.charAt(i); + } + int n = tnum - snum; + return (char) n; + } +} diff --git a/leetcode/hashtable/No409.java b/leetcode/hashtable/No409.java new file mode 100644 index 0000000..d7efcc8 --- /dev/null +++ b/leetcode/hashtable/No409.java @@ -0,0 +1,29 @@ +package Algorithm.leetcode.hashtable; + +import java.util.HashSet; +import java.util.Set; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No409 { + // 采用HashSet解决 + public int longestPalindrome(String s) { + Set hs = new HashSet<>(); + int count = 0; + for (int i = 0; i < s.length(); i++) { + if (hs.contains(s.charAt(i))) { + hs.remove(s.charAt(i)); + count++; + } else { + hs.add(s.charAt(i)); + } + } + if (!hs.isEmpty()) { + return count * 2 + 1; + } else { + return count * 2; + } + } +} diff --git a/leetcode/hashtable/No771.java b/leetcode/hashtable/No771.java new file mode 100644 index 0000000..c18ebd2 --- /dev/null +++ b/leetcode/hashtable/No771.java @@ -0,0 +1,23 @@ +package Algorithm.leetcode.hashtable; + +import java.util.HashSet; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No771 { + public int numJewelsInStones(String J, String S) { + int result = 0; + HashSet hashSet = new HashSet(); + for (char j : J.toCharArray()) { + hashSet.add(j); + } + for (char s : S.toCharArray()) { + if (hashSet.contains(s)) { + result++; + } + } + return result; + } +} diff --git a/leetcode/math/No202.java b/leetcode/math/No202.java new file mode 100644 index 0000000..ab0724e --- /dev/null +++ b/leetcode/math/No202.java @@ -0,0 +1,24 @@ +package Algorithm.leetcode.math; + +import java.util.HashSet; +import java.util.Set; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No202 { + public boolean isHappy(int n) { + Set set = new HashSet(); + while (n != 1 && !set.contains(n)) { + set.add(n); + int temp = 0; + while (n != 0) { + temp += Math.pow(n % 10, 2); + n = n / 10; + } + n = temp; + } + return n == 1; + } +} diff --git a/leetcode/math/No231.java b/leetcode/math/No231.java new file mode 100644 index 0000000..95d09ef --- /dev/null +++ b/leetcode/math/No231.java @@ -0,0 +1,11 @@ +package Algorithm.leetcode.math; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No231 { + public boolean isPowerOfTwo(int n) { + return n > 0 && ((n & (n - 1)) == 0); + } +} diff --git a/leetcode/math/No258.java b/leetcode/math/No258.java new file mode 100644 index 0000000..ce3a5cd --- /dev/null +++ b/leetcode/math/No258.java @@ -0,0 +1,11 @@ +package Algorithm.leetcode.math; + +/** + * + * Created by tujietg on Nov 6, 2019 + */ +public class No258 { + public int addDigits(int num) { + return num == 0 ? 0 : (num % 9 == 0 ? 9 : num % 9); + } +} From 4e2f562949c1f0fd88ae7c57250b4123d7b2bb3a Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 6 Nov 2019 21:26:34 +0800 Subject: [PATCH 055/116] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/DP/No121.java | 29 ----------------------------- leetcode/DP/No122.java | 16 ---------------- leetcode/LinkedList/No234.java | 2 +- leetcode/hashtable/No202.java | 24 ------------------------ 4 files changed, 1 insertion(+), 70 deletions(-) delete mode 100644 leetcode/DP/No121.java delete mode 100644 leetcode/DP/No122.java delete mode 100644 leetcode/hashtable/No202.java diff --git a/leetcode/DP/No121.java b/leetcode/DP/No121.java deleted file mode 100644 index 43f36fb..0000000 --- a/leetcode/DP/No121.java +++ /dev/null @@ -1,29 +0,0 @@ -package Algorithm.leetcode.DP; - -public class No121 { - public int maxProfit(int[] prices) { - if (prices.length == 0) { - return 0; - } - int sum = 0; - int mid = prices[0]; - for (int i = 1; i < prices.length; i++) { - if (mid < prices[i]) { - sum = Math.max(sum, prices[i] - mid); - } else { - mid = prices[i]; - } - } - return sum; - } - - public int maxProfit01(int[] prices) { - int sum = 0; - for (int i = 0; i < prices.length; i++) { - for (int j = i + 1; j < prices.length; j++) { - sum = Math.max(prices[j] - prices[i], sum); - } - } - return sum; - } -} diff --git a/leetcode/DP/No122.java b/leetcode/DP/No122.java deleted file mode 100644 index badbfaf..0000000 --- a/leetcode/DP/No122.java +++ /dev/null @@ -1,16 +0,0 @@ -package Algorithm.leetcode.DP; - -public class No122 { - public int maxProfit(int[] prices) { - if (prices.length == 0 || prices.length == 1) { - return 0; - } - int sum = 0; - for (int i = 0; i < prices.length - 1; i++) { - if (prices[i + 1] > prices[i]) { - sum += prices[i + 1] - prices[i]; - } - } - return sum; - } -} diff --git a/leetcode/LinkedList/No234.java b/leetcode/LinkedList/No234.java index 139e99a..c22447e 100644 --- a/leetcode/LinkedList/No234.java +++ b/leetcode/LinkedList/No234.java @@ -12,7 +12,7 @@ public boolean isPalindrome(ListNode head) { hd = head; ListNode end = head; demo(end); - return a; + return a; } public void demo(ListNode end) { diff --git a/leetcode/hashtable/No202.java b/leetcode/hashtable/No202.java deleted file mode 100644 index 3b5dd8b..0000000 --- a/leetcode/hashtable/No202.java +++ /dev/null @@ -1,24 +0,0 @@ -package Algorithm.leetcode.hashtable; - -import java.util.HashSet; -import java.util.Set; - -/** - * - * Created by tujietg on Nov 6, 2019 - */ -public class No202 { - public boolean isHappy(int n) { - Set set = new HashSet(); - while (n != 1 && !set.contains(n)) { - set.add(n); - int temp = 0; - while (n != 0) { - temp += Math.pow(n % 10, 2); - n = n / 10; - } - n = temp; - } - return n == 1; - } -} From dca68653f703bccb6e2ec7b2ab72fb9140aecc87 Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 7 Nov 2019 09:34:51 +0800 Subject: [PATCH 056/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A7=A3=E9=A2=98?= =?UTF-8?q?=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ec60f18..e5c47c4 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:114道 +🧗‍♂️ 已解题目:115道 ------- From 130e4af7a21d7483a8f4a73b64455569557d4c17 Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 8 Nov 2019 09:40:07 +0800 Subject: [PATCH 057/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No448?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- leetcode/Array/No448.java | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 leetcode/Array/No448.java diff --git a/README.md b/README.md index e5c47c4..ee9be3b 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:10天 +✊ 不间断刷题天数:1天 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:115道 +🧗‍♂️ 已解题目:116道 ------- diff --git a/leetcode/Array/No448.java b/leetcode/Array/No448.java new file mode 100644 index 0000000..fbf8bab --- /dev/null +++ b/leetcode/Array/No448.java @@ -0,0 +1,33 @@ +package Algorithm.leetcode.Array; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * + * Created by tujietg on Nov 8, 2019 + */ +public class No448 { + public static List findDisappearedNumbers(int[] nums) { + List list = new ArrayList(); + Set hashSet = new HashSet(); + + for (int i = 0; i < nums.length; i++) { + hashSet.add(nums[i]); + } + int n = nums.length; + for (int i = 1; i <= n; i++) { + if (!hashSet.contains(i)) { + list.add(i); + } + } + return list; + } + + public static void main(String[] args) { + int[] param = new int[] { 4, 3, 2, 7, 8, 2, 3, 1 }; + findDisappearedNumbers(param); + } +} From 2731e4fe8e814ebf9598ac95a6ce745ceff4951b Mon Sep 17 00:00:00 2001 From: tujietg Date: Sun, 10 Nov 2019 12:55:07 +0800 Subject: [PATCH 058/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E9=A2=98=E7=9B=AE=E6=80=BB=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++++ leetcode/DP/No303.java | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ee9be3b..9003b59 100755 --- a/README.md +++ b/README.md @@ -25,6 +25,11 @@ | No88 | [Merge Sorted Array]() | [java]() | O(n) | O(1) | 插入之后采用Arrays.sort()排序。 | | No121 | [Best Time to Buy and Sell Stock]() | [java]() | O(n) | O(1) | 一次死循环,利用中间值做判断。 | | No122 | [Best Time to Buy and Sell Stock II]() | [java]() | O(n) | O(1) | 一次循环,判断后值比前值大。 | +| No167 | [两数之和 II - 输入有序数组](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No169.java) | O(N) | O(1) | 双指针进行操作。头和末尾同时开始。 | +| No169 | [求众数](https://leetcode-cn.com/problems/majority-element/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No169.java) | O(N) | O(1) | 计数,出现最多的数最后计数的值最大。 | +| No189 | [旋转数组](https://leetcode-cn.com/problems/rotate-array/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No189.java) | | | | +| | | | | | | +| | | | | | | ------- diff --git a/leetcode/DP/No303.java b/leetcode/DP/No303.java index e7ce9e9..1680c35 100644 --- a/leetcode/DP/No303.java +++ b/leetcode/DP/No303.java @@ -3,7 +3,7 @@ /** * 分析:sumRange被频繁的调用,这里采用构造方法时候就算法0到每个元素的值时间复杂度为O(n),这样sumRange方法每次调用的时间复杂度为常数。 * - * Created by tujietg on Jul 29, 2019 + * Created by tujietg on Jul 29, 20191 */ public class No303 { From 88ab2778f8209371e36ff365cfb8d6706dab8ce3 Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 12 Nov 2019 11:17:09 +0800 Subject: [PATCH 059/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No1002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/Array/No1002.java | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 leetcode/Array/No1002.java diff --git a/README.md b/README.md index 9003b59..0c1fd57 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:116道 +🧗‍♂️ 已解题目:117道 ------- diff --git a/leetcode/Array/No1002.java b/leetcode/Array/No1002.java new file mode 100644 index 0000000..3962aa8 --- /dev/null +++ b/leetcode/Array/No1002.java @@ -0,0 +1,46 @@ +package Algorithm.leetcode.Array; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * Created by tujietg on Nov 12, 2019 + */ +public class No1002 { + public static List commonChars(String[] A) { + int[] endArr = new int[26]; + // 找出第一组的数 + for (char c : A[0].toCharArray()) { + endArr[c - 'a']++; + } + for (int i = 1; i < A.length; i++) { + int[] midArr = new int[26]; + for (char c : A[i].toCharArray()) { + midArr[c - 'a']++; + } + for (int j = 0; j < 26; j++) { + if (endArr[j] == 0 || midArr[j] == 0) { + endArr[j] = 0; + } else { + endArr[j] = Math.min(endArr[j], midArr[j]); + } + } + } + List list = new ArrayList(); + for (int i = 0; i < 26; i++) { + if (endArr[i] != 0) { + for (int j = 0; j < endArr[i]; j++) { + list.add((char) (i + 'a') + ""); + } + } + } + return list; + } + + public static void main(String[] args) { + String[] str = new String[] { "daaccccd", "adacbdda", "abddbaba", "bacbcbcb", "bdaaaddc", "cdadacba", + "bacbdcda", "bacdaacd" }; + commonChars(str); + } +} From aed11251ed5fc97df60bf5d4dcecea4bb5d3debb Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 13 Nov 2019 11:16:16 +0800 Subject: [PATCH 060/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No118?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- leetcode/Array/No118.java | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 leetcode/Array/No118.java diff --git a/README.md b/README.md index 0c1fd57..4436a4a 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:1天 +✊ 不间断刷题天数:2天 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:117道 +🧗‍♂️ 已解题目:118道 ------- diff --git a/leetcode/Array/No118.java b/leetcode/Array/No118.java new file mode 100644 index 0000000..ae2940f --- /dev/null +++ b/leetcode/Array/No118.java @@ -0,0 +1,28 @@ +package Algorithm.leetcode.Array; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * Created by tujietg on Nov 13, 2019 + */ +public class No118 { + public List> generate(int numRows) { + List> result = new ArrayList>(); + List end, pre = null; + for (int i = 0; i < numRows; i++) { + end = new ArrayList(); + for (int j = 0; j <= i; j++) { + if (j == 0 || j == i) { + end.add(1); + } else { + end.add(pre.get(j - 1) + pre.get(j)); + } + } + pre = end; + result.add(end); + } + return result; + } +} From 8ad8fefdb224c54c455c2292586b8b2105164d7a Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 14 Nov 2019 09:42:18 +0800 Subject: [PATCH 061/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No532?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- leetcode/Array/No532.java | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 leetcode/Array/No532.java diff --git a/README.md b/README.md index 4436a4a..9ac552a 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:2天 +✊ 不间断刷题天数:3天 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:118道 +🧗‍♂️ 已解题目:119道 ------- diff --git a/leetcode/Array/No532.java b/leetcode/Array/No532.java new file mode 100644 index 0000000..cb136fb --- /dev/null +++ b/leetcode/Array/No532.java @@ -0,0 +1,26 @@ +package Algorithm.leetcode.Array; + +import java.util.HashSet; +import java.util.Set; + +/** + * + * Created by tujietg on Nov 14, 2019 + */ +public class No532 { + public int findPairs(int[] nums, int k) { + Set hashSet = new HashSet(); + for (int i = 0; i < nums.length; i++) { + for (int j = i + 1; j < nums.length; j++) { + if (Math.abs(nums[i] - nums[j]) == k) { + if (nums[i] > nums[j]) { + hashSet.add(nums[j]); + } else { + hashSet.add(nums[i]); + } + } + } + } + return hashSet.size(); + } +} From e7453ab2d5980b0e738d738742c57b67386a84dc Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 14 Nov 2019 10:01:35 +0800 Subject: [PATCH 062/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E7=A7=8D?= =?UTF-8?q?=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Array/No532.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/leetcode/Array/No532.java b/leetcode/Array/No532.java index cb136fb..5d404b5 100644 --- a/leetcode/Array/No532.java +++ b/leetcode/Array/No532.java @@ -23,4 +23,20 @@ public int findPairs(int[] nums, int k) { } return hashSet.size(); } + + public static int findPairs02(int[] nums, int k) { + if (k < 0) + return 0; + Set numbers = new HashSet<>(); + Set found = new HashSet<>(); + for (int n : nums) { + if (numbers.contains(n + k)) + found.add(n); + if (numbers.contains(n - k)) + found.add(n); + numbers.add(n - k); + } + return found.size(); + } + } From c683f4c9e41650b349ea1633e40892ba470ae66e Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 14 Nov 2019 14:04:38 +0800 Subject: [PATCH 063/116] Create main.yml --- .github/workflows/main.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..b2340b2 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,17 @@ +name: CI + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - name: Run a one-line script + run: echo Hello, world! + - name: Run a multi-line script + run: | + echo Add other actions to build, + echo test, and deploy your project. From 059c38ae8fd392e9328a9e18a3caaf46729b5726 Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 14 Nov 2019 17:40:58 +0800 Subject: [PATCH 064/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No566?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/Array/No566.java | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 leetcode/Array/No566.java diff --git a/README.md b/README.md index 9ac552a..0d45f09 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:119道 +🧗‍♂️ 已解题目:120道 ------- diff --git a/leetcode/Array/No566.java b/leetcode/Array/No566.java new file mode 100644 index 0000000..515184e --- /dev/null +++ b/leetcode/Array/No566.java @@ -0,0 +1,20 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 14, 2019 + */ +public class No566 { + public int[][] matrixReshape(int[][] nums, int r, int c) { + int m = nums.length; + int n = nums[0].length; + if (m * n != r * c) { + return nums; + } + int[][] endNums = new int[r][c]; + for (int i = 0; i < r * c; i++) { + endNums[i / c][i % c] = nums[i / n][i % n]; + } + return endNums; + } +} From 0166f54d063df7e724b1debf5a9183fef8befeaf Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 15 Nov 2019 10:25:48 +0800 Subject: [PATCH 065/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No581?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- leetcode/Array/No581.java | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 leetcode/Array/No581.java diff --git a/README.md b/README.md index 0d45f09..4dca453 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:3天 +✊ 不间断刷题天数:4天 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:120道 +🧗‍♂️ 已解题目:121道 ------- diff --git a/leetcode/Array/No581.java b/leetcode/Array/No581.java new file mode 100644 index 0000000..807cfad --- /dev/null +++ b/leetcode/Array/No581.java @@ -0,0 +1,29 @@ +package Algorithm.leetcode.Array; + +import java.util.Arrays; + +/** + * + * Created by tujietg on Nov 15, 2019 + */ +public class No581 { + public int findUnsortedSubarray(int[] nums) { + int start = nums.length; + int end = nums.length; + int[] cloneNums = nums.clone(); + Arrays.sort(cloneNums); + for (int i = 0; i < nums.length; i++) { + if (cloneNums[i] != nums[i]) { + start = i; + break; + } + } + for (int i = nums.length - 1; i >= 0; i--) { + if (nums[i] != cloneNums[i]) { + end = i; + break; + } + } + return start == end ? 0 : end - start + 1; + } +} From c0291b8ea88d352163d4ab3d4f46646a56c5e8c3 Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 15 Nov 2019 10:41:59 +0800 Subject: [PATCH 066/116] =?UTF-8?q?=E4=BF=AE=E6=94=B9Readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 4dca453..9041e4e 100755 --- a/README.md +++ b/README.md @@ -28,8 +28,6 @@ | No167 | [两数之和 II - 输入有序数组](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No169.java) | O(N) | O(1) | 双指针进行操作。头和末尾同时开始。 | | No169 | [求众数](https://leetcode-cn.com/problems/majority-element/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No169.java) | O(N) | O(1) | 计数,出现最多的数最后计数的值最大。 | | No189 | [旋转数组](https://leetcode-cn.com/problems/rotate-array/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No189.java) | | | | -| | | | | | | -| | | | | | | ------- From 9a51745ea7f48e11c4cf783885c631b50972ac64 Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 15 Nov 2019 17:16:38 +0800 Subject: [PATCH 067/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No605?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/Array/No605.java | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 leetcode/Array/No605.java diff --git a/README.md b/README.md index 9041e4e..9abf0cd 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:121道 +🧗‍♂️ 已解题目:122道 ------- diff --git a/leetcode/Array/No605.java b/leetcode/Array/No605.java new file mode 100644 index 0000000..3feefc6 --- /dev/null +++ b/leetcode/Array/No605.java @@ -0,0 +1,24 @@ +package Algorithm.leetcode.Array; + +/** + * + * Created by tujietg on Nov 15, 2019 + */ +public class No605 { + public boolean canPlaceFlowers(int[] flowerbed, int n) { + int count = 1; + int result = 0; + for (int i = 0; i < flowerbed.length; i++) { + if (flowerbed[i] == 0) { + count++; + } else { + result += (count - 1) / 2; + count = 0; + } + } + if (count != 0) { + result += count / 2; + } + return result >= n; + } +} From f216841c04bf993ee3c49f7cab21d129a9834398 Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 20 Nov 2019 09:08:09 +0800 Subject: [PATCH 068/116] =?UTF-8?q?=E6=80=BB=E7=BB=93=E6=A0=91=E5=BD=A2?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 18 +++++++++++++++++- leetcode/Tree/No100.java | 27 +++++++++++++++++++++++++-- leetcode/Tree/No104.java | 4 ++-- leetcode/Tree/No110.java | 29 ++++++++++++++++++++++------- leetcode/Tree/No111.java | 20 +++++++++++++++++++- 5 files changed, 85 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 9abf0cd..183df84 100755 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:4天 +✊ 不间断刷题天数:1天 🐘 最长连续刷题天数: 10天 @@ -48,3 +48,19 @@ | No67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [java]() | O(N) | O(1) | 相当于自己做了加法运算 | | No344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [java]() | O(N) | O(1) | 利用中间值进行修改。 | +---- + +#### Tree + +| # | Title | Solution | Time | Space | Note | +| ----- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ----- | ------------------------------------------------------------ | +| No100 | [相同的树](https://leetcode-cn.com/problems/same-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No100.java) | O(N) | O(1) | 很简单的使用递归,递归的条件,节点是否都为空,或者是否一个为空一个为非空。 | +| No101 | [对称二叉树](https://leetcode-cn.com/problems/symmetric-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No101.java) | O(N) | O(1) | 递归遍历,注意,比较的是相对称的节点。 | +| No104 | [二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No104.java) | O(N) | O(1) | 分割成每个小块,比较每个节点左子节点的层级多还是右子节点的层级多。 | +| No108 | [将有序数组转换为二叉搜索树](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No108.java) | O(N) | O(1) | 生成树的时候每次都去找最合适的放在中间的节点。 | +| No110 | [平衡二叉树](https://leetcode-cn.com/problems/balanced-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No110.java) | O(N) | O(1) | 每个节点的都需要比较左右节点。另外写一个方法计算子树的高度。 | +| | | | | | | +| | | | | | | +| | | | | | | +| | | | | | | + diff --git a/leetcode/Tree/No100.java b/leetcode/Tree/No100.java index 7b9b5f1..0d6759d 100644 --- a/leetcode/Tree/No100.java +++ b/leetcode/Tree/No100.java @@ -11,16 +11,39 @@ class TreeNode { } public class No100 { - public boolean isSameTree(TreeNode p, TreeNode q) { + static int i = 0; + + public static boolean isSameTree(TreeNode p, TreeNode q) { + + System.out.println(++i); + if (p == null && q == null) { return true; } + if (p == null || q == null) { return false; } + + boolean taga = isSameTree(p.left, q.left); + + boolean tagb = isSameTree(p.right, q.right); + if (p.val == q.val) { - return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); + return taga && tagb; } return false; } + + public static void main(String[] args) { + TreeNode root1 = new TreeNode(1); + root1.left = new TreeNode(2); + root1.right = new TreeNode(3); + + TreeNode root2 = new TreeNode(1); + root2.left = new TreeNode(2); + root2.right = new TreeNode(3); + + isSameTree(root1, root2); + } } diff --git a/leetcode/Tree/No104.java b/leetcode/Tree/No104.java index 06d1cbc..5e24255 100644 --- a/leetcode/Tree/No104.java +++ b/leetcode/Tree/No104.java @@ -8,7 +8,7 @@ class TreeNodeInt { int val; TreeNodeInt left; - int right; + TreeNodeInt right; TreeNodeInt(int x) { val = x; @@ -20,6 +20,6 @@ public int maxDepth(TreeNodeInt root) { if (root == null) { return 0; } - return 1 + Math.max(maxDepth(root.left), (root.right)); + return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } } diff --git a/leetcode/Tree/No110.java b/leetcode/Tree/No110.java index db30c7c..aea1bd1 100644 --- a/leetcode/Tree/No110.java +++ b/leetcode/Tree/No110.java @@ -5,14 +5,14 @@ * Created by tujietg on Nov 6, 2019 */ public class No110 { - public boolean isBalanced(TreeNode root) { - if (root == null) { + public static boolean isBalanced(TreeNode root) { + if (root == null) return true; - } - if (depth(root.left) - depth(root.right) > 1 || depth(root.right) - depth(root.left) > 1) { - return false; - } - return isBalanced(root.right) && isBalanced(root.left); + int a = depth(root.left); + System.out.println(a); + int b = depth(root.right); + System.out.println(b); + return Math.abs(a - b) <= 1; } // 求出子树的高度 @@ -23,4 +23,19 @@ public static int depth(TreeNode root) { // 通过递归来求出每个子树的高度 return Math.max(depth(root.left), depth(root.right)) + 1; } + + // [1,2,2,3,null ,null,3, 4,null,null,4] + public static void main(String[] args) { + TreeNode root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(2); + root.left.left = new TreeNode(3); + root.left.right = null; + root.right.left = null; + root.right.right = new TreeNode(3); + root.left.left.left = new TreeNode(4); + root.right.right.right = new TreeNode(4); + System.out.println(isBalanced(root)); + } + } diff --git a/leetcode/Tree/No111.java b/leetcode/Tree/No111.java index 6b556d1..43b5d4f 100644 --- a/leetcode/Tree/No111.java +++ b/leetcode/Tree/No111.java @@ -5,7 +5,13 @@ * Created by tujietg on Nov 6, 2019 */ public class No111 { - public int minDepth(TreeNode root) { + + static int i = 0; + + public static int minDepth(TreeNode root) { + + System.out.println(++i); + if (root == null) { return 0; } @@ -13,4 +19,16 @@ public int minDepth(TreeNode root) { int right = minDepth(root.right); return (left == 0 || right == 0) ? left + right + 1 : Math.min(left + 1, right + 1); } + + // [3,9,20,null,null,15,7] + public static void main(String[] args) { + TreeNode root = new TreeNode(3); + root.left = new TreeNode(9); + root.right = new TreeNode(20); + root.left.left = null; + root.left.right = null; + root.right.left = new TreeNode(15); + root.right.right = new TreeNode(7); + System.out.println(minDepth(root)); + } } From 28a1ce18e9bd91fbd8bd949f04beb6c86204459a Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 20 Nov 2019 11:25:05 +0800 Subject: [PATCH 069/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- leetcode/Tree/No111.java | 4 +--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 183df84..c8dd2e4 100755 --- a/README.md +++ b/README.md @@ -59,8 +59,8 @@ | No104 | [二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No104.java) | O(N) | O(1) | 分割成每个小块,比较每个节点左子节点的层级多还是右子节点的层级多。 | | No108 | [将有序数组转换为二叉搜索树](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No108.java) | O(N) | O(1) | 生成树的时候每次都去找最合适的放在中间的节点。 | | No110 | [平衡二叉树](https://leetcode-cn.com/problems/balanced-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No110.java) | O(N) | O(1) | 每个节点的都需要比较左右节点。另外写一个方法计算子树的高度。 | -| | | | | | | -| | | | | | | -| | | | | | | -| | | | | | | +| No111 | [二叉树的最小深度](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No111.java) | O(N) | O(1) | 计算每个节点到最底节点的深度。 | +| No112 | [路径总和](https://leetcode-cn.com/problems/path-sum/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No112.java) | O(N) | O(1) | 递归的条件是,和减去当前节点的值最终等于0。 | +| No226 | [翻转二叉树](https://leetcode-cn.com/problems/invert-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No226.java) | O(N) | O(1) | 从最底层的子节点开始旋转。 | +| No617 | [合并二叉树](https://leetcode-cn.com/problems/merge-two-binary-trees/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No617.java) | O(N) | O(1) | 计算两个树合并节点的值,然后采用递归依次向下计算。 | diff --git a/leetcode/Tree/No111.java b/leetcode/Tree/No111.java index 43b5d4f..5f036ac 100644 --- a/leetcode/Tree/No111.java +++ b/leetcode/Tree/No111.java @@ -9,12 +9,10 @@ public class No111 { static int i = 0; public static int minDepth(TreeNode root) { - - System.out.println(++i); - if (root == null) { return 0; } + int left = minDepth(root.left); int right = minDepth(root.right); return (left == 0 || right == 0) ? left + right + 1 : Math.min(left + 1, right + 1); From 9c713f359e2d48a21197d6b38f84a30104f7c8f0 Mon Sep 17 00:00:00 2001 From: tujietg Date: Sun, 15 Dec 2019 09:30:53 +0800 Subject: [PATCH 070/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No235?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + leetcode/Tree/No235.java | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 leetcode/Tree/No235.java diff --git a/README.md b/README.md index c8dd2e4..018f87a 100755 --- a/README.md +++ b/README.md @@ -62,5 +62,6 @@ | No111 | [二叉树的最小深度](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No111.java) | O(N) | O(1) | 计算每个节点到最底节点的深度。 | | No112 | [路径总和](https://leetcode-cn.com/problems/path-sum/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No112.java) | O(N) | O(1) | 递归的条件是,和减去当前节点的值最终等于0。 | | No226 | [翻转二叉树](https://leetcode-cn.com/problems/invert-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No226.java) | O(N) | O(1) | 从最底层的子节点开始旋转。 | +| No235 | [ 二叉搜索树的最近公共祖先](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No235.java) | O(N) | O(1) | 递归寻找合适的root节点。 | | No617 | [合并二叉树](https://leetcode-cn.com/problems/merge-two-binary-trees/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No617.java) | O(N) | O(1) | 计算两个树合并节点的值,然后采用递归依次向下计算。 | diff --git a/leetcode/Tree/No235.java b/leetcode/Tree/No235.java new file mode 100644 index 0000000..c8cd470 --- /dev/null +++ b/leetcode/Tree/No235.java @@ -0,0 +1,17 @@ +package Algorithm.leetcode.Tree; + +/** + * + * Created by tujietg on Dec 15, 2019 + */ +public class No235 { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (root.val > p.val && root.val > q.val) { + return lowestCommonAncestor(root.left, p, q); + } else if (root.val < q.val && root.val < p.val) { + return lowestCommonAncestor(root.right, p, q); + } else { + return root; + } + } +} From 70045f8225cbd0f3b8deba9e6bf9f4dd4b42eeb7 Mon Sep 17 00:00:00 2001 From: tujietg Date: Sun, 15 Dec 2019 09:35:05 +0800 Subject: [PATCH 071/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=B7=E9=A2=98?= =?UTF-8?q?=E6=A0=91=E6=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 018f87a..5f3d1b0 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:122道 +🧗‍♂️ 已解题目:123道 ------- From ea4d848a89653dbba2f01f431c9c644272c19c2a Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 16 Dec 2019 11:08:52 +0800 Subject: [PATCH 072/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0257?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++-- leetcode/Tree/No257.java | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 leetcode/Tree/No257.java diff --git a/README.md b/README.md index 5f3d1b0..bdcb60a 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:1天 +✊ 不间断刷题天数:2天 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:123道 +🧗‍♂️ 已解题目:124道 ------- @@ -63,5 +63,6 @@ | No112 | [路径总和](https://leetcode-cn.com/problems/path-sum/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No112.java) | O(N) | O(1) | 递归的条件是,和减去当前节点的值最终等于0。 | | No226 | [翻转二叉树](https://leetcode-cn.com/problems/invert-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No226.java) | O(N) | O(1) | 从最底层的子节点开始旋转。 | | No235 | [ 二叉搜索树的最近公共祖先](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No235.java) | O(N) | O(1) | 递归寻找合适的root节点。 | +| No257 | [二叉树的所有路径](https://leetcode-cn.com/problems/binary-tree-paths/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No257.java) | O(N) | O(1) | 递归终止条件是root左右节点为空。自上往下的递归。 | | No617 | [合并二叉树](https://leetcode-cn.com/problems/merge-two-binary-trees/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No617.java) | O(N) | O(1) | 计算两个树合并节点的值,然后采用递归依次向下计算。 | diff --git a/leetcode/Tree/No257.java b/leetcode/Tree/No257.java new file mode 100644 index 0000000..af077bb --- /dev/null +++ b/leetcode/Tree/No257.java @@ -0,0 +1,31 @@ +package Algorithm.leetcode.Tree; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * Created by tujietg on Dec 16, 2019 + */ +public class No257 { + public List binaryTreePaths(TreeNode root) { + List endList = new ArrayList(); + if (root != null) { + fun(root, "", endList); + } + return endList; + } + + void fun(TreeNode root, String path, List endList) { + if (root.left == null && root.right == null) { + endList.add(path + root.val); + } + if (root.left != null) { + fun(root.left, path + root.val + "->", endList); + } + + if (root.right != null) { + fun(root.right, path + root.val + "->", endList); + } + } +} From 7dbfda636fb49163c7a15370d9e6edd43be77944 Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 10 Jan 2020 11:18:43 +0800 Subject: [PATCH 073/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No938?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Tree/No938.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 leetcode/Tree/No938.java diff --git a/leetcode/Tree/No938.java b/leetcode/Tree/No938.java new file mode 100644 index 0000000..3eded25 --- /dev/null +++ b/leetcode/Tree/No938.java @@ -0,0 +1,16 @@ +package Algorithm.leetcode.Tree; + +/** + * Created by tujietg on Jan 10, 2020 + */ +public class No938 { + public int rangeSumBST(TreeNode root, int L, int R) { + if (root == null) + return 0; + if (root.val < L) + return rangeSumBST(root.right, L, R); + if (root.val > R) + return rangeSumBST(root.left, L, R); + return root.val + rangeSumBST(root.left, L, R) + rangeSumBST(root.right, L, R); + } +} From 2747ee50dc65470823bd78e97948098ad96d832c Mon Sep 17 00:00:00 2001 From: tujietg Date: Sun, 12 Jan 2020 18:20:08 +0800 Subject: [PATCH 074/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0no404?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Tree/No404.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 leetcode/Tree/No404.java diff --git a/leetcode/Tree/No404.java b/leetcode/Tree/No404.java new file mode 100644 index 0000000..700f227 --- /dev/null +++ b/leetcode/Tree/No404.java @@ -0,0 +1,22 @@ +package Algorithm.leetcode.Tree; + +/** + * + * Created by tujietg on Jan 12, 2020 + */ +public class No404 { + public int sumOfLeftLeaves(TreeNode root) { + if (root == null) + return 0; + int answer = 0; + if (root.left != null) { + if (root.left.left == null && root.left.right == null) { + answer += root.left.val; + } else { + answer += sumOfLeftLeaves(root.left); + } + } + answer += sumOfLeftLeaves(root.right); + return answer; + } +} From ed5b787dfbdd551026e464a9847ad8f97d5bd343 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 20 Jan 2020 14:02:23 +0800 Subject: [PATCH 075/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No107?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Tree/No107.java | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 leetcode/Tree/No107.java diff --git a/leetcode/Tree/No107.java b/leetcode/Tree/No107.java new file mode 100644 index 0000000..9de2389 --- /dev/null +++ b/leetcode/Tree/No107.java @@ -0,0 +1,49 @@ +package Algorithm.leetcode.Tree; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +/** + * No107题解 + * + * Created by tujietg on Jan 20, 2020 + */ +public class No107 { + + public List> levelOrderBottom(TreeNode root) { + Queue queue = new LinkedList(); + List> result = new ArrayList>(); + + if (root == null) { + return result; + } + queue.add(root); + while (!queue.isEmpty()) { + int length = queue.size(); + List midList = new ArrayList(); + for (int i = 0; i < length; i++) { + if (queue.peek().left != null) { + queue.offer(queue.peek().left); + } + if (queue.peek().right != null) { + queue.offer(queue.peek().right); + } + midList.add(queue.poll().val); + } + result.add(0, midList); + } + return result; + } + + public static void main(String[] args) { + TreeNode root = new TreeNode(3); + root.left = new TreeNode(9); + root.right = new TreeNode(20); + root.right.left = new TreeNode(15); + root.right.right = new TreeNode(7); + new No107().levelOrderBottom(root); + } + +} From 667f28d3000137341f2890eeefeef2add32bd3a2 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 20 Jan 2020 15:03:44 +0800 Subject: [PATCH 076/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No1302?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Tree/No1302.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 leetcode/Tree/No1302.java diff --git a/leetcode/Tree/No1302.java b/leetcode/Tree/No1302.java new file mode 100644 index 0000000..138e5ec --- /dev/null +++ b/leetcode/Tree/No1302.java @@ -0,0 +1,29 @@ +package Algorithm.leetcode.Tree; + +import java.util.LinkedList; +import java.util.Queue; + +/** + * No1302 + * + * Created by tujietg on Jan 20, 2020 + */ +public class No1302 { + public int deepestLeavesSum(TreeNode root) { + int reslut = 0, i; + Queue queue = new LinkedList(); + queue.add(root); + while (!queue.isEmpty()) { + int len = queue.size(); + for (i = len - 1, reslut = 0; i >= 0; i--) { + TreeNode node = queue.poll(); + reslut += node.val; + if (node.left != null) + queue.add(node.left); + if (node.right != null) + queue.add(node.right); + } + } + return reslut; + } +} From 5f9561c39f42fbbd36f3b9309b99027268c7e760 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 20 Jan 2020 15:26:30 +0800 Subject: [PATCH 077/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No1315?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Tree/No1315.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 leetcode/Tree/No1315.java diff --git a/leetcode/Tree/No1315.java b/leetcode/Tree/No1315.java new file mode 100644 index 0000000..253fb13 --- /dev/null +++ b/leetcode/Tree/No1315.java @@ -0,0 +1,19 @@ +package Algorithm.leetcode.Tree; + +/** + * No1315 + * + * Created by tujietg on Jan 20, 2020 + */ +public class No1315 { + public int sumEvenGrandparent(TreeNode root) { + return helper(root, 1, 1); + } + + public int helper(TreeNode node, int p, int gp) { + if (node == null) { + return 0; + } + return helper(node.left, node.val, p) + helper(node.right, node.val, p) + (gp % 2 == 0 ? node.val : 0); + } +} From d18515d4455a921017ed9143f942435c0f9e5888 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 20 Jan 2020 15:44:23 +0800 Subject: [PATCH 078/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No700?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Tree/No700.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 leetcode/Tree/No700.java diff --git a/leetcode/Tree/No700.java b/leetcode/Tree/No700.java new file mode 100644 index 0000000..f64a1e7 --- /dev/null +++ b/leetcode/Tree/No700.java @@ -0,0 +1,22 @@ +package Algorithm.leetcode.Tree; + +/** + * No700 + * + * Created by tujietg on Jan 20, 2020 + */ +public class No700 { + public TreeNode searchBST(TreeNode root, int val) { + if (root == null) { + return root; + } + if (root.val == val) { + return root; + } else if (root.val > val) { + return searchBST(root.left, val); + } else if (root.val < val) { + return searchBST(root.right, val); + } + return null; + } +} From 125e1de063a0ca671e385e1f102aeae57014fb3c Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 21 Jan 2020 10:13:16 +0800 Subject: [PATCH 079/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No589=20No590?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Tree/No589.java | 43 ++++++++++++++++++++++++++++++++++++++++ leetcode/Tree/No590.java | 25 +++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 leetcode/Tree/No589.java create mode 100644 leetcode/Tree/No590.java diff --git a/leetcode/Tree/No589.java b/leetcode/Tree/No589.java new file mode 100644 index 0000000..9e630c4 --- /dev/null +++ b/leetcode/Tree/No589.java @@ -0,0 +1,43 @@ +package Algorithm.leetcode.Tree; + +import java.util.ArrayList; +import java.util.List; + +/** + * No589 + * + * Created by tujietg on Jan 21, 2020 + */ +class Node { + public int val; + public List children; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, List _children) { + val = _val; + children = _children; + } +} + +public class No589 { + + List list = new ArrayList(); + + public List preorder(Node root) { + if (root == null) { + return list; + } + list.add(root.val); + for (Node node : root.children) { + preorder(node); + } + return list; + } + +} diff --git a/leetcode/Tree/No590.java b/leetcode/Tree/No590.java new file mode 100644 index 0000000..fdb76f3 --- /dev/null +++ b/leetcode/Tree/No590.java @@ -0,0 +1,25 @@ +package Algorithm.leetcode.Tree; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * Created by tujietg on Jan 21, 2020 + */ +public class No590 { + + List list = new ArrayList(); + + public List preorder(Node root) { + if (root == null) { + return list; + } + for (Node node : root.children) { + preorder(node); + } + list.add(root.val); + return list; + } + +} From f82635da0b69bfa04ed240c775bed86da6385cc8 Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 21 Jan 2020 11:24:30 +0800 Subject: [PATCH 080/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No965?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Tree/No965.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 leetcode/Tree/No965.java diff --git a/leetcode/Tree/No965.java b/leetcode/Tree/No965.java new file mode 100644 index 0000000..90c11cf --- /dev/null +++ b/leetcode/Tree/No965.java @@ -0,0 +1,22 @@ +package Algorithm.leetcode.Tree; + +/** + * No965 + * + * Created by tujietg on Jan 21, 2020 + */ +public class No965 { + public boolean isUnivalTree(TreeNode root) { + if (root == null) { + return true; + } + if (root.left != null && root.left.val != root.val) { + return false; + } + if (root.right != null && root.right.val != root.val) { + return false; + } + + return isUnivalTree(root.left) && isUnivalTree(root.right); + } +} From e88f0fa6690d57a146e14441ac887ca686ee35cc Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 22 Jan 2020 18:38:13 +0800 Subject: [PATCH 081/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No559?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Tree/No559.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 leetcode/Tree/No559.java diff --git a/leetcode/Tree/No559.java b/leetcode/Tree/No559.java new file mode 100644 index 0000000..a661682 --- /dev/null +++ b/leetcode/Tree/No559.java @@ -0,0 +1,23 @@ +package Algorithm.leetcode.Tree; + +/** + * No559 + * + * Created by tujietg on Jan 22, 2020 + */ +public class No559 { + public int maxDepth(Node root) { + if (root == null) { + return 0; + } + int end = 0; + for (Node node : root.children) { + int value = maxDepth(node); + + if (value > end) { + end = value; + } + } + return end + 1; + } +} From 4ddb1347531b48a3df02d06b77f9ccfb913b51cf Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 23 Jan 2020 19:17:50 +0800 Subject: [PATCH 082/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No10356?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/Tree/No1305.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 leetcode/Tree/No1305.java diff --git a/leetcode/Tree/No1305.java b/leetcode/Tree/No1305.java new file mode 100644 index 0000000..917c6ef --- /dev/null +++ b/leetcode/Tree/No1305.java @@ -0,0 +1,31 @@ +package Algorithm.leetcode.Tree; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * No1305 + * + * Created by tujietg on Jan 23, 2020 + */ +public class No1305 { + List list = new ArrayList(); + + public List getAllElements(TreeNode root1, TreeNode root2) { + addList(root1); + addList(root2); + Collections.sort(list); + return list; + } + + public void addList(TreeNode node) { + if (node == null) { + return; + } + list.add(node.val); + addList(node.left); + addList(node.right); + } + +} From 10789903ece3e1d769c026735072f1a1242bd8ab Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 23 Jan 2020 19:21:26 +0800 Subject: [PATCH 083/116] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=A9=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bdcb60a..764f3f7 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:2天 +✊ 不间断刷题天数:4天 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:124道 +🧗‍♂️ 已解题目:135道 ------- From 5cb69146ed39f3d07047863f7f297c7608cbde85 Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 24 Jan 2020 20:00:20 +0800 Subject: [PATCH 084/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=88=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 764f3f7..54c6a6a 100755 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:4天 +✊ 不间断刷题天数:1天 🐘 最长连续刷题天数: 10天 From bf98265802d5a3f9eeb4e5ed774ebc8629274d80 Mon Sep 17 00:00:00 2001 From: tujietg Date: Sun, 26 Jan 2020 07:54:41 +0800 Subject: [PATCH 085/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A094?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/Tree/No94.java | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 leetcode/Tree/No94.java diff --git a/README.md b/README.md index 54c6a6a..e2cf12c 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数: 10天 -🧗‍♂️ 已解题目:135道 +🧗‍♂️ 已解题目:136道 ------- diff --git a/leetcode/Tree/No94.java b/leetcode/Tree/No94.java new file mode 100644 index 0000000..dc578d3 --- /dev/null +++ b/leetcode/Tree/No94.java @@ -0,0 +1,23 @@ +package Algorithm.leetcode.Tree; + +import java.util.ArrayList; +import java.util.List; + +/** + * No94 + * + * Created by tujietg on Jan 26, 2020 + */ +public class No94 { + List list = new ArrayList(); + + public List inorderTraversal(TreeNode root) { + if (root == null) { + return list; + } + inorderTraversal(root.left); + list.add(root.val); + inorderTraversal(root.right); + return list; + } +} From ca701e019d537ed21f100da2ea99bc8231885007 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 27 Jan 2020 19:47:53 +0800 Subject: [PATCH 086/116] =?UTF-8?q?=E4=BF=AE=E6=94=B9readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e2cf12c..cc55014 100755 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:1天 +✊ 不间断刷题天数:2天 🐘 最长连续刷题天数: 10天 From 178df9c125ba414091bf9de2bd525e4662da0044 Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 28 Jan 2020 19:08:54 +0800 Subject: [PATCH 087/116] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cc55014..7eedcce 100755 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:2天 +✊ 不间断刷题天数:0天 🐘 最长连续刷题天数: 10天 From dff2cc0e85760515a34271cc53defc3bdfcafdbc Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 29 Jan 2020 22:47:45 +0800 Subject: [PATCH 088/116] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E9=A2=98=E7=9B=AE=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7eedcce..ed2a493 100755 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ 📖 [算法复杂度和时间复杂度](https://zhuanlan.zhihu.com/p/50479555) -✊ 不间断刷题天数:0天 +✊ 不间断刷题天数:1天 -🐘 最长连续刷题天数: 10天 +🐘 最长连续刷题天数: 11天 🧗‍♂️ 已解题目:136道 From 5b116a38bda62dc6bf4b5f6907217b3c368b19b9 Mon Sep 17 00:00:00 2001 From: tujietg Date: Sat, 1 Feb 2020 22:12:09 +0800 Subject: [PATCH 089/116] =?UTF-8?q?=E4=BF=AE=E6=94=B9readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 92 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index ed2a493..edb5934 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# leetcode +44leetcode 🚴‍♀️ I like to brush leetcode, it is a way of my pastime. I really **enjoy** it, I will always update it. @@ -14,55 +14,73 @@ #### Array -| # | Title | Solution | Time | Space | Note | -| ----- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ----- | ------------------------------------------------------------ | -| No01 | [Two Sum](https://leetcode.com/problems/two-sum/) | [java](https://github.com/tujietg/Algorithm/blob/master/leetcode/Array/No01.java) | O(n) | O(n) | 采用map存储值和下标,然后再次遍历,判断得到结果。 | -| No26 | [Remove Duplicates from Sorted Array]() | [java]() | O(n) | O(1) | 定义i来记录不相等的个数,因为记录从下标0开始,最后长度需要加1. | -| No27 | [Remove Element]() | [java]() | O(n) | O(1) | 采用变量来记录相同的值。 | -| No35 | [Search Insert Position]() | [java]() | O(n) | O(1) | 1⃣ 找到给定数大于数组的元素。2⃣ 遍历,一个一个条件的判断。 | -| No53 | [Maximum Subarray]() | [java]() | O(n) | O(1) | 设置max字段的初始值为最小的整数。 | -| No66 | [Plus One]() | [java]() | O(n) | O(n) | 该算法解法中做了合适的小于9的判断。 | -| No88 | [Merge Sorted Array]() | [java]() | O(n) | O(1) | 插入之后采用Arrays.sort()排序。 | -| No121 | [Best Time to Buy and Sell Stock]() | [java]() | O(n) | O(1) | 一次死循环,利用中间值做判断。 | -| No122 | [Best Time to Buy and Sell Stock II]() | [java]() | O(n) | O(1) | 一次循环,判断后值比前值大。 | -| No167 | [两数之和 II - 输入有序数组](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No169.java) | O(N) | O(1) | 双指针进行操作。头和末尾同时开始。 | -| No169 | [求众数](https://leetcode-cn.com/problems/majority-element/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No169.java) | O(N) | O(1) | 计数,出现最多的数最后计数的值最大。 | -| No189 | [旋转数组](https://leetcode-cn.com/problems/rotate-array/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No189.java) | | | | +| # | Title | Solution | Note | +| ----- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | +| No01 | [Two Sum](https://leetcode.com/problems/two-sum/) | [java](https://github.com/tujietg/Algorithm/blob/master/leetcode/Array/No01.java) | 采用map存储值和下标,然后再次遍历,判断得到结果。 | +| No26 | [Remove Duplicates from Sorted Array]() | [java]() | 定义i来记录不相等的个数,因为记录从下标0开始,最后长度需要加1. | +| No27 | [Remove Element]() | [java]() | 采用变量来记录相同的值。 | +| No35 | [Search Insert Position]() | [java]() | 1⃣ 找到给定数大于数组的元素。2⃣ 遍历,一个一个条件的判断。 | +| No53 | [Maximum Subarray]() | [java]() | 设置max字段的初始值为最小的整数。 | +| No66 | [Plus One]() | [java]() | 该算法解法中做了合适的小于9的判断。 | +| No88 | [Merge Sorted Array]() | [java]() | 插入之后采用Arrays.sort()排序。 | +| No121 | [Best Time to Buy and Sell Stock]() | [java]() | 一次死循环,利用中间值做判断。 | +| No122 | [Best Time to Buy and Sell Stock II]() | [java]() | 一次循环,判断后值比前值大。 | +| No167 | [两数之和 II - 输入有序数组](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No169.java) | 双指针进行操作。头和末尾同时开始。 | +| No169 | [求众数](https://leetcode-cn.com/problems/majority-element/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No169.java) | 计数,出现最多的数最后计数的值最大。 | +| No189 | [旋转数组](https://leetcode-cn.com/problems/rotate-array/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Array/No189.java) | | ------- #### Dynamic Programming -| # | Title | Solution | Time | Space | Note | -| ------ | ------------------------------------------------------------ | -------- | ---- | ----- | ------------------------------------ | -| No303 | [Range Sum Query](https://leetcode.com/problems/range-sum-query-immutable/) | [java]() | O(1) | O(n) | sumRange被频繁的调用。 | -| No746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [java]() | O(N) | O(n) | 先计算到达每个楼梯最小数字,在做减法 | -| No1025 | [Divisor Game](https://leetcode.com/problems/divisor-game/) | [java]() | O(1) | O(1) | | +| # | Title | Solution | Note | +| ------ | ------------------------------------------------------------ | -------- | ------------------------------------ | +| No303 | [Range Sum Query](https://leetcode.com/problems/range-sum-query-immutable/) | [java]() | sumRange被频繁的调用。 | +| No746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [java]() | 先计算到达每个楼梯最小数字,在做减法 | +| No1025 | [Divisor Game](https://leetcode.com/problems/divisor-game/) | [java]() | | ----- #### String -| # | Title | Solution | Time | Space | Note | -| ----- | ------------------------------------------------------------ | -------- | ---- | ----- | ---------------------- | -| No67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [java]() | O(N) | O(1) | 相当于自己做了加法运算 | -| No344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [java]() | O(N) | O(1) | 利用中间值进行修改。 | +| # | Title | Solution | Note | +| ----- | ------------------------------------------------------------ | -------- | ---------------------- | +| No67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [java]() | 相当于自己做了加法运算 | +| No344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [java]() | 利用中间值进行修改。 | ---- #### Tree -| # | Title | Solution | Time | Space | Note | -| ----- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ----- | ------------------------------------------------------------ | -| No100 | [相同的树](https://leetcode-cn.com/problems/same-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No100.java) | O(N) | O(1) | 很简单的使用递归,递归的条件,节点是否都为空,或者是否一个为空一个为非空。 | -| No101 | [对称二叉树](https://leetcode-cn.com/problems/symmetric-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No101.java) | O(N) | O(1) | 递归遍历,注意,比较的是相对称的节点。 | -| No104 | [二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No104.java) | O(N) | O(1) | 分割成每个小块,比较每个节点左子节点的层级多还是右子节点的层级多。 | -| No108 | [将有序数组转换为二叉搜索树](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No108.java) | O(N) | O(1) | 生成树的时候每次都去找最合适的放在中间的节点。 | -| No110 | [平衡二叉树](https://leetcode-cn.com/problems/balanced-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No110.java) | O(N) | O(1) | 每个节点的都需要比较左右节点。另外写一个方法计算子树的高度。 | -| No111 | [二叉树的最小深度](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No111.java) | O(N) | O(1) | 计算每个节点到最底节点的深度。 | -| No112 | [路径总和](https://leetcode-cn.com/problems/path-sum/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No112.java) | O(N) | O(1) | 递归的条件是,和减去当前节点的值最终等于0。 | -| No226 | [翻转二叉树](https://leetcode-cn.com/problems/invert-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No226.java) | O(N) | O(1) | 从最底层的子节点开始旋转。 | -| No235 | [ 二叉搜索树的最近公共祖先](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No235.java) | O(N) | O(1) | 递归寻找合适的root节点。 | -| No257 | [二叉树的所有路径](https://leetcode-cn.com/problems/binary-tree-paths/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No257.java) | O(N) | O(1) | 递归终止条件是root左右节点为空。自上往下的递归。 | -| No617 | [合并二叉树](https://leetcode-cn.com/problems/merge-two-binary-trees/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No617.java) | O(N) | O(1) | 计算两个树合并节点的值,然后采用递归依次向下计算。 | +| # | Title | Solution | Note | +| ----- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | +| No100 | [相同的树](https://leetcode-cn.com/problems/same-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No100.java) | 很简单的使用递归,递归的条件,节点是否都为空,或者是否一个为空一个为非空。 | +| No101 | [对称二叉树](https://leetcode-cn.com/problems/symmetric-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No101.java) | 递归遍历,注意,比较的是相对称的节点。 | +| No104 | [二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No104.java) | 分割成每个小块,比较每个节点左子节点的层级多还是右子节点的层级多。 | +| No108 | [将有序数组转换为二叉搜索树](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No108.java) | 生成树的时候每次都去找最合适的放在中间的节点。 | +| No110 | [平衡二叉树](https://leetcode-cn.com/problems/balanced-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No110.java) | 每个节点的都需要比较左右节点。另外写一个方法计算子树的高度。 | +| No111 | [二叉树的最小深度](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No111.java) | 计算每个节点到最底节点的深度。 | +| No112 | [路径总和](https://leetcode-cn.com/problems/path-sum/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No112.java) | 递归的条件是,和减去当前节点的值最终等于0。 | +| No226 | [翻转二叉树](https://leetcode-cn.com/problems/invert-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No226.java) | 从最底层的子节点开始旋转。 | +| No235 | [ 二叉搜索树的最近公共祖先](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No235.java) | 递归寻找合适的root节点。 | +| No257 | [二叉树的所有路径](https://leetcode-cn.com/problems/binary-tree-paths/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No257.java) | 递归终止条件是root左右节点为空。自上往下的递归。 | +| No617 | [合并二叉树](https://leetcode-cn.com/problems/merge-two-binary-trees/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No617.java) | 计算两个树合并节点的值,然后采用递归依次向下计算。 | +| No94 | [94. 二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No94.java) | 直接使用递归解决。 | +| No404 | [404. 左叶子之和](https://leetcode-cn.com/problems/sum-of-left-leaves/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No404.java) | 可以参考全路径求和方法,增加判断条件 true 和false。 | +| No606 | [606. 根据二叉树创建字符串](https://leetcode-cn.com/problems/construct-string-from-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No606.java) | 递归,根据是否为“”来判断采用什么样的方式。(左右节点是否为空所带来的字符串不一样) | +| | | | | +| | | | | +| | | | | +| | | | | +| | | | | +| | | | | +| | | | | +| | | | | +| | | | | +| | | | | +| | | | | +| | | | | +| | | | | +| | | | | +| | | | | From 19f12892a0d898df77bddb86fea93f09d8a4b5c7 Mon Sep 17 00:00:00 2001 From: tujietg Date: Sun, 2 Feb 2020 22:18:36 +0800 Subject: [PATCH 090/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=80=BB=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index edb5934..bde32de 100755 --- a/README.md +++ b/README.md @@ -68,8 +68,8 @@ | No94 | [94. 二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No94.java) | 直接使用递归解决。 | | No404 | [404. 左叶子之和](https://leetcode-cn.com/problems/sum-of-left-leaves/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No404.java) | 可以参考全路径求和方法,增加判断条件 true 和false。 | | No606 | [606. 根据二叉树创建字符串](https://leetcode-cn.com/problems/construct-string-from-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No606.java) | 递归,根据是否为“”来判断采用什么样的方式。(左右节点是否为空所带来的字符串不一样) | -| | | | | -| | | | | +| No637 | [637. 二叉树的层平均值](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No637.java) | 采用队列的方式解决。 | +| No653 | [653. 两数之和 IV - 输入 BST](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No653.java) | | | | | | | | | | | | | | | | | From efd3bad317ee994e6812dffe8f5b1fc45b906392 Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 5 Feb 2020 17:41:56 +0800 Subject: [PATCH 091/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No145?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- leetcode/Tree/No145.java | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 leetcode/Tree/No145.java diff --git a/README.md b/README.md index bde32de..8e4bd97 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 🐘 最长连续刷题天数: 11天 -🧗‍♂️ 已解题目:136道 +🧗‍♂️ 已解题目:137道 ------- diff --git a/leetcode/Tree/No145.java b/leetcode/Tree/No145.java new file mode 100644 index 0000000..b44cb63 --- /dev/null +++ b/leetcode/Tree/No145.java @@ -0,0 +1,21 @@ +package Algorithm.leetcode.Tree; + +import java.util.ArrayList; +import java.util.List; + +/** + * No145 Created by tujietg on Feb 5, 2020 + */ +public class No145 { + List result = new ArrayList(); + + public List postorderTraversal(TreeNode root) { + if (root == null) { + return result; + } + postorderTraversal(root.left); + postorderTraversal(root.right); + result.add(root.val); + return result; + } +} From a380b2bfaffe06c674bf6e54c4ebf3655ba8cc9d Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 5 Feb 2020 17:44:47 +0800 Subject: [PATCH 092/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e4bd97..1f4bc85 100755 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ | No606 | [606. 根据二叉树创建字符串](https://leetcode-cn.com/problems/construct-string-from-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No606.java) | 递归,根据是否为“”来判断采用什么样的方式。(左右节点是否为空所带来的字符串不一样) | | No637 | [637. 二叉树的层平均值](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No637.java) | 采用队列的方式解决。 | | No653 | [653. 两数之和 IV - 输入 BST](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No653.java) | | -| | | | | +| No145 | [145. 二叉树的后序遍历](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No145.java) | 直接遍历解决问题。 | | | | | | | | | | | | | | | | From d374eb252e6b10ab8f56710256a175e30647c278 Mon Sep 17 00:00:00 2001 From: tujietg Date: Sun, 9 Feb 2020 23:33:30 +0800 Subject: [PATCH 093/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0sql=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 1f4bc85..df9ee95 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -44leetcode +## leetcode 🚴‍♀️ I like to brush leetcode, it is a way of my pastime. I really **enjoy** it, I will always update it. @@ -71,16 +71,17 @@ | No637 | [637. 二叉树的层平均值](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No637.java) | 采用队列的方式解决。 | | No653 | [653. 两数之和 IV - 输入 BST](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No653.java) | | | No145 | [145. 二叉树的后序遍历](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/Tree/No145.java) | 直接遍历解决问题。 | -| | | | | -| | | | | -| | | | | -| | | | | -| | | | | -| | | | | -| | | | | -| | | | | -| | | | | -| | | | | -| | | | | -| | | | | + +--- + +#### SQL + +| # | Title | Solution | Note | +| ----- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---------------------------------------------------- | +| No175 | [175. 组合两个表](https://leetcode-cn.com/problems/combine-two-tables/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No175.sql) | 把Person表作为主表,直接连表查询。 | +| No176 | [176. 第二高的薪水](https://leetcode-cn.com/problems/second-highest-salary/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No176.sql) | 利用子查询,先查询出最高薪水,然后再查询第二高薪水。 | +| | | | | +| | | | | + + From 38c56a99d9842e5c83bd8c4b1d022ee2e4bcb3f0 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 10 Feb 2020 09:18:43 +0800 Subject: [PATCH 094/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index df9ee95..0c71255 100755 --- a/README.md +++ b/README.md @@ -76,12 +76,15 @@ #### SQL -| # | Title | Solution | Note | -| ----- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---------------------------------------------------- | -| No175 | [175. 组合两个表](https://leetcode-cn.com/problems/combine-two-tables/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No175.sql) | 把Person表作为主表,直接连表查询。 | -| No176 | [176. 第二高的薪水](https://leetcode-cn.com/problems/second-highest-salary/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No176.sql) | 利用子查询,先查询出最高薪水,然后再查询第二高薪水。 | -| | | | | -| | | | | +| # | Title | Solution | Note | +| ----- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | +| No175 | [175. 组合两个表](https://leetcode-cn.com/problems/combine-two-tables/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No175.sql) | 把Person表作为主表,直接连表查询。 | +| No176 | [176. 第二高的薪水](https://leetcode-cn.com/problems/second-highest-salary/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No176.sql) | 利用子查询,先查询出最高薪水,然后再查询第二高薪水。 | +| No181 | [181. 超过经理收入的员工](https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No181.sql) | 两个表的内连接进行比较。 | +| No182 | [182. 查找重复的电子邮箱](https://leetcode-cn.com/problems/duplicate-emails/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No182.sql) | 采用group by进行分组,having进行条件的筛选,count进行计算总数。 | +| No183 | [183. 从不订购的客户](https://leetcode-cn.com/problems/customers-who-never-order/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No183.sql) | 判断某个条件为空。 | +| No196 | [196. 删除重复的电子邮箱](https://leetcode-cn.com/problems/delete-duplicate-emails/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No196.sql) | 判断Email相等而且id不能相等。 | +| No197 | [197. 上升的温度](https://leetcode-cn.com/problems/rising-temperature/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No197.sql) | 采用DATEDIFF函数 并且其结果等于1。 | From c31f23caa691f6d69bfc64ebd5935478ca583873 Mon Sep 17 00:00:00 2001 From: tujietg Date: Tue, 11 Feb 2020 17:34:33 +0800 Subject: [PATCH 095/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No535?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++++++++ leetcode/{hashtable => hash}/No349.java | 2 +- leetcode/{hashtable => hash}/No389.java | 2 +- leetcode/{hashtable => hash}/No409.java | 2 +- leetcode/hash/No535.java | 25 +++++++++++++++++++++++++ leetcode/{hashtable => hash}/No771.java | 2 +- 6 files changed, 39 insertions(+), 4 deletions(-) rename leetcode/{hashtable => hash}/No349.java (93%) rename leetcode/{hashtable => hash}/No389.java (90%) rename leetcode/{hashtable => hash}/No409.java (92%) create mode 100644 leetcode/hash/No535.java rename leetcode/{hashtable => hash}/No771.java (90%) diff --git a/README.md b/README.md index 0c71255..f70720f 100755 --- a/README.md +++ b/README.md @@ -86,5 +86,15 @@ | No196 | [196. 删除重复的电子邮箱](https://leetcode-cn.com/problems/delete-duplicate-emails/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No196.sql) | 判断Email相等而且id不能相等。 | | No197 | [197. 上升的温度](https://leetcode-cn.com/problems/rising-temperature/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No197.sql) | 采用DATEDIFF函数 并且其结果等于1。 | +--- + +####Hash + +| # | Title | Solution | Note | +| ----- | ------------------------------------------------------------ | -------- | ---- | +| No535 | [535. TinyURL 的加密与解密](https://leetcode-cn.com/problems/encode-and-decode-tinyurl/) | | | +| | | | | +| | | | | + diff --git a/leetcode/hashtable/No349.java b/leetcode/hash/No349.java similarity index 93% rename from leetcode/hashtable/No349.java rename to leetcode/hash/No349.java index 67721e8..fe86095 100644 --- a/leetcode/hashtable/No349.java +++ b/leetcode/hash/No349.java @@ -1,4 +1,4 @@ -package Algorithm.leetcode.hashtable; +package Algorithm.leetcode.hash; import java.util.HashSet; import java.util.Set; diff --git a/leetcode/hashtable/No389.java b/leetcode/hash/No389.java similarity index 90% rename from leetcode/hashtable/No389.java rename to leetcode/hash/No389.java index 93bfa2f..ef4eea6 100644 --- a/leetcode/hashtable/No389.java +++ b/leetcode/hash/No389.java @@ -1,4 +1,4 @@ -package Algorithm.leetcode.hashtable; +package Algorithm.leetcode.hash; /** * diff --git a/leetcode/hashtable/No409.java b/leetcode/hash/No409.java similarity index 92% rename from leetcode/hashtable/No409.java rename to leetcode/hash/No409.java index d7efcc8..14d2d43 100644 --- a/leetcode/hashtable/No409.java +++ b/leetcode/hash/No409.java @@ -1,4 +1,4 @@ -package Algorithm.leetcode.hashtable; +package Algorithm.leetcode.hash; import java.util.HashSet; import java.util.Set; diff --git a/leetcode/hash/No535.java b/leetcode/hash/No535.java new file mode 100644 index 0000000..6ed7cd6 --- /dev/null +++ b/leetcode/hash/No535.java @@ -0,0 +1,25 @@ +package Algorithm.leetcode.hash; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * Created by tujietg on Feb 11, 2020 + */ +public class No535 { + + Map map = new HashMap<>(); + int i = 0; + + // Encodes a URL to a shortened URL. + public String encode(String longUrl) { + map.put(i, longUrl); + return "http://tinyurl.com/" + i++; + } + + // Decodes a shortened URL to its original URL. + public String decode(String shortUrl) { + return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", ""))); + } +} diff --git a/leetcode/hashtable/No771.java b/leetcode/hash/No771.java similarity index 90% rename from leetcode/hashtable/No771.java rename to leetcode/hash/No771.java index c18ebd2..76d6568 100644 --- a/leetcode/hashtable/No771.java +++ b/leetcode/hash/No771.java @@ -1,4 +1,4 @@ -package Algorithm.leetcode.hashtable; +package Algorithm.leetcode.hash; import java.util.HashSet; From 6adc7b4fc297ceb0d1907187c9d0d83b8d22d81f Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 12 Feb 2020 18:01:30 +0800 Subject: [PATCH 096/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f70720f..1711fe6 100755 --- a/README.md +++ b/README.md @@ -86,15 +86,15 @@ | No196 | [196. 删除重复的电子邮箱](https://leetcode-cn.com/problems/delete-duplicate-emails/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No196.sql) | 判断Email相等而且id不能相等。 | | No197 | [197. 上升的温度](https://leetcode-cn.com/problems/rising-temperature/) | [MySql](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/SQL/No197.sql) | 采用DATEDIFF函数 并且其结果等于1。 | ---- +----- -####Hash +#### Hash -| # | Title | Solution | Note | -| ----- | ------------------------------------------------------------ | -------- | ---- | -| No535 | [535. TinyURL 的加密与解密](https://leetcode-cn.com/problems/encode-and-decode-tinyurl/) | | | -| | | | | -| | | | | +| # | Title | Solution | Note | +| ----- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------- | +| No535 | [535. TinyURL 的加密与解密](https://leetcode-cn.com/problems/encode-and-decode-tinyurl/) | [java](https://github.com/Programming-With-Love/leetcode/blob/master/leetcode/hash/No535.java) | 直接采用map作为容器存储器其中的规则。 | +| | | | | +| | | | | From 0d90fca73453d7ef80c3b3423495e2c00440054f Mon Sep 17 00:00:00 2001 From: tujietg Date: Wed, 13 May 2020 08:52:37 +0800 Subject: [PATCH 097/116] =?UTF-8?q?=E4=BF=AE=E6=94=B9package=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0no102?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/inspectionProfiles/Project_Default.xml | 36 ++ .idea/leetcode.iml | 20 + .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 642 +++++++++++++++++++ leetcode/Array/No14.java | 40 +- leetcode/Tree/No100.java | 49 -- leetcode/Tree/No101.java | 24 - leetcode/Tree/No104.java | 25 - leetcode/Tree/No107.java | 49 -- leetcode/Tree/No108.java | 23 - leetcode/Tree/No110.java | 41 -- leetcode/Tree/No111.java | 32 - leetcode/Tree/No112.java | 17 - leetcode/Tree/No1302.java | 29 - leetcode/Tree/No1305.java | 31 - leetcode/Tree/No1315.java | 19 - leetcode/Tree/No145.java | 21 - leetcode/Tree/No226.java | 19 - leetcode/Tree/No235.java | 17 - leetcode/Tree/No257.java | 31 - leetcode/Tree/No404.java | 22 - leetcode/Tree/No559.java | 23 - leetcode/Tree/No589.java | 43 -- leetcode/Tree/No590.java | 25 - leetcode/Tree/No617.java | 20 - leetcode/Tree/No700.java | 22 - leetcode/Tree/No938.java | 16 - leetcode/Tree/No94.java | 23 - leetcode/Tree/No965.java | 22 - leetcode/tree/No100.java | 47 ++ leetcode/tree/No101.java | 22 + leetcode/tree/No102.java | 38 ++ leetcode/tree/No104.java | 24 + leetcode/tree/No107.java | 47 ++ leetcode/tree/No108.java | 21 + leetcode/tree/No110.java | 39 ++ leetcode/tree/No111.java | 30 + leetcode/tree/No112.java | 15 + leetcode/tree/No1302.java | 28 + leetcode/tree/No1305.java | 30 + leetcode/tree/No1315.java | 18 + leetcode/tree/No145.java | 20 + leetcode/tree/No226.java | 17 + leetcode/tree/No235.java | 14 + leetcode/tree/No257.java | 29 + leetcode/tree/No404.java | 20 + leetcode/tree/No559.java | 22 + leetcode/tree/No589.java | 42 ++ leetcode/tree/No590.java | 23 + leetcode/tree/No617.java | 18 + leetcode/tree/No700.java | 21 + leetcode/tree/No938.java | 15 + leetcode/tree/No94.java | 24 + leetcode/tree/No965.java | 21 + 56 files changed, 1386 insertions(+), 661 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/leetcode.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml delete mode 100644 leetcode/Tree/No100.java delete mode 100644 leetcode/Tree/No101.java delete mode 100644 leetcode/Tree/No104.java delete mode 100644 leetcode/Tree/No107.java delete mode 100644 leetcode/Tree/No108.java delete mode 100644 leetcode/Tree/No110.java delete mode 100644 leetcode/Tree/No111.java delete mode 100644 leetcode/Tree/No112.java delete mode 100644 leetcode/Tree/No1302.java delete mode 100644 leetcode/Tree/No1305.java delete mode 100644 leetcode/Tree/No1315.java delete mode 100644 leetcode/Tree/No145.java delete mode 100644 leetcode/Tree/No226.java delete mode 100644 leetcode/Tree/No235.java delete mode 100644 leetcode/Tree/No257.java delete mode 100644 leetcode/Tree/No404.java delete mode 100644 leetcode/Tree/No559.java delete mode 100644 leetcode/Tree/No589.java delete mode 100644 leetcode/Tree/No590.java delete mode 100644 leetcode/Tree/No617.java delete mode 100644 leetcode/Tree/No700.java delete mode 100644 leetcode/Tree/No938.java delete mode 100644 leetcode/Tree/No94.java delete mode 100644 leetcode/Tree/No965.java create mode 100644 leetcode/tree/No100.java create mode 100644 leetcode/tree/No101.java create mode 100644 leetcode/tree/No102.java create mode 100644 leetcode/tree/No104.java create mode 100644 leetcode/tree/No107.java create mode 100644 leetcode/tree/No108.java create mode 100644 leetcode/tree/No110.java create mode 100644 leetcode/tree/No111.java create mode 100644 leetcode/tree/No112.java create mode 100644 leetcode/tree/No1302.java create mode 100644 leetcode/tree/No1305.java create mode 100644 leetcode/tree/No1315.java create mode 100644 leetcode/tree/No145.java create mode 100644 leetcode/tree/No226.java create mode 100644 leetcode/tree/No235.java create mode 100644 leetcode/tree/No257.java create mode 100644 leetcode/tree/No404.java create mode 100644 leetcode/tree/No559.java create mode 100644 leetcode/tree/No589.java create mode 100644 leetcode/tree/No590.java create mode 100644 leetcode/tree/No617.java create mode 100644 leetcode/tree/No700.java create mode 100644 leetcode/tree/No938.java create mode 100644 leetcode/tree/No94.java create mode 100644 leetcode/tree/No965.java diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..6560a98 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/.idea/leetcode.iml b/.idea/leetcode.iml new file mode 100644 index 0000000..1af720a --- /dev/null +++ b/.idea/leetcode.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..893f829 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..619d2f7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..6266404 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,642 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + true + true + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + Ali-Check + + + Android + + + CorrectnessLintAndroid + + + GeneralJavaScript + + + Java + + + Java language level migration aidsJava + + + JavaScript + + + LintAndroid + + + PerformanceLintAndroid + + + WSDL + + + + + Ali-Check + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -246,6 +208,9 @@ + + + @@ -273,18 +238,19 @@ - - + @@ -367,13 +333,7 @@ - - - - - - - + @@ -524,7 +484,20 @@ - + + + + + + + + + + + + + + diff --git a/leetcode/binarysearch/No744.java b/leetcode/binarysearch/No744.java index de52845..ce3ac20 100644 --- a/leetcode/binarysearch/No744.java +++ b/leetcode/binarysearch/No744.java @@ -1,25 +1,23 @@ -package Algorithm.leetcode.binarysearch; /** - * * Created by tujietg on Nov 6, 2019 */ public class No744 { - public char nextGreatestLetter(char[] letters, char target) { - int len = letters.length; - int tarInt = target - 'A' + 1; - int lerInt = letters[len - 1] - 'A' + 1; - if (target == 'z' || tarInt >= lerInt) { - return letters[0]; - } - int i; - for (i = 1; i < len; i++) { - int leInt = letters[i - 1] - 'A' + 1; - if (leInt > tarInt) { - break; - } - } - return letters[i - 1]; - } + public char nextGreatestLetter(char[] letters, char target) { + int len = letters.length; + int tarInt = target - 'A' + 1; + int lerInt = letters[len - 1] - 'A' + 1; + if (target == 'z' || tarInt >= lerInt) { + return letters[0]; + } + int i; + for (i = 1; i < len; i++) { + int leInt = letters[i - 1] - 'A' + 1; + if (leInt > tarInt) { + break; + } + } + return letters[i - 1]; + } } diff --git a/leetcode/stack/No155.java b/leetcode/stack/No155.java new file mode 100644 index 0000000..289773e --- /dev/null +++ b/leetcode/stack/No155.java @@ -0,0 +1,45 @@ +import java.util.LinkedList; + +/** + * @author tujietg + * @date 5/13/20 9:48 AM + */ +public class No155 { + + LinkedList list = null; + LinkedList minList = null; + + /** + * initialize your data structure here. + */ + public No155() { + list = new LinkedList(); + minList = new LinkedList(); + + } + + public void push(int x) { + list.addFirst(x); + if (minList.size() == 0 || minList.element() >= x) { + minList.addFirst(x); + } + } + + public void pop() { + if (minList.get(0).equals(list.element())) { + minList.poll(); + } + list.poll(); + } + + public int top() { + return list.element(); + + } + + public int getMin() { + return minList.get(0); + } + + +} From 5285f48a00595a4cd9eb14d01c81218ada146a2d Mon Sep 17 00:00:00 2001 From: tujietg Date: Fri, 15 May 2020 11:00:27 +0800 Subject: [PATCH 099/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No560?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 59 ++++++++++++++++++++++++--------------- leetcode/Array/No560.java | 19 +++++++++++++ 2 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 leetcode/Array/No560.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 645a3e2..6268ec8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,7 @@ - - + @@ -175,6 +180,8 @@ + + @@ -187,18 +194,22 @@ + + + + + + + - + @@ -252,11 +247,12 @@ + - @@ -303,217 +299,59 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - - + + diff --git a/LinkedList/No21.java b/LinkedList/No21.java index 4673db9..16b12bb 100644 --- a/LinkedList/No21.java +++ b/LinkedList/No21.java @@ -1,61 +1,61 @@ -package Algorithm.leetcode.LinkedList; +package LinkedList; class ListNode { - int val; - ListNode next; + int val; + ListNode next; - ListNode(int x) { - val = x; - } + ListNode(int x) { + val = x; + } } // 递归 public class No21 { - public ListNode mergeTwoLists(ListNode l1, ListNode l2) { - if (l1 == null) { - return l2; - } - if (l2 == null) { - return l1; - } - ListNode node = null; - if (l1.val < l2.val) { - node = l1; - node.next = mergeTwoLists(l1.next, l2); - } else { - node = l2; - node.next = mergeTwoLists(l1, l2.next); - } - return node; - } + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + if (l1 == null) { + return l2; + } + if (l2 == null) { + return l1; + } + ListNode node = null; + if (l1.val < l2.val) { + node = l1; + node.next = mergeTwoLists(l1.next, l2); + } else { + node = l2; + node.next = mergeTwoLists(l1, l2.next); + } + return node; + } - public ListNode mergeTwoLists01(ListNode l1, ListNode l2) { - if (l1 == null) { - return l2; - } - if (l2 == null) { - return l1; - } - ListNode head = new ListNode(-1); - ListNode node = head; - while (l1 != null && l2 != null) { - if (l1.val > l2.val) { - node.next = l2; - l2 = l2.next; - } else { - node.next = l1; - l1 = l1.next; - } - node = node.next; - } - if (l1 != null) { - node.next = l1; - } - if (l2 != null) { - node.next = l2; - } + public ListNode mergeTwoLists01(ListNode l1, ListNode l2) { + if (l1 == null) { + return l2; + } + if (l2 == null) { + return l1; + } + ListNode head = new ListNode(-1); + ListNode node = head; + while (l1 != null && l2 != null) { + if (l1.val > l2.val) { + node.next = l2; + l2 = l2.next; + } else { + node.next = l1; + l1 = l1.next; + } + node = node.next; + } + if (l1 != null) { + node.next = l1; + } + if (l2 != null) { + node.next = l2; + } - return head.next; + return head.next; - } + } } diff --git a/LinkedList/No25.java b/LinkedList/No25.java new file mode 100644 index 0000000..9ca6017 --- /dev/null +++ b/LinkedList/No25.java @@ -0,0 +1,58 @@ +package LinkedList; + +/** + * 第一次 刷困难 贼开心 + * + * @author tujietg + * @date 5/16/20 11:37 AM + */ +public class No25 { + + public ListNode reverseKGroup(ListNode head, int k) { + //虚拟一个节点 指向头部 + ListNode pre = new ListNode(Integer.MIN_VALUE); + pre.next = head; + + ListNode tail = pre; + ListNode start, next; + ListNode hire = pre; + + while (pre.next != null) { + // 将tail 移到小段的末尾 + for (int i = 0; i < k && tail != null; i++) { + tail = tail.next; + } + if (tail == null) { + break; + } + start = pre.next; + + next = tail.next; + tail.next = null; + + pre.next = reverseList(start); + start.next = next; + + pre = start; + tail = pre; + } + return hire.next; + + } + + + public ListNode reverseList(ListNode head) { + ListNode pre = null; + ListNode curr = head; + + while (curr != null) { + ListNode nextNode = curr.next; + curr.next = pre; + pre = curr; + curr = nextNode; + } + return pre; + } + + +} From 830c694079a5fd25bda420b8985c6772e8a138b9 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 18 May 2020 09:08:25 +0800 Subject: [PATCH 102/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No152?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 53 +++++++++++++++++++++++++-------------------- Array/No152.java | 23 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 Array/No152.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2b2ade1..a512c3c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,7 @@ - - + - + @@ -31,6 +30,15 @@ + + + + + + + + + @@ -87,6 +95,7 @@ @@ -169,7 +178,7 @@ - + @@ -180,31 +189,19 @@ - - - - - - - - - - - - - + @@ -201,7 +251,7 @@ - + @@ -246,17 +296,18 @@ + - - + @@ -332,13 +383,6 @@ - - - - - - - @@ -360,6 +404,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/String/No5.java b/String/No5.java index 417ef0c..571ca8a 100644 --- a/String/No5.java +++ b/String/No5.java @@ -1,9 +1,12 @@ + /** * @author tujietg * @date 5/21/20 9:07 AM */ public class No5 { public String longestPalindrome(String s) { + + int len = s.length(); if (len < 2) { return s; diff --git a/String/No76.java b/String/No76.java new file mode 100644 index 0000000..63d4c60 --- /dev/null +++ b/String/No76.java @@ -0,0 +1,62 @@ +package String; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author tujietg + * @date 5/23/20 1:03 PM + */ +public class No76 { + public static String minWindow(String s, String t) { + int sLen = s.length(); + + int start = -1; + int end = sLen - 1; + + char[] tChars = t.toCharArray(); + char[] sChars = s.toCharArray(); + + if (s.length() < t.length()) { + return ""; + } + + Map tMap = new HashMap<>(t.length()); + + for (int i = 0; i < tChars.length; i++) { + tMap.put(tChars[i], tMap.getOrDefault(tChars[i], 0) + 1); + } + + for (int i = 0; i < sLen; i++) { + for (int j = i; j < sLen; j++) { + String substring = s.substring(i, j + 1); + HashMap characterIntegerHashMap = new HashMap<>(t.length()); + characterIntegerHashMap.putAll(tMap); + if (check(characterIntegerHashMap, substring.toCharArray()) == true) { + if (j - i <= end - start) { + end = j; + start = i; + j = sLen; + while (!tMap.containsKey(sChars[i]) && i < sLen && !tMap.containsKey(sChars[i + 1])) { + i++; + } + } + } + } + } + return start == -1 ? "" : s.substring(start, end + 1); + } + + private static boolean check(Map midMap, char[] sChars) { + for (int i = 0; i < sChars.length; i++) { + if (midMap.containsKey(sChars[i])) { + if (midMap.get(sChars[i]).equals(1)) { + midMap.remove(sChars[i]); + } else { + midMap.put(sChars[i], midMap.get(sChars[i]) - 1); + } + } + } + return midMap.size() == 0; + } +} From b14d98adb93045f93652626be17ba39477fa3137 Mon Sep 17 00:00:00 2001 From: tujietg Date: Sun, 24 May 2020 15:56:50 +0800 Subject: [PATCH 105/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0No5416?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 49 +++++++++++++++++++++++++++------------------ String/No5416.java | 19 ++++++++++++++++++ 2 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 String/No5416.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d1bfc25..4227980 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,7 @@ - - + - - - - - - - - - @@ -70,7 +60,7 @@ - + @@ -82,6 +72,15 @@ + + + + + + + + + @@ -141,6 +140,7 @@ @@ -169,10 +169,11 @@ - + @@ -224,7 +225,6 @@ - @@ -245,13 +245,14 @@ - - + @@ -299,17 +304,18 @@ + - - + - + @@ -454,7 +460,7 @@ - + @@ -469,6 +475,13 @@ + + + + + + + diff --git a/String/No1456.java b/String/No1456.java new file mode 100644 index 0000000..86435cb --- /dev/null +++ b/String/No1456.java @@ -0,0 +1,30 @@ +package String; + +/** + * @author tujietg + * @date 5/27/20 8:39 AM + */ +public class No1456 { + + public int maxVowels(String s, int k) { + int max = 0, count = 0, j = 0, sLen = s.length(); + + for (int i = 0; i < sLen; i++) { + count += check(s.charAt(i)); + if (i - j + 1 > k) { + count -= check(s.charAt(j++)); + } + max = Math.max(max, count); + } + return max; + + } + + public int check(char item) { + if (item == 'a' || item == 'e' || item == 'i' || item == 'o' || item == 'u') { + return 1; + } else { + return 0; + } + } +} From 5f251a7d7537762ff333aefb7d1f3b3d785351a1 Mon Sep 17 00:00:00 2001 From: tujietg Date: Sat, 30 May 2020 11:08:25 +0800 Subject: [PATCH 107/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Nom64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tree/Nom68.java | 24 ++++++++++++++++++++++++ tree/TreeNode.java | 16 ++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tree/Nom68.java create mode 100644 tree/TreeNode.java diff --git a/tree/Nom68.java b/tree/Nom68.java new file mode 100644 index 0000000..ec7a054 --- /dev/null +++ b/tree/Nom68.java @@ -0,0 +1,24 @@ +package tree; + +/** + * @author tujietg + * @date 5/30/20 11:05 AM + */ +public class Nom68 { + + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (root == null || root == p || root == q) { + return root; + } + TreeNode leftNode = lowestCommonAncestor(root.left, p, q); + TreeNode rightNode = lowestCommonAncestor(root.right, p, q); + if (leftNode == null) { + return rightNode; + } + if (rightNode == null) { + return leftNode; + } + return root; + } + +} diff --git a/tree/TreeNode.java b/tree/TreeNode.java new file mode 100644 index 0000000..c8a32bc --- /dev/null +++ b/tree/TreeNode.java @@ -0,0 +1,16 @@ +package tree; + +/** + * @author tujietg + * @date 5/30/20 11:06 AM + */ +public class TreeNode { + + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} From 7be33e92eb78102ad642d18a9f848f9ce301543c Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 1 Jun 2020 10:43:58 +0800 Subject: [PATCH 108/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0NO1431?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Array/No1431.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Array/No1431.java diff --git a/Array/No1431.java b/Array/No1431.java new file mode 100644 index 0000000..4ecb3e3 --- /dev/null +++ b/Array/No1431.java @@ -0,0 +1,23 @@ +package Array; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author tujietg + * @date 6/1/20 10:43 AM + */ +public class No1431 { + public List kidsWithCandies(int[] candies, int extraCandies) { + int n = candies.length; + int maxCandies = 0; + for (int i = 0; i < n; ++i) { + maxCandies = Math.max(maxCandies, candies[i]); + } + List ret = new ArrayList(); + for (int i = 0; i < n; ++i) { + ret.add(candies[i] + extraCandies >= maxCandies); + } + return ret; + } +} From 9f35abe7dc2f49952449d9435cb927edfffaa457 Mon Sep 17 00:00:00 2001 From: tujietg Date: Mon, 1 Jun 2020 10:47:56 +0800 Subject: [PATCH 109/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 123 +++++++++++++++++++++++++++++++++----------- 1 file changed, 93 insertions(+), 30 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 31fe44b..5cd81db 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + - - - - - - - - - - - - - - - @@ -58,7 +43,7 @@ - + @@ -76,7 +61,7 @@ - + @@ -85,6 +70,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -146,6 +166,9 @@ @@ -177,8 +200,8 @@ @@ -230,7 +253,6 @@ - @@ -241,7 +263,7 @@ - + @@ -251,13 +273,14 @@ - + - + @@ -336,7 +344,8 @@ - + + 1590808105335 @@ -359,17 +368,24 @@ - - - + - + @@ -412,13 +428,10 @@ - - - - - @@ -573,8 +586,36 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LinkedList/No1290.java b/LinkedList/No1290.java new file mode 100644 index 0000000..f99340d --- /dev/null +++ b/LinkedList/No1290.java @@ -0,0 +1,17 @@ +package LinkedList; + +/** + * @author tujietg + * @date 6/9/20 8:48 AM + */ +public class No1290 { + public int getDecimalValue(ListNode head) { + int end = 0; + while (head != null) { + end = end * 2 + head.val; + head = head.next; + } + return end; + } + +} diff --git a/LinkedList/No24.java b/LinkedList/No24.java new file mode 100644 index 0000000..e2b99bb --- /dev/null +++ b/LinkedList/No24.java @@ -0,0 +1,20 @@ +package LinkedList; + +/** + * @author tujietg + * @date 6/9/20 8:50 AM + */ +public class No24 { + public ListNode reverseList(ListNode head) { + if (head == null || head.next == null) { + return head; + } + ListNode cur = reverseList(head.next); + + head.next.next = head; + + head.next = null; + return cur; + } + +} diff --git a/LinkedList/Nom0202.java b/LinkedList/Nom0202.java new file mode 100644 index 0000000..5e0ce03 --- /dev/null +++ b/LinkedList/Nom0202.java @@ -0,0 +1,21 @@ +package LinkedList; + +/** + * @author tujietg + * @date 6/9/20 8:49 AM + */ +public class Nom0202 { + + public int kthToLast(ListNode head, int k) { + ListNode p = head; + for (int i = 0; i < k; i++) { + p = p.next; + } + while (p != null) { + head = head.next; + p = p.next; + } + return head.val; + } + +} diff --git a/LinkedList/Nom22.java b/LinkedList/Nom22.java new file mode 100644 index 0000000..89d0bcc --- /dev/null +++ b/LinkedList/Nom22.java @@ -0,0 +1,20 @@ +package LinkedList; + +/** + * @author tujietg + * @date 6/9/20 8:49 AM + */ +public class Nom22 { + + public ListNode getKthFromEnd(ListNode head, int k) { + ListNode p = head; + for (int i = 0; i < k; i++) { + p = p.next; + } + while (p != null) { + head = head.next; + p = p.next; + } + return head; + } +} From 582913027ef681e2b9706dd3b3bf8f9fc7a46571 Mon Sep 17 00:00:00 2001 From: tujietg Date: Thu, 11 Jun 2020 09:30:07 +0800 Subject: [PATCH 114/116] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Nom35?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 63 +++++++++++++++++++++++++------------------ LinkedList/Nom35.java | 39 +++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 26 deletions(-) create mode 100644 LinkedList/Nom35.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0c53bca..61ce8c0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,10 +2,7 @@ - - - - + - - - - - - - - - @@ -103,11 +91,23 @@ - + - - + + + + + + + + + + + + + + @@ -182,6 +182,7 @@ @@ -212,9 +213,9 @@ @@ -266,6 +267,7 @@ + @@ -287,13 +289,12 @@