From 42609b9ddf7cbb477d756fea37f9ea00af139d0a Mon Sep 17 00:00:00 2001 From: SUANFA_Trainer <1842856540@qq.com> Date: Tue, 20 Oct 2020 15:10:54 +0800 Subject: [PATCH 01/20] A test file! --- week01/Hello.python | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 week01/Hello.python diff --git a/week01/Hello.python b/week01/Hello.python new file mode 100644 index 00000000..8208cde5 --- /dev/null +++ b/week01/Hello.python @@ -0,0 +1,2 @@ +print("Hello World !") +#for test! \ No newline at end of file From d4aa0a4a4942b641e1f948e05ac41a7437189218 Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 25 Oct 2020 10:59:44 +0800 Subject: [PATCH 02/20] Delete Hello.python --- week01/Hello.python | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 week01/Hello.python diff --git a/week01/Hello.python b/week01/Hello.python deleted file mode 100644 index 8208cde5..00000000 --- a/week01/Hello.python +++ /dev/null @@ -1,2 +0,0 @@ -print("Hello World !") -#for test! \ No newline at end of file From 293dcf7d02c8134c098237759e7e6e53ea2f6904 Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 25 Oct 2020 20:54:53 +0800 Subject: [PATCH 03/20] Update NOTE.md --- week01/NOTE.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/week01/NOTE.md b/week01/NOTE.md index 50de3041..ebc8972a 100644 --- a/week01/NOTE.md +++ b/week01/NOTE.md @@ -1 +1,3 @@ -学习笔记 \ No newline at end of file +这一周的学习,发现自己的简单的基础知识还是不错的,对循环和条件的使用比较熟练,题目一般只能想到简单的暴力拆解,复杂的题目基本搬砖。精巧的思路只能仰望了。 +这可能是思维习惯上还没有养成,在学校的基础不扎实,学习的知识和工程应用上的脱节。在看超哥课程时还是有一点理解上的困难。 +这一周课程比较繁忙,时间上投入较少,部分作业基本照搬了,这一周会抓紧时间来巩固。 From c6da251e4bece7354b6abdbf85811155b0ea38a0 Mon Sep 17 00:00:00 2001 From: SUANFA_Trainer <1842856540@qq.com> Date: Sun, 25 Oct 2020 21:19:43 +0800 Subject: [PATCH 04/20] =?UTF-8?q?01=E6=90=AC=E7=A0=96=E6=89=80=E5=BE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../_1removeDuplicates.java | 24 +++ week01/week01Homework_java/_2rotateArray.java | 58 +++++++ .../_3mergeTwoSortedLists.java | 27 +++ .../_4mergeSortedArray.java | 13 ++ week01/week01Homework_java/_5twoSum.java | 36 ++++ week01/week01Homework_java/_6moveZeroes.java | 32 ++++ week01/week01Homework_java/_7plusOne.java | 45 +++++ .../_8designCircularDeque.java | 158 ++++++++++++++++++ .../_9trappingRainWater.java | 28 ++++ 9 files changed, 421 insertions(+) create mode 100644 week01/week01Homework_java/_1removeDuplicates.java create mode 100644 week01/week01Homework_java/_2rotateArray.java create mode 100644 week01/week01Homework_java/_3mergeTwoSortedLists.java create mode 100644 week01/week01Homework_java/_4mergeSortedArray.java create mode 100644 week01/week01Homework_java/_5twoSum.java create mode 100644 week01/week01Homework_java/_6moveZeroes.java create mode 100644 week01/week01Homework_java/_7plusOne.java create mode 100644 week01/week01Homework_java/_8designCircularDeque.java create mode 100644 week01/week01Homework_java/_9trappingRainWater.java diff --git a/week01/week01Homework_java/_1removeDuplicates.java b/week01/week01Homework_java/_1removeDuplicates.java new file mode 100644 index 00000000..00d97e97 --- /dev/null +++ b/week01/week01Homework_java/_1removeDuplicates.java @@ -0,0 +1,24 @@ +/* +* 给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 + +不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。 +*/ +public class _1removeDuplicates { + public static int solution(int[] nums){ + int i = 0; + for (int j = 1 ; j < nums.length ; j++ ) { + if(nums[i] != nums[j] ){ + i++; // i的起始位为0,所以i++在先 + nums[i]=nums[j]; + } + } + return i+1; + } + public static void main(String[] args) { + int[] nums = {1,2,2,3,4,5,5,7,9}; + int l = solution(nums); + for(int i = 0 ; i < l ; i ++){ + System.out.print(nums[i]+"\t"); + } + } +} diff --git a/week01/week01Homework_java/_2rotateArray.java b/week01/week01Homework_java/_2rotateArray.java new file mode 100644 index 00000000..b4c3400f --- /dev/null +++ b/week01/week01Homework_java/_2rotateArray.java @@ -0,0 +1,58 @@ +/* +* +给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。*/ +public class _2rotateArray { + public static void rotateByForce(int[] nums, int k) { + /** 暴力! + 1、取出末尾k个 + 2、双指针由后向前移动元素 + ? 如果只有k个或者小于k个?? + **/ + //先处理边界条件 + if (nums.length == 1) return; + if (nums.length == k) return; + if (nums.length < k) k = k - nums.length; + + int[] temp = new int[k]; + int t = 0; + for (int i = nums.length - k; i <= nums.length - 1; i++) { + temp[t] = nums[i]; + t++; + } + t = nums.length - 1; + for (int i = nums.length - 1 - k; i > -1; i--) { + nums[t] = nums[i]; + t--; + } + for (int i = 0; i < k; i++) { + nums[i] = temp[i]; + } + } + + public static void main(String[] args) { + int k = 3; + int[] a = {-1}; + int[] b = {1, 2, 3}; + int[] c = {2, 5, 8, 9}; + int[] d = {8, 9}; + rotateByForce(a, k); + for (int i = 0; i < a.length; i++) { + System.out.print(a[i] + "\t"); + } + System.out.println("\n"); + rotateByForce(b, k); + for (int i = 0; i < b.length; i++) { + System.out.print(b[i] + "\t"); + } + System.out.println("\n"); + rotateByForce(c, k); + for (int i = 0; i < c.length; i++) { + System.out.print(c[i] + "\t"); + } + System.out.println("\n"); + rotateByForce(d, k); + for (int i = 0; i < d.length; i++) { + System.out.print(d[i] + "\t"); + } + } +} diff --git a/week01/week01Homework_java/_3mergeTwoSortedLists.java b/week01/week01Homework_java/_3mergeTwoSortedLists.java new file mode 100644 index 00000000..a98fb2bf --- /dev/null +++ b/week01/week01Homework_java/_3mergeTwoSortedLists.java @@ -0,0 +1,27 @@ +/*将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 */ +public class _3mergeTwoSortedLists { + + //Definition for singly-linked list. + public class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } + } + public static ListNode mergeTwoLists(ListNode l1,ListNode l2){ + if(l1==null){ + return l2; + }else if (l2 == null) { + return l1; + } + if(l1.val hashtable = new HashMap(); + for(int i = 0; i < nums.length ; ++i){ + if(hashtable.containsKey(target - nums[i])){ + return new int[]{hashtable.get(target-nums[i]),i}; + } + hashtable.put(nums[i],i); + } + return new int[0]; + } + } + // 2、暴力 + class Solution2 { + public int[] twoSum(int[] nums, int target) { + int[] a = new int[2]; + for(int i = 0 ; i < nums.length ; i++){ + for(int j = i+1 ; j < nums.length ; j++){ + if(nums[i] + nums[j] == target ){ + a[0]=i; + a[1]=j; + break; + } + } + } + return a; + } + + } + +} diff --git a/week01/week01Homework_java/_6moveZeroes.java b/week01/week01Homework_java/_6moveZeroes.java new file mode 100644 index 00000000..aac668d1 --- /dev/null +++ b/week01/week01Homework_java/_6moveZeroes.java @@ -0,0 +1,32 @@ +public class _6moveZeroes { + class Solution { + public void moveZeroes(int[] nums) { + // 快慢指针法 + int i = 0 ; + for(int j = 0;j=0;i--){ + if(add==0)continue; + temp = digits[i]+1; + if(temp==10) + { + digits[i]=0; + add = 1; + }else{ + add = 0; + digits[i]=temp; + } + } + if(add == 1){ + int[] ex = new int[digits.length+1]; + for(int i=digits.length;i>0;i--) + { + ex[i]=digits[i-1]; + } + ex[0]=1; + digits=ex; + } + return digits; + } + } + // 2、一个好题解 + class Solution2 { + public int[] plusOne(int[] digits) { + for(int i = digits.length -1 ; i >=0 ; i--){ + digits[i]++; + digits[i] = digits[i]%10; + if(digits[i] != 0)return digits; + } + digits = new int[digits.length + 1]; + digits[0] = 1; + return digits; + } + } +} diff --git a/week01/week01Homework_java/_8designCircularDeque.java b/week01/week01Homework_java/_8designCircularDeque.java new file mode 100644 index 00000000..c0b5ad83 --- /dev/null +++ b/week01/week01Homework_java/_8designCircularDeque.java @@ -0,0 +1,158 @@ +public class _8designCircularDeque { + class MyCircularDeque { + private int size; + private int lastIndex; + private MyCircularDequeNode node; + + /** + * Initialize your data structure here. Set the size of the deque to be k. + */ + public MyCircularDeque(int k) { + this.size = k; + } + + /** + * Adds an item at the front of Deque. Return true if the operation is successful. + */ + public boolean insertFront(int value) { + if (lastIndex < size) { + MyCircularDequeNode node = new MyCircularDequeNode(value); + if (this.node == null) { + this.node = node; + lastIndex ++; + } else { + this.node.prev = node; + node.next = this.node; + this.node = node; + lastIndex++; + } + return true; + } else { + return false; + } + } + + /** + * Adds an item at the rear of Deque. Return true if the operation is successful. + */ + public boolean insertLast(int value) { + if (lastIndex < size) { + MyCircularDequeNode node = new MyCircularDequeNode(value); + if (this.node == null) { + this.node = node; + lastIndex ++; + } else { + MyCircularDequeNode oNode = this.node; + while (oNode.next != null) { + oNode = oNode.next; + } + oNode.next = node; + node.prev = oNode; + lastIndex++; + } + return true; + } else { + return false; + } + } + + /** + * Deletes an item from the front of Deque. Return true if the operation is successful. + */ + public boolean deleteFront() { + if (this.node == null) { + return false; + } else { + MyCircularDequeNode next = this.node.next; + if (next == null) { + this.node = null; + lastIndex--; + return true; + } else { + next.prev = null; + this.node = next; + lastIndex--; + return true; + } + } + } + + /** + * Deletes an item from the rear of Deque. Return true if the operation is successful. + */ + public boolean deleteLast() { + if (this.node == null) { + return false; + } else { + MyCircularDequeNode next = this.node; + while (next.next != null) { + next = next.next; + } + MyCircularDequeNode prev = next.prev; + if (prev != null) { + prev.next = null; + } else { + this.node = null; + } + lastIndex--; + return true; + } + } + + /** + * Get the front item from the deque. + */ + public int getFront() { + if (this.node == null) { + return -1; + } else { + return this.node.val; + } + } + + /** + * Get the last item from the deque. + */ + public int getRear() { + if (this.node == null) { + return -1; + } else { + MyCircularDequeNode next = this.node; + while (next.next != null) { + next = next.next; + } + return next.val; + } + } + + /** + * Checks whether the circular deque is empty or not. + */ + public boolean isEmpty() { + return lastIndex == 0; + } + + /** + * Checks whether the circular deque is full or not. + */ + public boolean isFull() { + return lastIndex == size; + } + + private class MyCircularDequeNode{ + int val; + MyCircularDequeNode prev; + MyCircularDequeNode next; + + public MyCircularDequeNode(int val) { + this.val = val; + } + } + + } + +// 作者:anz +// 链接:https://leetcode-cn.com/problems/design-circular-deque/solution/shuang-duan-dui-lie-shuang-zhi-zhen-by-anz/ +// 来源:力扣(LeetCode) +// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 +} diff --git a/week01/week01Homework_java/_9trappingRainWater.java b/week01/week01Homework_java/_9trappingRainWater.java new file mode 100644 index 00000000..f6207d8e --- /dev/null +++ b/week01/week01Homework_java/_9trappingRainWater.java @@ -0,0 +1,28 @@ +public class _9trappingRainWater { + class Solution { + // 双指针 + public int trap(int[] height) { + int left = 0, right = height.length - 1; + int ans = 0; + int left_max = 0, right_max = 0; + while (left < right) { + if (height[left] < height[right]) { + if (height[left] >= left_max) { + left_max = height[left]; + } else { + ans += (left_max - height[left]); + } + ++left; + } else { + if (height[right] >= right_max) { + right_max = height[right]; + } else { + ans += (right_max - height[right]); + } + --right; + } + } + return ans; + } + } +} From 26dfe5b8388c9fad32c54e47e54ddd43a47255ef Mon Sep 17 00:00:00 2001 From: SUANFA_Trainer <1842856540@qq.com> Date: Sun, 25 Oct 2020 21:53:40 +0800 Subject: [PATCH 05/20] week01Homework --- week01/Hello.python | 2 - week01/_1removeDuplicates.java | 24 +++++ week01/_2rotateArray.java | 58 +++++++++++ week01/_3mergeTwoSortedLists.java | 27 +++++ week01/_4mergeSortedArray.java | 13 +++ week01/_5twoSum.java | 36 +++++++ week01/_6moveZeroes.java | 32 ++++++ week01/_7plusOne.java | 45 +++++++++ week01/_8designCircularDeque.java | 158 ++++++++++++++++++++++++++++++ week01/_9trappingRainWater.java | 28 ++++++ 10 files changed, 421 insertions(+), 2 deletions(-) delete mode 100644 week01/Hello.python create mode 100644 week01/_1removeDuplicates.java create mode 100644 week01/_2rotateArray.java create mode 100644 week01/_3mergeTwoSortedLists.java create mode 100644 week01/_4mergeSortedArray.java create mode 100644 week01/_5twoSum.java create mode 100644 week01/_6moveZeroes.java create mode 100644 week01/_7plusOne.java create mode 100644 week01/_8designCircularDeque.java create mode 100644 week01/_9trappingRainWater.java diff --git a/week01/Hello.python b/week01/Hello.python deleted file mode 100644 index 8208cde5..00000000 --- a/week01/Hello.python +++ /dev/null @@ -1,2 +0,0 @@ -print("Hello World !") -#for test! \ No newline at end of file diff --git a/week01/_1removeDuplicates.java b/week01/_1removeDuplicates.java new file mode 100644 index 00000000..00d97e97 --- /dev/null +++ b/week01/_1removeDuplicates.java @@ -0,0 +1,24 @@ +/* +* 给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 + +不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。 +*/ +public class _1removeDuplicates { + public static int solution(int[] nums){ + int i = 0; + for (int j = 1 ; j < nums.length ; j++ ) { + if(nums[i] != nums[j] ){ + i++; // i的起始位为0,所以i++在先 + nums[i]=nums[j]; + } + } + return i+1; + } + public static void main(String[] args) { + int[] nums = {1,2,2,3,4,5,5,7,9}; + int l = solution(nums); + for(int i = 0 ; i < l ; i ++){ + System.out.print(nums[i]+"\t"); + } + } +} diff --git a/week01/_2rotateArray.java b/week01/_2rotateArray.java new file mode 100644 index 00000000..b4c3400f --- /dev/null +++ b/week01/_2rotateArray.java @@ -0,0 +1,58 @@ +/* +* +给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。*/ +public class _2rotateArray { + public static void rotateByForce(int[] nums, int k) { + /** 暴力! + 1、取出末尾k个 + 2、双指针由后向前移动元素 + ? 如果只有k个或者小于k个?? + **/ + //先处理边界条件 + if (nums.length == 1) return; + if (nums.length == k) return; + if (nums.length < k) k = k - nums.length; + + int[] temp = new int[k]; + int t = 0; + for (int i = nums.length - k; i <= nums.length - 1; i++) { + temp[t] = nums[i]; + t++; + } + t = nums.length - 1; + for (int i = nums.length - 1 - k; i > -1; i--) { + nums[t] = nums[i]; + t--; + } + for (int i = 0; i < k; i++) { + nums[i] = temp[i]; + } + } + + public static void main(String[] args) { + int k = 3; + int[] a = {-1}; + int[] b = {1, 2, 3}; + int[] c = {2, 5, 8, 9}; + int[] d = {8, 9}; + rotateByForce(a, k); + for (int i = 0; i < a.length; i++) { + System.out.print(a[i] + "\t"); + } + System.out.println("\n"); + rotateByForce(b, k); + for (int i = 0; i < b.length; i++) { + System.out.print(b[i] + "\t"); + } + System.out.println("\n"); + rotateByForce(c, k); + for (int i = 0; i < c.length; i++) { + System.out.print(c[i] + "\t"); + } + System.out.println("\n"); + rotateByForce(d, k); + for (int i = 0; i < d.length; i++) { + System.out.print(d[i] + "\t"); + } + } +} diff --git a/week01/_3mergeTwoSortedLists.java b/week01/_3mergeTwoSortedLists.java new file mode 100644 index 00000000..a98fb2bf --- /dev/null +++ b/week01/_3mergeTwoSortedLists.java @@ -0,0 +1,27 @@ +/*将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 */ +public class _3mergeTwoSortedLists { + + //Definition for singly-linked list. + public class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } + } + public static ListNode mergeTwoLists(ListNode l1,ListNode l2){ + if(l1==null){ + return l2; + }else if (l2 == null) { + return l1; + } + if(l1.val hashtable = new HashMap(); + for(int i = 0; i < nums.length ; ++i){ + if(hashtable.containsKey(target - nums[i])){ + return new int[]{hashtable.get(target-nums[i]),i}; + } + hashtable.put(nums[i],i); + } + return new int[0]; + } + } + // 2、暴力 + class Solution2 { + public int[] twoSum(int[] nums, int target) { + int[] a = new int[2]; + for(int i = 0 ; i < nums.length ; i++){ + for(int j = i+1 ; j < nums.length ; j++){ + if(nums[i] + nums[j] == target ){ + a[0]=i; + a[1]=j; + break; + } + } + } + return a; + } + + } + +} diff --git a/week01/_6moveZeroes.java b/week01/_6moveZeroes.java new file mode 100644 index 00000000..aac668d1 --- /dev/null +++ b/week01/_6moveZeroes.java @@ -0,0 +1,32 @@ +public class _6moveZeroes { + class Solution { + public void moveZeroes(int[] nums) { + // 快慢指针法 + int i = 0 ; + for(int j = 0;j=0;i--){ + if(add==0)continue; + temp = digits[i]+1; + if(temp==10) + { + digits[i]=0; + add = 1; + }else{ + add = 0; + digits[i]=temp; + } + } + if(add == 1){ + int[] ex = new int[digits.length+1]; + for(int i=digits.length;i>0;i--) + { + ex[i]=digits[i-1]; + } + ex[0]=1; + digits=ex; + } + return digits; + } + } + // 2、一个好题解 + class Solution2 { + public int[] plusOne(int[] digits) { + for(int i = digits.length -1 ; i >=0 ; i--){ + digits[i]++; + digits[i] = digits[i]%10; + if(digits[i] != 0)return digits; + } + digits = new int[digits.length + 1]; + digits[0] = 1; + return digits; + } + } +} diff --git a/week01/_8designCircularDeque.java b/week01/_8designCircularDeque.java new file mode 100644 index 00000000..c0b5ad83 --- /dev/null +++ b/week01/_8designCircularDeque.java @@ -0,0 +1,158 @@ +public class _8designCircularDeque { + class MyCircularDeque { + private int size; + private int lastIndex; + private MyCircularDequeNode node; + + /** + * Initialize your data structure here. Set the size of the deque to be k. + */ + public MyCircularDeque(int k) { + this.size = k; + } + + /** + * Adds an item at the front of Deque. Return true if the operation is successful. + */ + public boolean insertFront(int value) { + if (lastIndex < size) { + MyCircularDequeNode node = new MyCircularDequeNode(value); + if (this.node == null) { + this.node = node; + lastIndex ++; + } else { + this.node.prev = node; + node.next = this.node; + this.node = node; + lastIndex++; + } + return true; + } else { + return false; + } + } + + /** + * Adds an item at the rear of Deque. Return true if the operation is successful. + */ + public boolean insertLast(int value) { + if (lastIndex < size) { + MyCircularDequeNode node = new MyCircularDequeNode(value); + if (this.node == null) { + this.node = node; + lastIndex ++; + } else { + MyCircularDequeNode oNode = this.node; + while (oNode.next != null) { + oNode = oNode.next; + } + oNode.next = node; + node.prev = oNode; + lastIndex++; + } + return true; + } else { + return false; + } + } + + /** + * Deletes an item from the front of Deque. Return true if the operation is successful. + */ + public boolean deleteFront() { + if (this.node == null) { + return false; + } else { + MyCircularDequeNode next = this.node.next; + if (next == null) { + this.node = null; + lastIndex--; + return true; + } else { + next.prev = null; + this.node = next; + lastIndex--; + return true; + } + } + } + + /** + * Deletes an item from the rear of Deque. Return true if the operation is successful. + */ + public boolean deleteLast() { + if (this.node == null) { + return false; + } else { + MyCircularDequeNode next = this.node; + while (next.next != null) { + next = next.next; + } + MyCircularDequeNode prev = next.prev; + if (prev != null) { + prev.next = null; + } else { + this.node = null; + } + lastIndex--; + return true; + } + } + + /** + * Get the front item from the deque. + */ + public int getFront() { + if (this.node == null) { + return -1; + } else { + return this.node.val; + } + } + + /** + * Get the last item from the deque. + */ + public int getRear() { + if (this.node == null) { + return -1; + } else { + MyCircularDequeNode next = this.node; + while (next.next != null) { + next = next.next; + } + return next.val; + } + } + + /** + * Checks whether the circular deque is empty or not. + */ + public boolean isEmpty() { + return lastIndex == 0; + } + + /** + * Checks whether the circular deque is full or not. + */ + public boolean isFull() { + return lastIndex == size; + } + + private class MyCircularDequeNode{ + int val; + MyCircularDequeNode prev; + MyCircularDequeNode next; + + public MyCircularDequeNode(int val) { + this.val = val; + } + } + + } + +// 作者:anz +// 链接:https://leetcode-cn.com/problems/design-circular-deque/solution/shuang-duan-dui-lie-shuang-zhi-zhen-by-anz/ +// 来源:力扣(LeetCode) +// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 +} diff --git a/week01/_9trappingRainWater.java b/week01/_9trappingRainWater.java new file mode 100644 index 00000000..f6207d8e --- /dev/null +++ b/week01/_9trappingRainWater.java @@ -0,0 +1,28 @@ +public class _9trappingRainWater { + class Solution { + // 双指针 + public int trap(int[] height) { + int left = 0, right = height.length - 1; + int ans = 0; + int left_max = 0, right_max = 0; + while (left < right) { + if (height[left] < height[right]) { + if (height[left] >= left_max) { + left_max = height[left]; + } else { + ans += (left_max - height[left]); + } + ++left; + } else { + if (height[right] >= right_max) { + right_max = height[right]; + } else { + ans += (right_max - height[right]); + } + --right; + } + } + return ans; + } + } +} From 1eacf66c210524034531b962627a2460791ddbf5 Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 25 Oct 2020 22:04:35 +0800 Subject: [PATCH 06/20] Delete _1removeDuplicates.java --- week01/_1removeDuplicates.java | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 week01/_1removeDuplicates.java diff --git a/week01/_1removeDuplicates.java b/week01/_1removeDuplicates.java deleted file mode 100644 index 00d97e97..00000000 --- a/week01/_1removeDuplicates.java +++ /dev/null @@ -1,24 +0,0 @@ -/* -* 给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 - -不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。 -*/ -public class _1removeDuplicates { - public static int solution(int[] nums){ - int i = 0; - for (int j = 1 ; j < nums.length ; j++ ) { - if(nums[i] != nums[j] ){ - i++; // i的起始位为0,所以i++在先 - nums[i]=nums[j]; - } - } - return i+1; - } - public static void main(String[] args) { - int[] nums = {1,2,2,3,4,5,5,7,9}; - int l = solution(nums); - for(int i = 0 ; i < l ; i ++){ - System.out.print(nums[i]+"\t"); - } - } -} From 1077e51fa534e2849fcdcc4905e44424bfa78ad4 Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 25 Oct 2020 22:05:04 +0800 Subject: [PATCH 07/20] Delete _9trappingRainWater.java --- week01/_9trappingRainWater.java | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 week01/_9trappingRainWater.java diff --git a/week01/_9trappingRainWater.java b/week01/_9trappingRainWater.java deleted file mode 100644 index f6207d8e..00000000 --- a/week01/_9trappingRainWater.java +++ /dev/null @@ -1,28 +0,0 @@ -public class _9trappingRainWater { - class Solution { - // 双指针 - public int trap(int[] height) { - int left = 0, right = height.length - 1; - int ans = 0; - int left_max = 0, right_max = 0; - while (left < right) { - if (height[left] < height[right]) { - if (height[left] >= left_max) { - left_max = height[left]; - } else { - ans += (left_max - height[left]); - } - ++left; - } else { - if (height[right] >= right_max) { - right_max = height[right]; - } else { - ans += (right_max - height[right]); - } - --right; - } - } - return ans; - } - } -} From e3e7e90270e1ecf83815a57b669133702198045a Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 25 Oct 2020 22:05:59 +0800 Subject: [PATCH 08/20] Delete _2rotateArray.java --- week01/_2rotateArray.java | 58 --------------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 week01/_2rotateArray.java diff --git a/week01/_2rotateArray.java b/week01/_2rotateArray.java deleted file mode 100644 index b4c3400f..00000000 --- a/week01/_2rotateArray.java +++ /dev/null @@ -1,58 +0,0 @@ -/* -* -给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。*/ -public class _2rotateArray { - public static void rotateByForce(int[] nums, int k) { - /** 暴力! - 1、取出末尾k个 - 2、双指针由后向前移动元素 - ? 如果只有k个或者小于k个?? - **/ - //先处理边界条件 - if (nums.length == 1) return; - if (nums.length == k) return; - if (nums.length < k) k = k - nums.length; - - int[] temp = new int[k]; - int t = 0; - for (int i = nums.length - k; i <= nums.length - 1; i++) { - temp[t] = nums[i]; - t++; - } - t = nums.length - 1; - for (int i = nums.length - 1 - k; i > -1; i--) { - nums[t] = nums[i]; - t--; - } - for (int i = 0; i < k; i++) { - nums[i] = temp[i]; - } - } - - public static void main(String[] args) { - int k = 3; - int[] a = {-1}; - int[] b = {1, 2, 3}; - int[] c = {2, 5, 8, 9}; - int[] d = {8, 9}; - rotateByForce(a, k); - for (int i = 0; i < a.length; i++) { - System.out.print(a[i] + "\t"); - } - System.out.println("\n"); - rotateByForce(b, k); - for (int i = 0; i < b.length; i++) { - System.out.print(b[i] + "\t"); - } - System.out.println("\n"); - rotateByForce(c, k); - for (int i = 0; i < c.length; i++) { - System.out.print(c[i] + "\t"); - } - System.out.println("\n"); - rotateByForce(d, k); - for (int i = 0; i < d.length; i++) { - System.out.print(d[i] + "\t"); - } - } -} From f6444bc671e7f7090189fa9d0058f12fc06ffb8a Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 25 Oct 2020 22:06:16 +0800 Subject: [PATCH 09/20] Delete _3mergeTwoSortedLists.java --- week01/_3mergeTwoSortedLists.java | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 week01/_3mergeTwoSortedLists.java diff --git a/week01/_3mergeTwoSortedLists.java b/week01/_3mergeTwoSortedLists.java deleted file mode 100644 index a98fb2bf..00000000 --- a/week01/_3mergeTwoSortedLists.java +++ /dev/null @@ -1,27 +0,0 @@ -/*将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 */ -public class _3mergeTwoSortedLists { - - //Definition for singly-linked list. - public class ListNode { - int val; - ListNode next; - ListNode() {} - ListNode(int val) { this.val = val; } - ListNode(int val, ListNode next) { this.val = val; this.next = next; } - } - public static ListNode mergeTwoLists(ListNode l1,ListNode l2){ - if(l1==null){ - return l2; - }else if (l2 == null) { - return l1; - } - if(l1.val Date: Sun, 25 Oct 2020 22:06:42 +0800 Subject: [PATCH 10/20] Delete _4mergeSortedArray.java --- week01/_4mergeSortedArray.java | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 week01/_4mergeSortedArray.java diff --git a/week01/_4mergeSortedArray.java b/week01/_4mergeSortedArray.java deleted file mode 100644 index 25214efc..00000000 --- a/week01/_4mergeSortedArray.java +++ /dev/null @@ -1,13 +0,0 @@ -import java.util.Arrays; - -/*给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组*/ -public class _4mergeSortedArray { - // 1.暴力+使用Arrays.sort() - public void merge(int[] nums1, int m, int[] nums2, int n) { - int j = 0; - for(int i=nums1.length-nums2.length;i Date: Sun, 25 Oct 2020 22:07:04 +0800 Subject: [PATCH 11/20] Delete _5twoSum.java --- week01/_5twoSum.java | 36 ------------------------------------ 1 file changed, 36 deletions(-) delete mode 100644 week01/_5twoSum.java diff --git a/week01/_5twoSum.java b/week01/_5twoSum.java deleted file mode 100644 index 502db036..00000000 --- a/week01/_5twoSum.java +++ /dev/null @@ -1,36 +0,0 @@ -import java.util.HashMap; -import java.util.Map; - -public class _5twoSum { - //使用HashMap - class Solution { - public int[] twoSum(int[] nums, int target) { - Map hashtable = new HashMap(); - for(int i = 0; i < nums.length ; ++i){ - if(hashtable.containsKey(target - nums[i])){ - return new int[]{hashtable.get(target-nums[i]),i}; - } - hashtable.put(nums[i],i); - } - return new int[0]; - } - } - // 2、暴力 - class Solution2 { - public int[] twoSum(int[] nums, int target) { - int[] a = new int[2]; - for(int i = 0 ; i < nums.length ; i++){ - for(int j = i+1 ; j < nums.length ; j++){ - if(nums[i] + nums[j] == target ){ - a[0]=i; - a[1]=j; - break; - } - } - } - return a; - } - - } - -} From a77a9dcd40defb3fcc0e18966b89ffd2841f29eb Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 25 Oct 2020 22:07:25 +0800 Subject: [PATCH 12/20] Delete _6moveZeroes.java --- week01/_6moveZeroes.java | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 week01/_6moveZeroes.java diff --git a/week01/_6moveZeroes.java b/week01/_6moveZeroes.java deleted file mode 100644 index aac668d1..00000000 --- a/week01/_6moveZeroes.java +++ /dev/null @@ -1,32 +0,0 @@ -public class _6moveZeroes { - class Solution { - public void moveZeroes(int[] nums) { - // 快慢指针法 - int i = 0 ; - for(int j = 0;j Date: Sun, 25 Oct 2020 22:07:42 +0800 Subject: [PATCH 13/20] Delete _7plusOne.java --- week01/_7plusOne.java | 45 ------------------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 week01/_7plusOne.java diff --git a/week01/_7plusOne.java b/week01/_7plusOne.java deleted file mode 100644 index 6107695a..00000000 --- a/week01/_7plusOne.java +++ /dev/null @@ -1,45 +0,0 @@ -public class _7plusOne { - - // 1、自己写的 - class Solution { - public int[] plusOne(int[] digits) { - int temp=0; - int add=1; - for(int i=digits.length-1;i>=0;i--){ - if(add==0)continue; - temp = digits[i]+1; - if(temp==10) - { - digits[i]=0; - add = 1; - }else{ - add = 0; - digits[i]=temp; - } - } - if(add == 1){ - int[] ex = new int[digits.length+1]; - for(int i=digits.length;i>0;i--) - { - ex[i]=digits[i-1]; - } - ex[0]=1; - digits=ex; - } - return digits; - } - } - // 2、一个好题解 - class Solution2 { - public int[] plusOne(int[] digits) { - for(int i = digits.length -1 ; i >=0 ; i--){ - digits[i]++; - digits[i] = digits[i]%10; - if(digits[i] != 0)return digits; - } - digits = new int[digits.length + 1]; - digits[0] = 1; - return digits; - } - } -} From 02a7f70380a30860c0a3a3aeb1ffd1a3f3d6aa8a Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 25 Oct 2020 22:08:07 +0800 Subject: [PATCH 14/20] Delete _8designCircularDeque.java --- week01/_8designCircularDeque.java | 158 ------------------------------ 1 file changed, 158 deletions(-) delete mode 100644 week01/_8designCircularDeque.java diff --git a/week01/_8designCircularDeque.java b/week01/_8designCircularDeque.java deleted file mode 100644 index c0b5ad83..00000000 --- a/week01/_8designCircularDeque.java +++ /dev/null @@ -1,158 +0,0 @@ -public class _8designCircularDeque { - class MyCircularDeque { - private int size; - private int lastIndex; - private MyCircularDequeNode node; - - /** - * Initialize your data structure here. Set the size of the deque to be k. - */ - public MyCircularDeque(int k) { - this.size = k; - } - - /** - * Adds an item at the front of Deque. Return true if the operation is successful. - */ - public boolean insertFront(int value) { - if (lastIndex < size) { - MyCircularDequeNode node = new MyCircularDequeNode(value); - if (this.node == null) { - this.node = node; - lastIndex ++; - } else { - this.node.prev = node; - node.next = this.node; - this.node = node; - lastIndex++; - } - return true; - } else { - return false; - } - } - - /** - * Adds an item at the rear of Deque. Return true if the operation is successful. - */ - public boolean insertLast(int value) { - if (lastIndex < size) { - MyCircularDequeNode node = new MyCircularDequeNode(value); - if (this.node == null) { - this.node = node; - lastIndex ++; - } else { - MyCircularDequeNode oNode = this.node; - while (oNode.next != null) { - oNode = oNode.next; - } - oNode.next = node; - node.prev = oNode; - lastIndex++; - } - return true; - } else { - return false; - } - } - - /** - * Deletes an item from the front of Deque. Return true if the operation is successful. - */ - public boolean deleteFront() { - if (this.node == null) { - return false; - } else { - MyCircularDequeNode next = this.node.next; - if (next == null) { - this.node = null; - lastIndex--; - return true; - } else { - next.prev = null; - this.node = next; - lastIndex--; - return true; - } - } - } - - /** - * Deletes an item from the rear of Deque. Return true if the operation is successful. - */ - public boolean deleteLast() { - if (this.node == null) { - return false; - } else { - MyCircularDequeNode next = this.node; - while (next.next != null) { - next = next.next; - } - MyCircularDequeNode prev = next.prev; - if (prev != null) { - prev.next = null; - } else { - this.node = null; - } - lastIndex--; - return true; - } - } - - /** - * Get the front item from the deque. - */ - public int getFront() { - if (this.node == null) { - return -1; - } else { - return this.node.val; - } - } - - /** - * Get the last item from the deque. - */ - public int getRear() { - if (this.node == null) { - return -1; - } else { - MyCircularDequeNode next = this.node; - while (next.next != null) { - next = next.next; - } - return next.val; - } - } - - /** - * Checks whether the circular deque is empty or not. - */ - public boolean isEmpty() { - return lastIndex == 0; - } - - /** - * Checks whether the circular deque is full or not. - */ - public boolean isFull() { - return lastIndex == size; - } - - private class MyCircularDequeNode{ - int val; - MyCircularDequeNode prev; - MyCircularDequeNode next; - - public MyCircularDequeNode(int val) { - this.val = val; - } - } - - } - -// 作者:anz -// 链接:https://leetcode-cn.com/problems/design-circular-deque/solution/shuang-duan-dui-lie-shuang-zhi-zhen-by-anz/ -// 来源:力扣(LeetCode) -// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 -} From 456c545effc1ecd45ddc4b5e1efc2f660113c88c Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 25 Oct 2020 22:09:43 +0800 Subject: [PATCH 15/20] Update NOTE.md --- week01/NOTE.md | 1 + 1 file changed, 1 insertion(+) diff --git a/week01/NOTE.md b/week01/NOTE.md index ebc8972a..d944a041 100644 --- a/week01/NOTE.md +++ b/week01/NOTE.md @@ -1,3 +1,4 @@ 这一周的学习,发现自己的简单的基础知识还是不错的,对循环和条件的使用比较熟练,题目一般只能想到简单的暴力拆解,复杂的题目基本搬砖。精巧的思路只能仰望了。 这可能是思维习惯上还没有养成,在学校的基础不扎实,学习的知识和工程应用上的脱节。在看超哥课程时还是有一点理解上的困难。 这一周课程比较繁忙,时间上投入较少,部分作业基本照搬了,这一周会抓紧时间来巩固。 +希望够得到班班或者超哥的一些学习建议。 From b552c7ad46d715ae02f86171dffadc1c84f49832 Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 1 Nov 2020 21:04:13 +0800 Subject: [PATCH 16/20] Add files via upload --- week02/Week02/HomeworkList | 17 +++ week02/Week02/_1ValidAnagram.java | 86 +++++++++++ week02/Week02/_2TwoSumByHash.java | 43 ++++++ week02/Week02/_3N_TreedFirstTravel.java | 71 ++++++++++ week02/Week02/_4AnagramGroups.java | 101 +++++++++++++ week02/Week02/_5BinaryTreeMidInorder.java | 53 +++++++ week02/Week02/_6BinaryTreeFirstInorder.java | 51 +++++++ week02/Week02/_7N_TreeLayout.java | 78 ++++++++++ week02/Week02/_8UglyNumber.java | 100 +++++++++++++ week02/Week02/_9TheTopKElements.java | 149 ++++++++++++++++++++ 10 files changed, 749 insertions(+) create mode 100644 week02/Week02/HomeworkList create mode 100644 week02/Week02/_1ValidAnagram.java create mode 100644 week02/Week02/_2TwoSumByHash.java create mode 100644 week02/Week02/_3N_TreedFirstTravel.java create mode 100644 week02/Week02/_4AnagramGroups.java create mode 100644 week02/Week02/_5BinaryTreeMidInorder.java create mode 100644 week02/Week02/_6BinaryTreeFirstInorder.java create mode 100644 week02/Week02/_7N_TreeLayout.java create mode 100644 week02/Week02/_8UglyNumber.java create mode 100644 week02/Week02/_9TheTopKElements.java diff --git a/week02/Week02/HomeworkList b/week02/Week02/HomeworkList new file mode 100644 index 00000000..ca8a2d8f --- /dev/null +++ b/week02/Week02/HomeworkList @@ -0,0 +1,17 @@ +本周作业 + +简单: +写一个关于 HashMap 的小总结。 +说明:对于不熟悉 Java 语言的同学,此项作业可选做。 +1.有效的字母异位词(亚马逊、Facebook、谷歌在半年内面试中考过) +2.两数之和(近半年内,亚马逊考查此题达到 216 次、字节跳动 147 次、谷歌 104 次,Facebook、苹果、微软、腾讯也在近半年内面试常考) +3.N 叉树的前序遍历(亚马逊在半年内面试中考过) +HeapSort :自学 https://www.geeksforgeeks.org/heap-sort/ + +中等: +4.字母异位词分组(亚马逊在半年内面试中常考) +5.二叉树的中序遍历(亚马逊、字节跳动、微软在半年内面试中考过) +6.二叉树的前序遍历(字节跳动、谷歌、腾讯在半年内面试中考过) +7.N 叉树的层序遍历(亚马逊在半年内面试中考过) +8.丑数(字节跳动在半年内面试中考过) +9.前 K 个高频元素(亚马逊在半年内面试中常考) \ No newline at end of file diff --git a/week02/Week02/_1ValidAnagram.java b/week02/Week02/_1ValidAnagram.java new file mode 100644 index 00000000..a580afbd --- /dev/null +++ b/week02/Week02/_1ValidAnagram.java @@ -0,0 +1,86 @@ +package Week02; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +/** + * 有效的字母异位词(亚马逊、Facebook、谷歌在半年内面试中考过)*/ +//给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。 +// +// 示例 1: +// +// 输入: s = "anagram", t = "nagaram" +//输出: true +// +// +// 示例 2: +// +// 输入: s = "rat", t = "car" +//输出: false +// +// 说明: +//你可以假设字符串只包含小写字母。 +// +// 进阶: +//如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况? +// Related Topics 排序 哈希表 +// 👍 268 👎 0 +public class _1ValidAnagram { + public static void main(String[] args) { + // for test + String s1 = "anagram"; + String t1 = "nagaram"; + String s2 = "rat"; + String t2 = "car"; + System.out.println(s1+"\t"+t1+"\t"+"Is Valid Anagram ?\t:"+isAnagram01(s1,t1)); + System.out.println(s2+"\t"+t2+"\t"+"Is Valid Anagram ?\t:"+isAnagram01(s2,t2)); + } + //1.暴力 统计数组 + 两重循环 + public static boolean isAnagram01(String s, String t){ + if(s.length() != t.length()){ + return false; + } + int[] table = new int[26];//仅支持小写 + for(int i=0; i map = new HashMap<>(); + for(char ch: s.toCharArray()){ + map.put(ch,map.getOrDefault(ch,0)+1); + } + for(char ch: t.toCharArray()){ + Integer count = map.get(ch); + if(count == null){ + return false; + }else if(count > 1){ + map.put(ch,count - 1); + }else{ + map.remove(ch); + } + } + return map.isEmpty(); + } +} diff --git a/week02/Week02/_2TwoSumByHash.java b/week02/Week02/_2TwoSumByHash.java new file mode 100644 index 00000000..11c438eb --- /dev/null +++ b/week02/Week02/_2TwoSumByHash.java @@ -0,0 +1,43 @@ +package Week02; +//给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 +// +// 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。 +// +// +// +// 示例: +// +// 给定 nums = [2, 7, 11, 15], target = 9 +// +//因为 nums[0] + nums[1] = 2 + 7 = 9 +//所以返回 [0, 1] +// +// Related Topics 数组 哈希表 +// 👍 9500 👎 0 + +import java.util.HashMap; +import java.util.Map; + +public class _2TwoSumByHash { + // 使用哈希表解 + public static void main(String[] args) { + int[] nums = {2, 7, 11, 15}; + int target = 9; + int[] result = twoSum(nums, target); + if (result.length == 0) { + System.out.println("No such numbers !"); + } + System.out.println(nums[result[0]] + nums[result[1]] + "=" + target); + } + + public static int[] twoSum(int[] nums, int target) { + Map hashtable = new HashMap(); + for (int i = 0; i < nums.length; i++) { + if (hashtable.containsKey(target - nums[i])) { + return new int[]{hashtable.get(target - nums[i]), i}; + } + hashtable.put(nums[i], i); + } + return new int[0]; + } +} diff --git a/week02/Week02/_3N_TreedFirstTravel.java b/week02/Week02/_3N_TreedFirstTravel.java new file mode 100644 index 00000000..de0bd5e6 --- /dev/null +++ b/week02/Week02/_3N_TreedFirstTravel.java @@ -0,0 +1,71 @@ +package Week02; + +//import org.w3c.dom.Node; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class _3N_TreedFirstTravel { + +// Definition for a Node. +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; + } +}; +//1.迭代法 + public static LinkedList preorder(Node root){ + LinkedList stack = new LinkedList<>(); + LinkedList output = new LinkedList<>(); + if(root == null){ + return output; + } + stack.add(root); + while(!stack.isEmpty()){ + Node node = stack.pollLast(); + output.add(node.val); + Collections.reverse(node.children); + for(Node item : node.children){ + stack.add(item); + } + } + return output; + } +//2.递归 +ArrayList res = new ArrayList(); + + public List preorder1(Node root) { + dfs(root); + return res; + } + + public void dfs(Node root) { + if (root == null) { + return; + } + res.add(root.val); + if (root.children == null) { + return; + } + for (Node child: root.children) { + dfs(child); + } + } + +// 作者:yi-wen-statistics +// 链接:https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/solution/589ncha-shu-de-qian-xu-bian-li-duo-jie-by-yi-wen-s/ +// 来源:力扣(LeetCode) +// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 +} diff --git a/week02/Week02/_4AnagramGroups.java b/week02/Week02/_4AnagramGroups.java new file mode 100644 index 00000000..ef2241ef --- /dev/null +++ b/week02/Week02/_4AnagramGroups.java @@ -0,0 +1,101 @@ +package Week02; + +import java.util.*; + +/** + * Given an array of strings strs, group the anagrams together. You can return the answer in any order. + *

+ * An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. + *

+ *   + *

+ * Example 1: + *

+ * Input: strs = ["eat","tea","tan","ate","nat","bat"] + * Output: [["bat"],["nat","tan"],["ate","eat","tea"]] + * Example 2: + *

+ * Input: strs = [""] + * Output: [[""]] + * Example 3: + *

+ * Input: strs = ["a"] + * Output: [["a"]] + *

+ * 来源:力扣(LeetCode) + * 链接:https://leetcode-cn.com/problems/group-anagrams + * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + */ +public class _4AnagramGroups { + //方法一:排序数组分类 + + //方法二:按计数分类 + public List> groupAnagrams(String[] strs) { + if (strs.length == 0) return new ArrayList(); + Map ans = new HashMap(); + int[] count = new int[26]; + for (String s : strs) { + Arrays.fill(count,0); + for (char c : s.toCharArray()) count[c-'a']++; + StringBuilder sb = new StringBuilder(""); + for(int i = 0; i< 26 ;i++){ + sb.append('#'); + sb.append(count[i]); + } + String key = sb.toString(); + if (!ans.containsKey(key)) ans.put(key, new ArrayList()); + ans.get(key).add(s); + } + return new ArrayList(ans.values()); + } + + +} + + + //方法一:排序数组分类 + + public List> groupAnagrams1(String[] strs) { + if (strs.length == 0) return new ArrayList(); + Map ans = new HashMap(); + for (String s : strs) { + char[] ca = s.toCharArray(); + Arrays.sort(ca); + String key = String.valueOf(ca); + if (!ans.containsKey(key)) ans.put(key, new ArrayList()); + ans.get(key).add(s); + } + return new ArrayList(ans.values()); + } + + + /*作者:LeetCode + 链接:https://leetcode-cn.com/problems/group-anagrams/solution/zi-mu-yi-wei-ci-fen-zu-by-leetcode/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/ + //方法二:按计数分类 + public List> groupAnagrams2(String[] strs) { + if (strs.length == 0) return new ArrayList(); + Map ans = new HashMap(); + int[] count = new int[26]; + for (String s : strs) { + Arrays.fill(count, 0); + for (char c : s.toCharArray()) count[c - 'a']++; + + StringBuilder sb = new StringBuilder(""); + for (int i = 0; i < 26; i++) { + sb.append('#'); + sb.append(count[i]); + } + String key = sb.toString(); + if (!ans.containsKey(key)) ans.put(key, new ArrayList()); + ans.get(key).add(s); + } + return new ArrayList(ans.values()); + } + + +//作者:LeetCode +// 链接:https://leetcode-cn.com/problems/group-anagrams/solution/zi-mu-yi-wei-ci-fen-zu-by-leetcode/ +// 来源:力扣(LeetCode) +// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 \ No newline at end of file diff --git a/week02/Week02/_5BinaryTreeMidInorder.java b/week02/Week02/_5BinaryTreeMidInorder.java new file mode 100644 index 00000000..e805f58a --- /dev/null +++ b/week02/Week02/_5BinaryTreeMidInorder.java @@ -0,0 +1,53 @@ +package Week02; + +public class _5BinaryTreeMidInorder { + +} +//1.递归 +class Solution { + public: + void inorder(TreeNode* root, vector& res) { + if (!root) { + return; + } + inorder(root->left, res); + res.push_back(root->val); + inorder(root->right, res); + } + vector inorderTraversal(TreeNode* root) { + vector res; + inorder(root, res); + return res; + } +}; + +作者:LeetCode-Solution + 链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/solution/er-cha-shu-de-zhong-xu-bian-li-by-leetcode-solutio/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 + + +//2、迭代 +class Solution { + public: + vector inorderTraversal(TreeNode* root) { + vector res; + stack stk; + while (root != nullptr || !stk.empty()) { + while (root != nullptr) { + stk.push(root); + root = root->left; + } + root = stk.top(); + stk.pop(); + res.push_back(root->val); + root = root->right; + } + return res; + } +}; + +作者:LeetCode-Solution + 链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/solution/er-cha-shu-de-zhong-xu-bian-li-by-leetcode-solutio/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 \ No newline at end of file diff --git a/week02/Week02/_6BinaryTreeFirstInorder.java b/week02/Week02/_6BinaryTreeFirstInorder.java new file mode 100644 index 00000000..04f7222b --- /dev/null +++ b/week02/Week02/_6BinaryTreeFirstInorder.java @@ -0,0 +1,51 @@ +package Week02; + +public class _6BinaryTreeFirstInorder { +} +class Solution { + public List preorderTraversal(TreeNode root) { + List res = new ArrayList(); + preorder(root, res); + return res; + } + + public void preorder(TreeNode root, List res) { + if (root == null) { + return; + } + res.add(root.val); + preorder(root.left, res); + preorder(root.right, res); + } +} + +作者:LeetCode-Solution + 链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/solution/er-cha-shu-de-qian-xu-bian-li-by-leetcode-solution/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 +class Solution { + public List preorderTraversal(TreeNode root) { + List res = new ArrayList(); + if (root == null) { + return res; + } + + Deque stack = new LinkedList(); + TreeNode node = root; + while (!stack.isEmpty() || node != null) { + while (node != null) { + res.add(node.val); + stack.push(node); + node = node.left; + } + node = stack.pop(); + node = node.right; + } + return res; + } +} + +作者:LeetCode-Solution + 链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/solution/er-cha-shu-de-qian-xu-bian-li-by-leetcode-solution/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 \ No newline at end of file diff --git a/week02/Week02/_7N_TreeLayout.java b/week02/Week02/_7N_TreeLayout.java new file mode 100644 index 00000000..e5680ed8 --- /dev/null +++ b/week02/Week02/_7N_TreeLayout.java @@ -0,0 +1,78 @@ +package Week02; + +public class _7N_TreeLayout { + +} +方法一:利用队列实现广度优先搜索 + List values = new ArrayList<>(); + Queue queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + Node nextNode = queue.remove(); + values.add(nextNode.val); + for (Node child : nextNode.children) { + queue.add(child); + } + } + + 作者:LeetCode + 链接:https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/solution/ncha-shu-de-ceng-xu-bian-li-by-leetcode/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 + + 方法二:简化的广度优先搜索 +// This code is a modified version of the code posted by +// #zzzliu on the discussion forums. +class Solution { + + public List> levelOrder(Node root) { + List> result = new ArrayList<>(); + if (root == null) return result; + + List previousLayer = Arrays.asList(root); + + while (!previousLayer.isEmpty()) { + List currentLayer = new ArrayList<>(); + List previousVals = new ArrayList<>(); + for (Node node : previousLayer) { + previousVals.add(node.val); + currentLayer.addAll(node.children); + } + result.add(previousVals); + previousLayer = currentLayer; + } + + return result; + } +} + +作者:LeetCode + 链接:https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/solution/ncha-shu-de-ceng-xu-bian-li-by-leetcode/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 + + 方法三:递归 +class Solution { + + private List> result = new ArrayList<>(); + + public List> levelOrder(Node root) { + if (root != null) traverseNode(root, 0); + return result; + } + + private void traverseNode(Node node, int level) { + if (result.size() <= level) { + result.add(new ArrayList<>()); + } + result.get(level).add(node.val); + for (Node child : node.children) { + traverseNode(child, level + 1); + } + } +} + +作者:LeetCode + 链接:https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/solution/ncha-shu-de-ceng-xu-bian-li-by-leetcode/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 \ No newline at end of file diff --git a/week02/Week02/_8UglyNumber.java b/week02/Week02/_8UglyNumber.java new file mode 100644 index 00000000..dd7fd7db --- /dev/null +++ b/week02/Week02/_8UglyNumber.java @@ -0,0 +1,100 @@ +package Week02; +/**我们把只包含质因子 2、3 和 5 的数称作丑数(Ugly Number)。求按从小到大的顺序的第 n 个丑数。*/ +/**示例: + + 输入: n = 10 + 输出: 12 + 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。 + 说明:   + + 1 是丑数。 + n 不超过1690。 + 注意:本题与主站 264 题相同:https://leetcode-cn.com/problems/ugly-number-ii/ + + 来源:力扣(LeetCode) + 链接:https://leetcode-cn.com/problems/chou-shu-lcof + 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。*/ +public class _8UglyNumber { +} + +class Solution { + private int[] uglyNumber = {2,3,5}; + public int nthUglyNumber(int n) { + //创建小根堆,每次出堆的都是最小值 + Queue queue = new PriorityQueue<>(); + queue.add(1L); + //记录出堆的个数,出堆的元素完全按照从小到大排序 + int count = 0; + while (! queue.isEmpty()){ + long cut = queue.poll(); + + //如果出堆的个数>=n,当前cut就是第n个丑数 + if(++count >= n){ + return (int) cut; + } + for(int num : uglyNumber){ + //排除重复的数字 + if(! queue.contains(num * cut)){ + queue.add(num * cut); + } + } + } + return -1; + } +} + +作者:YanShaoJiangHu + 链接:https://leetcode-cn.com/problems/chou-shu-lcof/solution/li-yong-xiao-gen-dui-wan-mei-jie-jue-by-yanshaojia/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 +//动态规划 +class Solution { + public int nthUglyNumber(int n) { + int a = 0, b = 0, c = 0; + int[] dp = new int[n]; + dp[0] = 1; + for(int i = 1; i < n; i++) { + int n2 = dp[a] * 2, n3 = dp[b] * 3, n5 = dp[c] * 5; + dp[i] = Math.min(Math.min(n2, n3), n5); + if(dp[i] == n2) a++; + if(dp[i] == n3) b++; + if(dp[i] == n5) c++; + } + return dp[n - 1]; + } +} + +作者:jyd + 链接:https://leetcode-cn.com/problems/chou-shu-lcof/solution/mian-shi-ti-49-chou-shu-dong-tai-gui-hua-qing-xi-t/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 + /** + *定义三个指针p2,p3,p5,p2指向的数字永远乘2,p3指向的数字永远乘3,p5指向的数字永远乘5 + * 初始化所有指针都指向第一个丑数,即1 + * 我们从2*dp[p2], 3*dp[p3], 5*dp[p5]选取最小的一个数字,作为新的丑数。这边新的丑数就是2*dp[p2]=2*1=2,然后p2++ + * 此时p3和p5指向第1个丑数,p2指向第2个丑数。然后重复上一步 + * 这里基于的一个事实是,丑数数列是递增的,当p5指针在当前位置时,后面的数乘以5必然比前面的数乘以5大,所以下一个丑数必然是先考虑前面的数乘以5。p2,p3同理,所以才可以使用指针 + * + * 作者:yuanninesuns + * 链接:https://leetcode-cn.com/problems/chou-shu-lcof/solution/xiang-xi-jie-da-chao-jian-dan-bu-dong-zhao-wo-by-y/ + * 来源:力扣(LeetCode) + * 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 + */ + */ +public int nthUglyNumber(int n) { + int p2=0,p3=0,p5=0; + int[] dp=new int[n]; + dp[0]=1; + for(int i=1;i occurrences = new HashMap(); + for (int num : nums) { + occurrences.put(num, occurrences.getOrDefault(num, 0) + 1); + } + + // int[] 的第一个元素代表数组的值,第二个元素代表了该值出现的次数 + PriorityQueue queue = new PriorityQueue(new Comparator() { + public int compare(int[] m, int[] n) { + return m[1] - n[1]; + } + }); + for (Map.Entry entry : occurrences.entrySet()) { + int num = entry.getKey(), count = entry.getValue(); + if (queue.size() == k) { + if (queue.peek()[1] < count) { + queue.poll(); + queue.offer(new int[]{num, count}); + } + } else { + queue.offer(new int[]{num, count}); + } + } + int[] ret = new int[k]; + for (int i = 0; i < k; ++i) { + ret[i] = queue.poll()[0]; + } + return ret; + } +} + +作者:LeetCode-Solution + 链接:https://leetcode-cn.com/problems/top-k-frequent-elements/solution/qian-k-ge-gao-pin-yuan-su-by-leetcode-solution/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 + + 方法二:基于快速排序 + 思路与算法 + + 我们可以使用基于快速排序的方法,求出「出现次数数组」的前 kk 大的值。 + + 在对数组 \textit{arr}[l \ldots r]arr[l…r] 做快速排序的过程中,我们首先将数组划分为两个部分 \textit{arr}[i \ldots q-1]arr[i…q−1] 与 \textit{arr}[q+1 \ldots j]arr[q+1…j],并使得 \textit{arr}[i \ldots q-1]arr[i…q−1] 中的每一个值都不超过 \textit{arr}[q]arr[q],且 \textit{arr}[q+1 \ldots j]arr[q+1…j] 中的每一个值都大于 \textit{arr}[q]arr[q]。 + + 于是,我们根据 kk 与左侧子数组 \textit{arr}[i \ldots q-1]arr[i…q−1] 的长度(为 q-iq−i)的大小关系: + + 如果 k \le q-ik≤q−i,则数组 \textit{arr}[l \ldots r]arr[l…r] 前 kk 大的值,就等于子数组 \textit{arr}[i \ldots q-1]arr[i…q−1] 前 kk 大的值。 + 否则,数组 \textit{arr}[l \ldots r]arr[l…r] 前 kk 大的值,就等于左侧子数组全部元素,加上右侧子数组 \textit{arr}[q+1 \ldots j]arr[q+1…j] 中前 k - (q - i)k−(q−i) 大的值。 + 原版的快速排序算法的平均时间复杂度 为 O(N\log N)O(NlogN)。我们的算法中,每次只需在其中的一个分支递归即可,因此算法的平均时间复杂度降为 O(N)O(N)。 + + 作者:LeetCode-Solution + 链接:https://leetcode-cn.com/problems/top-k-frequent-elements/solution/qian-k-ge-gao-pin-yuan-su-by-leetcode-solution/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 +class Solution { + public int[] topKFrequent(int[] nums, int k) { + Map occurrences = new HashMap(); + for (int num : nums) { + occurrences.put(num, occurrences.getOrDefault(num, 0) + 1); + } + + List values = new ArrayList(); + for (Map.Entry entry : occurrences.entrySet()) { + int num = entry.getKey(), count = entry.getValue(); + values.add(new int[]{num, count}); + } + int[] ret = new int[k]; + qsort(values, 0, values.size() - 1, ret, 0, k); + return ret; + } + + public void qsort(List values, int start, int end, int[] ret, int retIndex, int k) { + int picked = (int) (Math.random() * (end - start + 1)) + start; + Collections.swap(values, picked, start); + + int pivot = values.get(start)[1]; + int index = start; + for (int i = start + 1; i <= end; i++) { + if (values.get(i)[1] >= pivot) { + Collections.swap(values, index + 1, i); + index++; + } + } + Collections.swap(values, start, index); + + if (k <= index - start) { + qsort(values, start, index - 1, ret, retIndex, k); + } else { + for (int i = start; i <= index; i++) { + ret[retIndex++] = values.get(i)[0]; + } + if (k > index - start + 1) { + qsort(values, index + 1, end, ret, retIndex, k - (index - start + 1)); + } + } + } +} + +作者:LeetCode-Solution + 链接:https://leetcode-cn.com/problems/top-k-frequent-elements/solution/qian-k-ge-gao-pin-yuan-su-by-leetcode-solution/ + 来源:力扣(LeetCode) + 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 \ No newline at end of file From fb6987f2b3cebf0c9c42629fd4aaf82daec54d25 Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 1 Nov 2020 21:09:51 +0800 Subject: [PATCH 17/20] Update NOTE.md --- week02/NOTE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week02/NOTE.md b/week02/NOTE.md index 50de3041..32aeea6c 100644 --- a/week02/NOTE.md +++ b/week02/NOTE.md @@ -1 +1 @@ -学习笔记 \ No newline at end of file +本周还是没有找到正确的学习姿势,进入到学习状态,感觉知识点都能听明白,但是做题就很懵,甚至是完全看着题解才能写出来。有点不知所措。今天身体有点不舒服,为了不错过交作业的时间就先潦草的把作业交了。感觉需要砸进去好多时间,但是自己现在做毕设也需要学很多东西,而且都在起步阶段。所以,不知道该怎么提高学习算法训练营的效率。 From 6085581e717df14cf36d3f4391159e8ca5ecc2e9 Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 1 Nov 2020 21:13:30 +0800 Subject: [PATCH 18/20] Update NOTE.md --- week02/NOTE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week02/NOTE.md b/week02/NOTE.md index 32aeea6c..81958bae 100644 --- a/week02/NOTE.md +++ b/week02/NOTE.md @@ -1 +1 @@ -本周还是没有找到正确的学习姿势,进入到学习状态,感觉知识点都能听明白,但是做题就很懵,甚至是完全看着题解才能写出来。有点不知所措。今天身体有点不舒服,为了不错过交作业的时间就先潦草的把作业交了。感觉需要砸进去好多时间,但是自己现在做毕设也需要学很多东西,而且都在起步阶段。所以,不知道该怎么提高学习算法训练营的效率。 +本周还是没有找到正确的学习姿势,进入到学习状态,感觉知识点都能听明白,但是做题就很懵,甚至是完全看着题解才能写出来。有点不知所措。今天身体有点不舒服,为了不错过交作业的时间就先潦草的把作业交了。感觉需要砸进去好多时间,但是自己现在做毕设也需要学很多东西,而且都在起步阶段。所以,不知道该怎么提高学习算法训练营的效率。不知道像我目前这样的状态还能不能抢救应该怎么抢救。 From 751f463dd75381f0953866c88fe1153c38712fe1 Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 15 Nov 2020 14:45:00 +0800 Subject: [PATCH 19/20] Update NOTE.md --- week04/NOTE.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/week04/NOTE.md b/week04/NOTE.md index 50de3041..02570cce 100644 --- a/week04/NOTE.md +++ b/week04/NOTE.md @@ -1 +1,31 @@ -学习笔记 \ No newline at end of file +学习笔记 +public class FindUnOrder { + public static void main(String[] args) { + int[] datas = {4, 5, 6, 7, 0, 1, 2}; + int targetId = opt(datas); + System.out.println(targetId + "\t" + datas[targetId]); + } +/** + * 问题:寻找一个半有序数组 [4, 5, 6, 7, 0, 1, 2] 中间无序的地方 + * 题目分析:这样一个前半段有序后半段也是有序的数组 + * 本质就是旋转数组寻找最小值问题 + * 使用二分查找 代码如下 + * 反复比较中间值和右端的大小 + * 如果中间值大就把寻找区间往右移动 + * 反之往左移动直到找到最小中间元素的下标并输出 + */ + + private static int opt(int[] numbers) { + int left = 0; + int right = numbers.length-1; + while(left < right){ + int mid = (left + right)/2; + if (numbers[mid] > numbers[right]) + left = mid + 1; + else if (numbers[mid] < numbers[right]) + right = mid ; + else right -=1; + } + return left; + } +} From ae7d5b5324caa4885c45280e59825ff3f0af397c Mon Sep 17 00:00:00 2001 From: ZCW223 <62451437+ZCW223@users.noreply.github.com> Date: Sun, 15 Nov 2020 14:47:51 +0800 Subject: [PATCH 20/20] Add files via upload --- week04/_1_LemonadeChange.java | 47 ++++++++++++++++++ week04/_2_BestTimeToBuyAndSellStockII.java | 40 ++++++++++++++++ week04/_3_Solution.java | 41 ++++++++++++++++ week04/_4_WalkingRobotSimulation.java | 55 ++++++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 week04/_1_LemonadeChange.java create mode 100644 week04/_2_BestTimeToBuyAndSellStockII.java create mode 100644 week04/_3_Solution.java create mode 100644 week04/_4_WalkingRobotSimulation.java diff --git a/week04/_1_LemonadeChange.java b/week04/_1_LemonadeChange.java new file mode 100644 index 00000000..7330b31b --- /dev/null +++ b/week04/_1_LemonadeChange.java @@ -0,0 +1,47 @@ +public class _1_LemonadeChange { + /** + * 860. 柠檬水找零 + 在柠檬水摊上,每一杯柠檬水的售价为 5 美元。 + + 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯。 + + 每位顾客只买一杯柠檬水,然后向你付 5 美元、10 美元或 20 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 5 美元。 + + 注意,一开始你手头没有任何零钱。 + + 如果你能给每位顾客正确找零,返回 true ,否则返回 false 。*/ + public static void main(String[] args) { + int[] customsABills = {5, 5, 5, 10, 10}; + int[] customsBBills = {5, 5, 10, 20, 10}; + System.out.println("can you provide the correct change to customsA ?\t" + lemonadeChange(customsABills)); + System.out.println("can you provide the correct change to customsB ?\t" + lemonadeChange(customsBBills)); + } + + private static boolean lemonadeChange(int[] Bills) { + if (Bills.length == 0) { + return true; + } + int five = 0; + int ten = 0; + for (int Bill : Bills) { + if (Bill == 5) { + five++; + } + if (Bill == 10) { + if (five >= 1) { + five--; + ten++; + } else return false; + } + if (Bill == 20) { + if (ten >= 1 && five >= 1) { + ten--; + five--; + } else if (five >= 3) { + five -= 3; + } else return false; + } + } + return true; + } +} diff --git a/week04/_2_BestTimeToBuyAndSellStockII.java b/week04/_2_BestTimeToBuyAndSellStockII.java new file mode 100644 index 00000000..3d1290d1 --- /dev/null +++ b/week04/_2_BestTimeToBuyAndSellStockII.java @@ -0,0 +1,40 @@ +public class _2_BestTimeToBuyAndSellStockII { + /** + * 122. 买卖股票的最佳时机 II + 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 + + 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。 + + 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 + + + + 示例 1: + + 输入: [7,1,5,3,6,4] + 输出: 7 + 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。 + 随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。 + 示例 2: + + 输入: [1,2,3,4,5] + 输出: 4 + 解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。 + 注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。 + 因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。 + 示例 3: + + 输入: [7,6,4,3,1] + 输出: 0 + 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。*/ + public int maxProfit(int[] prices) { + //一次遍历,比较差价的解法 + int ans = 0; + int n = prices.length; + for (int i = 1; i < n; i++) { + if(prices[i] > prices[i-1]) + ans += (prices[i] - prices[i-1]); + } + return ans; + } +} diff --git a/week04/_3_Solution.java b/week04/_3_Solution.java new file mode 100644 index 00000000..62b73257 --- /dev/null +++ b/week04/_3_Solution.java @@ -0,0 +1,41 @@ +import java.util.Arrays; +/** + * 455. 分发饼干 + 假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。 + + 对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。 + + + 示例 1: + + 输入: g = [1,2,3], s = [1,1] + 输出: 1 + 解释: + 你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。 + 虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。 + 所以你应该输出1。 + 示例 2: + + 输入: g = [1,2], s = [1,2,3] + 输出: 2 + 解释: + 你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。 + 你拥有的饼干数量和尺寸都足以让所有孩子满足。 + 所以你应该输出2.*/ +public class _3_Solution { + public int findContentChildren(int[] g, int[] s) { + //双指针法,对两个数组先排序再比较 + if(g.length == 0 || s.length == 0) return 0; + int g0 = 0; + int s0 = 0; + Arrays.sort(g); + Arrays.sort(s); + for( ;g0 obstacleSet = new HashSet(); + for (int[] obstacle: obstacles) { + long ox = (long) obstacle[0] + 30000; + long oy = (long) obstacle[1] + 30000; + obstacleSet.add((ox << 16) + oy); + } + + int ans = 0; + for (int cmd: commands) { + if (cmd == -2) //left + di = (di + 3) % 4; + else if (cmd == -1) //right + di = (di + 1) % 4; + else { + for (int k = 0; k < cmd; ++k) { + int nx = x + dx[di]; + int ny = y + dy[di]; + long code = (((long) nx + 30000) << 16) + ((long) ny + 30000); + if (!obstacleSet.contains(code)) { + x = nx; + y = ny; + ans = Math.max(ans, x*x + y*y); + } + } + } + } + + return ans; + } +}