diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..ed03b012b --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# kdiff3 ignore +*.orig + +# maven ignore +target/ + +# eclipse ignore +.settings/ +.project +.classpath + +# idea ignore +.idea/ +*.ipr +*.iml +*.iws + +# temp ignore +*.log +*.cache +*.diff +*.patch +*.tmp + +# system ignore +.DS_Store +Thumbs.db + +# package ignore (optional) +# *.jar +# *.war +# *.zip +# *.tar +# *.tar.gz \ No newline at end of file diff --git a/Week01/NOTE.md b/Week01/NOTE.md index 50de30414..ef72d67ed 100644 --- a/Week01/NOTE.md +++ b/Week01/NOTE.md @@ -1 +1,10 @@ -学习笔记 \ No newline at end of file +学习笔记 + + +1:本周链表和数组掌握程度50%,感觉基础问题不大 +2:队列,栈,优先队列,双端队列,还需要再消化下,再完成作业 + +快捷键: +vscode代码格式化: shift+option+F +idea代码格式化: option+comand +L +插入代码: i \ No newline at end of file diff --git a/Week01/src/main/java/IsPalindrome.java b/Week01/src/main/java/IsPalindrome.java new file mode 100644 index 000000000..413d7c296 --- /dev/null +++ b/Week01/src/main/java/IsPalindrome.java @@ -0,0 +1,70 @@ +public class IsPalindrome { + + // 帅选+判断,将字母和数字筛选出来,组成一个新的字符串,之后再进行比对 + public static boolean isPalindrome1(String s) { + StringBuffer sgood = new StringBuffer(); + for (int i = 0; i < s.length(); i++) { + char ch = s.charAt(i); + if (Character.isLetterOrDigit(ch)) { + sgood.append(Character.toLowerCase(ch)); + } + } + + // 反转字符串,直接调用库函数 + StringBuffer reverseStr = new StringBuffer(sgood).reverse(); + return sgood.toString().equals(reverseStr.toString()); + } + + // 双指针方式 + public static boolean isPalindrome2(String s) { + // 字符串比对只保留字母和数字,构建新的字符串,空间复杂度是O(s) + StringBuffer newStr = new StringBuffer(); + for (int i = 0; i < s.length(); i++) { + char ch = s.charAt(i); + + if (Character.isLetterOrDigit(ch)) { + newStr.append(Character.toLowerCase(ch)); + } + } + + // 双指针 + int left = 0, right = newStr.length() - 1; + + while (left < right) { + if (newStr.charAt(left) != newStr.charAt(right)) { + return false; + } + ++left; + --right; + } + return true; + } + + // 双指针,直接在原来的字符串上进行比对 + public static boolean isPalindrome(String s) { + int left = 0, right = s.length() - 1; + + while (left < right) { + while ((left < right) && !Character.isLetterOrDigit(s.charAt(left))) { + ++left; + } + while ((left < right) && !Character.isLetterOrDigit(s.charAt(right))) { + --right; + } + + if (left < right){ + if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { + return false; + } + ++left; + --right; + } + } + return true; + } + + public static void main(String[] args) { + String input = "A man, a plan, a canal: Panama"; + System.out.println(isPalindrome(input)); + } +} \ No newline at end of file diff --git a/Week01/src/main/java/KuoHaoValid.java b/Week01/src/main/java/KuoHaoValid.java new file mode 100644 index 000000000..c0949f949 --- /dev/null +++ b/Week01/src/main/java/KuoHaoValid.java @@ -0,0 +1,59 @@ +//--------------------- Change Logs---------------------- +//
@author ruirui.qu Initial Created at 2020-08-23
+//-------------------------------------------------------
+
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Stack;
+
+public class KuoHaoValid {
+
+ public static boolean isValid1(String s) {
+ int n = s.length();
+ if (n % 2 == 1) {
+ return false;
+ }
+
+ Map @author ruirui.qu Initial Created at 2020-06-14
+//-------------------------------------------------------
+
+public class MergeTwoArrays {
+
+ public static void merge(int[] nums1, int m, int[] nums2, int n) {
+ //总长度
+ int i = m + n - 1;
+ int p = m - 1;
+ int q = n - 1;
+
+ while (p >= 0 && q >= 0) {
+ if (nums1[p] < nums2[q]) {
+ nums1[i] = nums2[q];
+ q--;
+ i--;
+ } else if (nums1[p] > nums2[q]) {
+ nums1[i] = nums1[p];
+ p--;
+ i--;
+ } else {
+ nums1[i] = nums2[q];
+ nums1[i - 1] = nums1[p];
+ q--;
+ p--;
+ i = i - 2;
+ }
+ }
+
+ while (p < 0 && q >= 0) {
+ nums1[i] = nums2[q];
+ i--;
+ q--;
+ }
+
+ }
+
+
+ public static void main(String[] args) {
+ int[] m = new int[]{1, 2, 3, 0, 0, 0};
+
+ int[] n = new int[]{2, 5, 6};
+
+
+ merge(m, 3, n, 3);
+
+ System.out.println("===>"+m);
+ }
+}
\ No newline at end of file
diff --git a/Week01/src/main/java/MergeTwoLists.java b/Week01/src/main/java/MergeTwoLists.java
new file mode 100644
index 000000000..027586b71
--- /dev/null
+++ b/Week01/src/main/java/MergeTwoLists.java
@@ -0,0 +1,46 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-06-14
+//-------------------------------------------------------
+
+public class MergeTwoLists {
+
+ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
+ ListNode head = null;
+ head = getHead(head, l1, l2);
+ return head;
+ }
+
+ public ListNode getHead(ListNode head, ListNode l1, ListNode l2) {
+
+ if (l1 == null && l2 == null) {
+ return null;
+ }
+ if (l1 == null) {
+ head = l2;
+ return head;
+ }
+ if (l2 == null) {
+ head = l1;
+ return head;
+ }
+ if (l1.val < l2.val) {
+ head = l1;
+ l1 = l1.next;
+ } else {
+ head = l2;
+ l2 = l2.next;
+ }
+ head.next = getHead(head.next, l1, l2);
+ return head;
+ }
+
+
+ public class ListNode {
+ int val;
+ ListNode next;
+
+ ListNode(int x) {
+ val = x;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Week01/src/main/java/MinStack.java b/Week01/src/main/java/MinStack.java
new file mode 100644
index 000000000..8aa1e0068
--- /dev/null
+++ b/Week01/src/main/java/MinStack.java
@@ -0,0 +1,37 @@
+import java.util.Stack;
+
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-23
+// 155,使用辅助栈
+//-------------------------------------------------------
+public class MinStack {
+ private Stack @author ruirui.qu Initial Created at 2020-08-23
+//-------------------------------------------------------
+public class MinStack1 {
+ private Stack @author ruirui.qu Initial Created at 2020-08-23
+// 自定义栈
+//-------------------------------------------------------
+public class MinStack2 {
+
+ private Node head;
+
+ public void push (int x){
+ if (head ==null){
+ head = new Node(x,x);
+ }else{
+ head = new Node(x,Math.min(x,head.min),head);
+ }
+ }
+
+ public void pop(){
+ head = head.next;
+ }
+
+ public int top(){
+ return head.val;
+ }
+
+ public int getMin(){
+ return head.min;
+ }
+ public static class Node {
+ int val;
+ int min;
+ Node next;
+
+ public Node(int val, int min) {
+ this.val = val;
+ this.min = min;
+ }
+
+ public Node(int val, int min, Node next) {
+ this.val = val;
+ this.min = min;
+ this.next = next;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Week01/src/main/java/PlusOne.java b/Week01/src/main/java/PlusOne.java
new file mode 100644
index 000000000..88281bb92
--- /dev/null
+++ b/Week01/src/main/java/PlusOne.java
@@ -0,0 +1,41 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-06-14
+//-------------------------------------------------------
+
+public class PlusOne {
+ public static int[] plusOne(int[] digits) {
+ if (digits == null) {
+ return digits;
+ }
+ int ci = 0;
+ for (int i = digits.length - 1; i >= 0; i--) {
+ int value = 0;
+ if (i == digits.length - 1) {
+ value = (digits[i] + 1 + ci) % 10;
+ ci = (digits[i] + 1 + ci) / 10;
+ } else {
+ value = (digits[i] + ci) % 10;
+ ci = (digits[i] + ci) / 10;
+ }
+ digits[i] = value;
+ }
+ if (ci == 0) {
+ return digits;
+ }
+ int[] result = new int[digits.length + 1];
+ result[0] = ci;
+ for (int i = 0; i < digits.length; i++) {
+ result[i + 1] = digits[i];
+ }
+ return result;
+ }
+
+
+ public static void main(String[] args) {
+ int[] input = new int[]{1, 2, 3};
+
+ int[] result = plusOne(input);
+
+ System.out.println("===>" + result);
+ }
+}
\ No newline at end of file
diff --git a/Week01/src/main/java/RemoveDuplicates.java b/Week01/src/main/java/RemoveDuplicates.java
new file mode 100644
index 000000000..c48b72434
--- /dev/null
+++ b/Week01/src/main/java/RemoveDuplicates.java
@@ -0,0 +1,35 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-06-14
+//-------------------------------------------------------
+
+import java.util.Arrays;
+
+public class RemoveDuplicates {
+
+ public static int removeDuplicates(int[] nums) {
+ if (nums == null || nums.length <= 1) {
+ return nums.length;
+ }
+ Arrays.sort(nums);
+
+ int i = 0;
+
+ for (int j = 1; j < nums.length; j++) {
+ if (nums[j] != nums[i]) {
+ i++;
+ nums[i] = nums[j];
+ }
+ }
+
+ return i+1;
+ }
+
+
+ public static void main(String[] args) {
+ int[] inputArray = new int[]{1, 1, 2};
+
+ int result = removeDuplicates(inputArray);
+
+ System.out.println("===>" + result);
+ }
+}
\ No newline at end of file
diff --git a/Week01/src/main/java/Rotate.java b/Week01/src/main/java/Rotate.java
new file mode 100644
index 000000000..a9e0c5967
--- /dev/null
+++ b/Week01/src/main/java/Rotate.java
@@ -0,0 +1,40 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-06-14
+//-------------------------------------------------------
+
+public class Rotate {
+
+ public static void rotate(int[] nums, int k) {
+ if (nums == null || nums.length <= 0) {
+ return;
+ }
+
+ int[] tempArray = new int[nums.length];
+
+ //暴力解决
+ for (int i = 0; i < k; i++) {
+ int moveValue = nums[nums.length - 1];
+
+ for (int j = 0; j @author ruirui.qu Initial Created at 2020-06-14
+//-------------------------------------------------------
+
+public class Trap {
+
+ public int trap(int[] height) {
+ //取当前柱子的左边最大,右边最大,减去柱子的高度
+ if (height == null || height.length == 0) {
+ return 0;
+ }
+ int sum = 0;
+ int length = height.length;
+ for (int i = 0; i < length - 1; i++) {
+ int maxLeft = 0;
+ int maxRight = 0;
+ for (int j = i; j >= 0; j--) {
+ maxLeft = Math.max(maxLeft, height[j]);
+ }
+ for (int j = i; j < length; j++) {
+ maxRight = Math.max(maxRight, height[j]);
+ }
+ sum = sum + Math.min(maxLeft, maxRight) - height[i];
+ }
+ return sum;
+ }
+}
\ No newline at end of file
diff --git a/Week01/src/main/java/TwoSum.java b/Week01/src/main/java/TwoSum.java
new file mode 100644
index 000000000..a0a60001c
--- /dev/null
+++ b/Week01/src/main/java/TwoSum.java
@@ -0,0 +1,56 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-06-14
+//-------------------------------------------------------
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+public class TwoSum {
+
+ public static int[] twoSum(int[] nums, int target) {
+ List @author ruirui.qu Initial Created at 2020-08-23
+//-------------------------------------------------------
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class GroupAnagrams {
+
+ public List @author ruirui.qu Initial Created at 2020-08-23
+//-------------------------------------------------------
+
+import java.util.Arrays;
+
+public class IsAnagram {
+
+ //字母个数一致
+ public static boolean isAnagram1(String s, String t) {
+ if (s == null && t == null) {
+ return false;
+ }
+ if (s.length() != t.length()) {
+ return false;
+ }
+ char[] sChar = s.toCharArray();
+ char[] tChar = t.toCharArray();
+
+ Arrays.sort(sChar);
+ Arrays.sort(tChar);
+
+ return Arrays.equals(sChar, tChar);
+ }
+
+ public static boolean isAnagram(String s, String t) {
+ if (s == null && t == null) {
+ return false;
+ }
+ if (s.length() != t.length()) {
+ return false;
+ }
+ int[] counter = new int[26];
+ for (int i = 0; i < s.length(); i++) {
+ counter[s.charAt(i) - 'a']++;
+ counter[t.charAt(i) - 'a']--;
+ }
+
+ for (int i = 0; i < counter.length; i++) {
+ if (counter[i] != 0) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
\ No newline at end of file
diff --git a/Week03/src/main/java/BuildTree.java b/Week03/src/main/java/BuildTree.java
new file mode 100644
index 000000000..811601b0b
--- /dev/null
+++ b/Week03/src/main/java/BuildTree.java
@@ -0,0 +1,54 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-23
+//-------------------------------------------------------
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class BuildTree {
+
+ public static class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ public TreeNode(int val) {
+ this.val = val;
+ }
+ }
+
+ private Map @author ruirui.qu Initial Created at 2020-08-23
+//-------------------------------------------------------
+
+public class LowestCommonAncestor {
+ private TreeNode ans;
+
+ public LowestCommonAncestor() {
+ this.ans = null;
+ }
+
+ private boolean dfs(TreeNode root, TreeNode p, TreeNode q) {
+ if (root == null) {
+ return false;
+ }
+ boolean lson = dfs(root.left, p, q);
+ boolean rson = dfs(root.right, p, q);
+ if ((lson && rson) || ((root.val == p.val || root.val == q.val) && (lson || rson))) {
+ ans = root;
+ }
+ return lson || rson || (root.val == p.val || root.val == q.val);
+ }
+
+ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
+ this.dfs(root, p, q);
+ return this.ans;
+ }
+
+ public static class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ public TreeNode(int val) {
+ this.val = val;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Week04/NOTE.md b/Week04/NOTE.md
index 50de30414..761303bbc 100644
--- a/Week04/NOTE.md
+++ b/Week04/NOTE.md
@@ -1 +1,6 @@
-学习笔记
\ No newline at end of file
+贪心算法是不是就是每次都是选取最优||或者优先满足当前条件;然后重复每次选择的步骤;一镜到底的节奏呢?
+
+贪心 回溯 递归 动态规划
+
+上述四个,做题的时候,总觉得都是在要么for;要么递归呢
+多练多练 再看看
diff --git a/Week04/src/main/java/FindContentChildren.java b/Week04/src/main/java/FindContentChildren.java
new file mode 100644
index 000000000..a812c2e61
--- /dev/null
+++ b/Week04/src/main/java/FindContentChildren.java
@@ -0,0 +1,42 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-07-04
+//-------------------------------------------------------
+
+import java.util.Arrays;
+
+public class FindContentChildren {
+
+ public static int findContentChildren(int[] g, int[] s) {
+ if (g == null || g.length == 0 || s == null || s.length == 0) {
+ return 0;
+ }
+ Arrays.sort(g);
+ Arrays.sort(s);
+
+ int findChildren = 0;
+
+ //初始化操作起始位置
+ int i = 0;
+ int j = 0;
+ while (i < g.length && j < s.length) {
+
+ //当前饼干满足当前小孩
+ if (g[i] <= s[j]) {
+ findChildren = findChildren + 1;
+ i++;
+ j++;
+ } else {
+ //不满足,饼干继续向后找
+ j++;
+ }
+ }
+ return findChildren;
+ }
+
+ public static void main(String[] args) {
+ int[] chs = new int[]{1, 2, 3};
+ int[] foods = new int[]{1, 1};
+ int result = findContentChildren(chs, foods);
+ System.out.println("==>" + result);
+ }
+}
\ No newline at end of file
diff --git a/Week04/src/main/java/LemonadeChange.java b/Week04/src/main/java/LemonadeChange.java
new file mode 100644
index 000000000..441ea0507
--- /dev/null
+++ b/Week04/src/main/java/LemonadeChange.java
@@ -0,0 +1,64 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-07-04
+//-------------------------------------------------------
+
+public class LemonadeChange {
+
+
+ //题目中,只有5,10,20三种币
+ //5元不找零
+ //10元,找一张5块
+ //20元,找10+5;5+5+5
+ public static boolean lemonadeChange(int[] bills) {
+ if (bills == null || bills.length == 0) {
+ return false;
+ }
+
+ int fiveCount = 0;
+
+ int tenCount = 0;
+ for (int i = 0; i < bills.length; i++) {
+ switch (bills[i]) {
+ case 5:
+ //如果是5块,直接收了
+ fiveCount++;
+ break;
+ case 10:
+ //如果是10块,只能找零5块,若没有,则返回失败
+ if (fiveCount == 0) {
+ return false;
+ }
+ //此时手里少一张5块
+ fiveCount--;
+ //多了一张10块
+ tenCount++;
+ break;
+ case 20:
+ //手里有10块,5块,则优先按照最大金额给找;
+ if (fiveCount >= 1 && tenCount >= 1) {
+ fiveCount--;
+ tenCount--;
+ break;
+ }
+ //如果没有10块,则找零3张5块
+ if (fiveCount >= 3) {
+ fiveCount = fiveCount - 3;
+ break;
+ }
+ //都不满足,则失败
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+
+ public static void main(String[] args) {
+ int[] input = new int[]{5, 5, 10};
+
+ boolean result = lemonadeChange(input);
+
+ System.out.println("===>" + result);
+ }
+}
\ No newline at end of file
diff --git a/Week04/src/main/java/MaxProfit.java b/Week04/src/main/java/MaxProfit.java
new file mode 100644
index 000000000..b643de20e
--- /dev/null
+++ b/Week04/src/main/java/MaxProfit.java
@@ -0,0 +1,44 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-07-04
+//-------------------------------------------------------
+
+public class MaxProfit {
+
+ //交易多次
+ public static int maxProfit(int[] prices) {
+ int maxprofit = 0;
+ for (int i = 1; i < prices.length; i++) {
+
+ //多次买卖一直股票,后一天的值大于前一天的,则算赚了
+ if (prices[i] > prices[i - 1]) {
+ maxprofit += prices[i] - prices[i - 1];
+ }
+ }
+ return maxprofit;
+ }
+
+
+ public static void main(String[] args) {
+ int[] input = new int[]{7, 1, 5, 3, 6, 4};
+ int result = maxProfit(input);
+ System.out.println("===>" + result);
+ }
+
+
+ //交易一次,获取最大利润
+/* public static int maxProfit(int[] prices) {
+ int cur = prices[0];
+ int maxProfit = 0;
+
+ for (int i = 1; i < prices.length; i++) {
+ //当前值大于cur,则计算价值
+ if (prices[i] > cur && (prices[i] - cur) > maxProfit) {
+ maxProfit = prices[i] - cur;
+ }
+ if (prices[i] < cur) {
+ cur = prices[i];
+ }
+ }
+ return maxProfit;
+ }*/
+}
\ No newline at end of file
diff --git a/Week06/src/main/java/MinPathSum.java b/Week06/src/main/java/MinPathSum.java
new file mode 100644
index 000000000..65a07208c
--- /dev/null
+++ b/Week06/src/main/java/MinPathSum.java
@@ -0,0 +1,26 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-23
+//-------------------------------------------------------
+
+public class MinPathSum {
+ public int minPathSum(int[][] grid) {
+ if (grid == null || grid.length == 0 || grid[0].length == 0) {
+ return 0;
+ }
+ int rows = grid.length, columns = grid[0].length;
+ int[][] dp = new int[rows][columns];
+ dp[0][0] = grid[0][0];
+ for (int i = 1; i < rows; i++) {
+ dp[i][0] = dp[i - 1][0] + grid[i][0];
+ }
+ for (int j = 1; j < columns; j++) {
+ dp[0][j] = dp[0][j - 1] + grid[0][j];
+ }
+ for (int i = 1; i < rows; i++) {
+ for (int j = 1; j < columns; j++) {
+ dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
+ }
+ }
+ return dp[rows - 1][columns - 1];
+ }
+}
\ No newline at end of file
diff --git a/Week06/src/main/java/NumberDecodings.java b/Week06/src/main/java/NumberDecodings.java
new file mode 100644
index 000000000..b15d5014e
--- /dev/null
+++ b/Week06/src/main/java/NumberDecodings.java
@@ -0,0 +1,26 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-23
+//-------------------------------------------------------
+
+public class NumberDecodings {
+ public int numDecodings(String s) {
+ if (s == null || s.length() == 0) {
+ return 0;
+ }
+ int n = s.length();
+ int[] dp = new int[n + 1];
+ dp[0] = 1;
+ dp[1] = s.charAt(0) != '0' ? 1 : 0;
+ for (int i = 2; i <= n; i++) {
+ int first = Integer.valueOf(s.substring(i - 1, i));
+ int second = Integer.valueOf(s.substring(i - 2, i));
+ if (first >= 1 && first <= 9) {
+ dp[i] += dp[i - 1];
+ }
+ if (second >= 10 && second <= 26) {
+ dp[i] += dp[i - 2];
+ }
+ }
+ return dp[n];
+ }
+}
\ No newline at end of file
diff --git a/Week07/NOTE.md b/Week07/NOTE.md
index 50de30414..35e5040b8 100644
--- a/Week07/NOTE.md
+++ b/Week07/NOTE.md
@@ -1 +1 @@
-学习笔记
\ No newline at end of file
+从动态规划开始,后边的视频还挺难的,得再看一遍~~
\ No newline at end of file
diff --git a/Week07/src/main/java/ClimbStairs.java b/Week07/src/main/java/ClimbStairs.java
new file mode 100644
index 000000000..448b96df1
--- /dev/null
+++ b/Week07/src/main/java/ClimbStairs.java
@@ -0,0 +1,57 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-07-26
+//-------------------------------------------------------
+
+public class ClimbStairs {
+/* //递归,会超时
+ public static int climbStairs(int n) {
+ if (n==1){
+ return 1;
+ }
+ if(n==2){
+ return 2;
+ }
+ return climbStairs(n-1)+climbStairs(n-2);
+ }*/
+
+
+// 将临时计算结果存储,最后直接取数组中记录值
+/* public static int climbStairs(int n) {
+ int[] result = new int[n + 1];
+ return recordClimbStairs(n, result);
+ }
+
+ private static int recordClimbStairs(int n, int[] result) {
+ //已经计算过方法数
+ if (result[n] > 0) {
+ return result[n];
+ }
+
+ if (n == 1) {
+ result[n] = 1;
+ } else if (n == 2) {
+ result[n] = 2;
+ } else {
+ result[n] = recordClimbStairs(n - 1, result) + recordClimbStairs(n - 2, result);
+ }
+ return result[n];
+ }*/
+
+
+ //dp
+ public static int climbStairs(int n) {
+ if (n == 1) {
+ return 1;
+ }
+ if (n == 2) {
+ return 2;
+ }
+ int[] dp = new int[n + 1];
+ dp[1] = 1;
+ dp[2] = 2;
+ for (int i = 3; i <= n; i++) {
+ dp[i] = dp[i - 1] + dp[i - 2];
+ }
+ return dp[n];
+ }
+}
\ No newline at end of file
diff --git a/Week07/src/main/java/NumIslands.java b/Week07/src/main/java/NumIslands.java
new file mode 100644
index 000000000..2397d0637
--- /dev/null
+++ b/Week07/src/main/java/NumIslands.java
@@ -0,0 +1,43 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-07-26
+//-------------------------------------------------------
+
+public class NumIslands {
+ public int numIslands(char[][] grid) {
+ if (grid == null || grid.length == 0) {
+ return 0;
+ }
+ int row = grid.length;
+ int column = grid[0].length;
+ int count = 0;
+
+ for (int r = 0; r < row; r++) {
+ for (int c = 0; c < column; c++) {
+ if (grid[r][c] == '1') {
+ count = count + 1;
+ dfs(grid, r, c);
+ }
+ }
+ }
+ return count;
+ }
+
+ public void dfs(char[][] grid, int r, int c) {
+ int nr = grid.length;
+
+ int nc = grid[0].length;
+
+ if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') {
+ return;
+ }
+ grid[r][c] = '0';
+
+ dfs(grid, r - 1, c);
+
+ dfs(grid, r + 1, c);
+
+ dfs(grid, r, c - 1);
+
+ dfs(grid, r, c + 1);
+ }
+}
\ No newline at end of file
diff --git a/Week07/src/main/java/Trie.java b/Week07/src/main/java/Trie.java
new file mode 100644
index 000000000..9b156870b
--- /dev/null
+++ b/Week07/src/main/java/Trie.java
@@ -0,0 +1,88 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-07-26
+//-------------------------------------------------------
+public class Trie {
+
+ private TrieNode root;
+
+ public Trie() {
+ root = new TrieNode();
+ }
+
+ public void insert(String word) {
+ TrieNode node = root;
+ for (int i = 0; i < word.length(); i++) {
+
+ char currentChar = word.charAt(i);
+
+ if (!node.containsKey(currentChar)) {
+ node.put(currentChar, new TrieNode());
+ }
+
+ node = node.get(currentChar);
+ }
+ node.setEnd();
+ }
+
+ public TrieNode searchPrefix(String word) {
+ TrieNode node = root;
+ for (int i = 0; i < word.length(); i++) {
+
+ char curLetter = word.charAt(i);
+
+ if (node.containsKey(curLetter)) {
+ node = node.get(curLetter);
+ } else {
+ return null;
+ }
+ }
+ return node;
+ }
+
+ public boolean search(String word) {
+ TrieNode node = searchPrefix(word);
+ return node != null && node.isEnd();
+ }
+
+ public boolean startsWith(String prefix) {
+ TrieNode node = searchPrefix(prefix);
+ return node != null;
+ }
+
+
+ //--------------------- Change Logs----------------------
+ // @author ruirui.qu Initial Created at 2020-07-26
+ //-------------------------------------------------------
+ private static class TrieNode {
+
+ private TrieNode[] links;
+
+ private final int R = 26;
+
+ private boolean isEnd;
+
+ public TrieNode() {
+ links = new TrieNode[R];
+ }
+
+ public boolean containsKey(char ch) {
+ return links[ch - 'a'] != null;
+ }
+
+ public TrieNode get(char ch) {
+ return links[ch - 'a'];
+ }
+
+ public void put(char ch, TrieNode node) {
+ links[ch - 'a'] = node;
+ }
+
+ public void setEnd() {
+ isEnd = true;
+ }
+
+ public boolean isEnd() {
+ return isEnd;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Week08/NOTE.md b/Week08/NOTE.md
index 50de30414..d6e260846 100644
--- a/Week08/NOTE.md
+++ b/Week08/NOTE.md
@@ -1 +1,13 @@
-学习笔记
\ No newline at end of file
+学习笔记,用的纸质笔记,貌似没看到上传图片的地方...
+1: 位运算
+ A: 判断奇偶
+ B:清零最低位的1
+ C:得到最低位的1
+
+2:布隆过滤器:判断一个元素是否在集合中;如果其对应的二进制位都是1,则可能存在;如果其对应的二进制位有一个是0,则一定不存在;
+
+3:LRU:最近最少使用,要熟练LRU代码
+
+4:排序算法:
+ A:复杂度,稳定性
+ B:nlongn的代码模板熟记
\ No newline at end of file
diff --git a/Week08/src/main/java/BubbleSort.java b/Week08/src/main/java/BubbleSort.java
new file mode 100644
index 000000000..28830e305
--- /dev/null
+++ b/Week08/src/main/java/BubbleSort.java
@@ -0,0 +1,28 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-03
+//-------------------------------------------------------
+
+public class BubbleSort {
+ //嵌套循环,相邻两个元素,如果逆序,则交换顺序
+ public static void bubble(int[] a, int n) {
+ for (int i = 0; i < n-1; i++) {
+
+ //如果大于,则交换位置, 最终最后一位则是当前数组最大值
+ if (a[i] > a[i+1]){
+ int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp;
+ }
+ }
+ }
+
+ public static void bubbleSort(int[] a, int n) {
+ for (int i = n; i >= 1; i--) {
+ bubble(a, i);
+ }
+ }
+
+ public static void main(String[] args) {
+ int[] input = new int[]{9,6,1,4,8,7,2};
+ bubbleSort(input,input.length);
+ System.out.println("====>"+input);
+ }
+}
\ No newline at end of file
diff --git a/Week08/src/main/java/HammingWeight.java b/Week08/src/main/java/HammingWeight.java
new file mode 100644
index 000000000..46522a50a
--- /dev/null
+++ b/Week08/src/main/java/HammingWeight.java
@@ -0,0 +1,40 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-02
+//-------------------------------------------------------
+
+public class HammingWeight {
+
+ //int 32位,按位&,1与当前位相&,则会得到最低位数字;依次左移循环,便可得到1的个数
+/* public static int hammingWeight(int n) {
+ int count = 0;
+ int mask = 1;
+
+ for (int i = 0; i < 32; i++) {
+ if ((n & mask) != 0) {
+ count++;
+ }
+ mask = mask << 1;
+ }
+
+ return count;
+ }*/
+
+
+ //清零最低位 x= x&(x-1)
+ public static int hammingWeight(int n) {
+ int count = 0;
+
+ while (n != 0) {
+ count++;
+ n = n & (n - 1);
+ }
+ return count;
+ }
+
+
+ public static void main(String[] args) {
+ int input = 00000000000000000000000000001011;
+
+ System.out.println("====>" + hammingWeight(input));
+ }
+}
\ No newline at end of file
diff --git a/Week08/src/main/java/HeapSort.java b/Week08/src/main/java/HeapSort.java
new file mode 100644
index 000000000..9cee8340b
--- /dev/null
+++ b/Week08/src/main/java/HeapSort.java
@@ -0,0 +1,83 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-02
+//-------------------------------------------------------
+
+public class HeapSort {
+
+ //完全二叉树
+ //堆,parent> child
+
+ public static void heapify(int[] tree, int n, int i) {
+
+ //递归终止条件
+ if (i >= n) {
+ return;
+ }
+
+ //左孩子
+ int c1 = 2 * i + 1;
+ //右孩子
+ int c2 = 2 * i + 2;
+
+ int max = i;
+
+ //在父亲节点,左孩子,右孩子节点之间寻找最大值
+ if (c1 < n && tree[c1] > tree[max]) {
+ max = c1;
+ }
+
+ if (c2 < n && tree[c2] > tree[max]) {
+ max = c2;
+ }
+
+
+ //如果当前节点不是最大值,则需要进行节点交换
+ if (max != i) {
+ swap(tree, max, i);
+
+ //轮询进行heapyfy
+ heapify(tree, n, max);
+ }
+ }
+
+ private static void buildHeap(int[] tree, int n) {
+ int lastNode = n - 1;
+ int parent = (lastNode - 1) / 2;
+
+ for (int i = parent; i >= 0; i--) {
+ heapify(tree, n, i);
+ }
+ }
+
+ private static void heapSort(int[] tree, int n) {
+ buildHeap(tree, n);
+
+ for (int i = n - 1; i >= 0; i--) {
+ swap(tree, i, 0);
+ heapify(tree, i, 0);
+ }
+ }
+
+ private static void swap(int[] tree, int max, int i) {
+ int temp = tree[max];
+ tree[max] = tree[i];
+ tree[i] = temp;
+ }
+
+
+ public static void main(String[] args) {
+// int[] array = new int[]{4, 10, 3, 5, 1, 2};
+//
+// heapify(array, 6, 0);
+
+// int[] tree = new int[]{2,5,3,1,10,4};
+// buildHeap(tree, 6);
+// System.out.println("===>" + tree);
+
+ int[] tree = new int[]{2, 5, 3, 1, 10, 4};
+
+ heapSort(tree, 6);
+
+ System.out.println("====>" + tree);
+ }
+}
\ No newline at end of file
diff --git a/Week08/src/main/java/InsertionSort.java b/Week08/src/main/java/InsertionSort.java
new file mode 100644
index 000000000..04212269a
--- /dev/null
+++ b/Week08/src/main/java/InsertionSort.java
@@ -0,0 +1,36 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-03
+//-------------------------------------------------------
+
+public class InsertionSort {
+
+ //将未排好序的数字,插入到已排好的顺序中
+
+ // n :第N个数字
+ public static void insert(int[] a, int n) {
+ int key = a[n];
+
+ int i = n;
+
+ while (a[i - 1] > key) {
+ a[i] = a[i - 1];
+ i--;
+ if (i == 0) {
+ break;
+ }
+ }
+ a[i] = key;
+ }
+
+ public static void insertionSort(int[] a, int n) {
+ for (int i = 1; i < n; i++) {
+ insert(a, i);
+ }
+ }
+
+ public static void main(String[] args) {
+ int[] input = new int[]{3, 6, 7, 4, 2, 1, 5};
+ insertionSort(input, 7);
+ System.out.println("====>" + input);
+ }
+}
\ No newline at end of file
diff --git a/Week08/src/main/java/IsPowerOfTwo.java b/Week08/src/main/java/IsPowerOfTwo.java
new file mode 100644
index 000000000..4d8a78a7c
--- /dev/null
+++ b/Week08/src/main/java/IsPowerOfTwo.java
@@ -0,0 +1,33 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-02
+//-------------------------------------------------------
+
+public class IsPowerOfTwo {
+
+ //轮询/2;
+/* public static boolean isPowerOfTwo(int n) {
+ if (n == 0) {
+ return false;
+ }
+
+ while (n % 2 == 0) {
+ n = n / 2;
+ }
+ return n == 1;
+ }*/
+
+ //2的幂次方只有一个1
+ public static boolean isPowerOfTwo(int n) {
+ if (n == 0) {
+ return false;
+ }
+
+ long x = (long) (n);
+ return (x & (x - 1)) == 0;
+ }
+
+
+ public static void main(String[] args) {
+ System.out.println("====>" + isPowerOfTwo(16));
+ }
+}
\ No newline at end of file
diff --git a/Week08/src/main/java/LRUCache.java b/Week08/src/main/java/LRUCache.java
new file mode 100644
index 000000000..783ebc5a4
--- /dev/null
+++ b/Week08/src/main/java/LRUCache.java
@@ -0,0 +1,87 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-02
+//-------------------------------------------------------
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class LRUCache {
+ private Map @author ruirui.qu Initial Created at 2020-08-03
+//-------------------------------------------------------
+
+public class SelectionSort {
+ //每次选出最大的,和当前排查的数据段的最后一个位置交换
+ public static int findMaxPos(int[] a, int n) {
+ int max = a[0];
+ int maxPos = 0;
+
+ for (int i = 0; i < n; i++) {
+ if (a[i] > max) {
+ max = a[i];
+ maxPos = i;
+ }
+ }
+ return maxPos;
+ }
+
+ public static void selectSort(int[] a, int n) {
+ if (a == null || n <= 1) {
+ return;
+ }
+
+ while(n>1){
+ int maxPos = findMaxPos(a,n);
+ int temp = a[maxPos]; a[maxPos] = a[n-1]; a[n-1] = temp;
+ n--;
+ }
+ }
+
+ public static void main(String[] args) {
+ int[] input = new int[]{9,6,1,4,8,7,2};
+ selectSort(input,7);
+ System.out.println("====>"+input);
+ }
+}
\ No newline at end of file
diff --git a/Week09/NOTE.md b/Week09/NOTE.md
index 50de30414..0a7983564 100644
--- a/Week09/NOTE.md
+++ b/Week09/NOTE.md
@@ -1 +1,3 @@
-学习笔记
\ No newline at end of file
+学习笔记
+
+动态规划还在复习中~
\ No newline at end of file
diff --git a/Week09/src/main/java/FirstUniqChar.java b/Week09/src/main/java/FirstUniqChar.java
new file mode 100644
index 000000000..69e9524bb
--- /dev/null
+++ b/Week09/src/main/java/FirstUniqChar.java
@@ -0,0 +1,32 @@
+import java.util.HashMap;
+import java.util.Map;
+
+public class FirstUniqChar {
+
+ //方法1:循环计数遍历
+ public static int firstUniqChar(String s) {
+ if (s == null || s.length() == 0) {
+ return -1;
+ }
+ HashMap @author ruirui.qu Initial Created at 2020-08-16
+//-------------------------------------------------------
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-16 @author ruirui.qu Initial Created at 2020-08-09
+//-------------------------------------------------------
+
+public class LongestPalindrome {
+
+ //copy官方代码
+ public String longestPalindrome(String s) {
+ if (s == null || s.length() < 1) {
+ return "";
+ }
+ int start = 0, end = 0;
+ for (int i = 0; i < s.length(); i++) {
+ int length1 = isPalindRome(s, i, i);
+
+ int length2 = isPalindRome(s, i, i + 1);
+
+ int len = Math.max(length1, length2);
+ if (len > end - start) {
+ start = i - (len - 1) / 2;
+ end = i + len / 2;
+ }
+ }
+ return s.substring(start, end + 1);
+ }
+
+ private int isPalindRome(String s, int left, int right) {
+ int L = left, R = right;
+ while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
+ L--;
+ R++;
+ }
+ return R - L - 1;
+ }
+}
\ No newline at end of file
diff --git a/Week09/src/main/java/NumDecodings.java b/Week09/src/main/java/NumDecodings.java
new file mode 100644
index 000000000..cfeafd255
--- /dev/null
+++ b/Week09/src/main/java/NumDecodings.java
@@ -0,0 +1,84 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-16
+//-------------------------------------------------------
+
+public class NumDecodings {
+
+ public static int numDecodings(String s) {
+// System.out.println("===>"+s);
+
+ int returnSub1 = check(String.valueOf(s.charAt(0)));
+
+ int returnSub2 = 0;
+
+ if (s.length() >= 2) {
+
+ returnSub2 = check(s.substring(0, 2));
+ }
+
+ int result1 = 0;
+ if (returnSub1 > 0) {
+ if(s.length()==1){
+ result1=1;
+ }else{
+ result1 = numDecodings(s.substring(1, s.length()));
+ }
+ }
+
+
+ int result2 = 0;
+ if (returnSub2 > 0) {
+ if (s.length()==2) {
+ result2 = 1;
+ }else{
+ result2 = numDecodings(s.substring(2, s.length()));
+ }
+ }
+
+// if (result1 == 0 || result2 == 0) {
+// return 0;
+// }
+
+// if (result3 == 0 || result4 == 0) {
+// return 0;
+// }
+
+ return result1 + result2;
+ }
+
+ private static int check(String s) {
+ if (s.charAt(0) == '0') {
+ return 0;
+ }
+ //长度是1位,return 1
+ if (s.length() == 1) {
+ if (s.equals("0")) {
+ return 0;
+ } else {
+ return 1;
+ }
+ }
+ //长度大于2位,一个字母只能占1位;或者2位
+ if (s.length() == 2) {
+ Integer value = Integer.parseInt(s);
+ if (value > 26) {
+ return 0;
+ } else if (s.equals("00")) {
+ return 0;
+ } else if (s.charAt(s.length() - 1) == '0') {
+ return 1;
+
+ } else {
+ return 1;
+ }
+ }
+ return 0;
+ }
+
+
+ public static void main(String[] args) {
+ String s = "226";
+ int result = numDecodings(s);
+ System.out.println("===>" + result);
+ }
+}
\ No newline at end of file
diff --git a/Week09/src/main/java/ReverseWord.java b/Week09/src/main/java/ReverseWord.java
new file mode 100644
index 000000000..b630e2ee3
--- /dev/null
+++ b/Week09/src/main/java/ReverseWord.java
@@ -0,0 +1,78 @@
+//--------------------- Change Logs----------------------
+// @author ruirui.qu Initial Created at 2020-08-09
+//-------------------------------------------------------
+
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Deque;
+import java.util.Iterator;
+import java.util.List;
+
+public class ReverseWord {
+
+ //1:调用现成的api直接操作reverse
+ public static String reverseWords1(String s) {
+
+ // 除去开头和末尾的空白字符
+ s = s.trim();
+
+ // 正则匹配连续的空白字符作为分隔符分割
+ List> groupAnagrams(String[] strs) {
+ if (strs.length == 0) {
+ return new ArrayList();
+ }
+ Map