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 pairs = new HashMap() {{ + put(')', '('); + put(']', '['); + put('}', '{'); + }}; + LinkedList stack = new LinkedList(); + for (int i = 0; i < n; i++) { + char ch = s.charAt(i); + if (pairs.containsKey(ch)) { + if (stack.isEmpty() || stack.peek() != pairs.get(ch)) { + return false; + } + stack.pop(); + } else { + stack.push(ch); + } + } + return stack.isEmpty(); + } + + public static boolean isValid(String s) { + Stack stack = new Stack(); + for (char c : s.toCharArray()) { + if (c == '(') { + stack.push(')'); + } else if (c == '{') { + stack.push('}'); + } else if (c == '[') { + stack.push(']'); + } else if (stack.isEmpty() || stack.pop() != c) { + return false; + } + } + return stack.isEmpty(); + } + + public static void main(String[] args) { + String ss = "()"; + System.out.println("====>" + isValid(ss)); + } +} \ No newline at end of file diff --git a/Week01/src/main/java/MergeTwoArrays.java b/Week01/src/main/java/MergeTwoArrays.java new file mode 100644 index 000000000..ea4f5f7de --- /dev/null +++ b/Week01/src/main/java/MergeTwoArrays.java @@ -0,0 +1,50 @@ +//--------------------- Change Logs---------------------- +//

@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 dataStack; + private Stack minStack; + + public MinStack() { + dataStack = new Stack(); + minStack = new Stack(); + } + + public void push(int x) { + dataStack.push(x); + if (minStack.isEmpty() || x < minStack.peek()) { + minStack.push(x); + } + } + + public void pop() { + int x = dataStack.pop(); + if (x == minStack.peek()) { + minStack.pop(); + } + } + + public int top(){ + return dataStack.peek(); + } + + public int getMin(){ + return minStack.peek(); + } +} \ No newline at end of file diff --git a/Week01/src/main/java/MinStack1.java b/Week01/src/main/java/MinStack1.java new file mode 100644 index 000000000..288e5788b --- /dev/null +++ b/Week01/src/main/java/MinStack1.java @@ -0,0 +1,42 @@ +import java.util.Stack; + +//--------------------- Change Logs---------------------- +//

@author ruirui.qu Initial Created at 2020-08-23

+//------------------------------------------------------- +public class MinStack1 { + private Stack stack; + + public MinStack1() { + this.stack = new Stack(); + } + + public void push(int x) { + if (stack.isEmpty()) { + stack.push(new Node(x, x)); + } else { + stack.push(new Node(x, Math.min(x, stack.peek().min))); + } + } + + public void pop() { + stack.pop(); + } + + public int top() { + return stack.peek().val; + } + + public int getMin() { + return stack.peek().min; + } + + private static class Node { + int val; + int min; + + public Node(int val, int min) { + this.val = val; + this.min = min; + } + } +} \ No newline at end of file diff --git a/Week01/src/main/java/MinStack2.java b/Week01/src/main/java/MinStack2.java new file mode 100644 index 000000000..5540bdefe --- /dev/null +++ b/Week01/src/main/java/MinStack2.java @@ -0,0 +1,44 @@ +//--------------------- Change Logs---------------------- +//

@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 " + input); + } +} \ No newline at end of file diff --git a/Week01/src/main/java/Trap.java b/Week01/src/main/java/Trap.java new file mode 100644 index 000000000..3ca20582e --- /dev/null +++ b/Week01/src/main/java/Trap.java @@ -0,0 +1,27 @@ +//--------------------- Change Logs---------------------- +//

@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 list = new ArrayList(nums.length); + for (int i = 0; i < nums.length; i++) { + list.add(new Node(nums[i], i)); + } + + Collections.sort(list, new Comparator() { + @Override + public int compare(Node o1, Node o2) { + return o1.value - o2.value; + } + }); + int i = 0; + int j = list.size() - 1; + while (i != j) { + if (list.get(i).value + list.get(j).value == target) { + return new int[]{list.get(i).index, list.get(j).index}; + } else if (list.get(i).value + list.get(j).value < target) { + i++; + } else { + j--; + } + } + return null; + } + + private static class Node { + private int value; + + private int index; + + public Node(int value, int index) { + this.value = value; + this.index = index; + } + } + + public static void main(String[] args) { + int[] nums = new int[]{2, 7, 11, 15}; + int target = 9; + + int[] result = twoSum(nums, target); + System.out.println("===>"+result); + } +} \ No newline at end of file diff --git a/Week02/src/main/java/GroupAnagrams.java b/Week02/src/main/java/GroupAnagrams.java new file mode 100644 index 000000000..be6f045c7 --- /dev/null +++ b/Week02/src/main/java/GroupAnagrams.java @@ -0,0 +1,30 @@ +//--------------------- Change Logs---------------------- +//

@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> groupAnagrams(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()); + } + +} \ No newline at end of file diff --git a/Week02/src/main/java/IsAnagram.java b/Week02/src/main/java/IsAnagram.java new file mode 100644 index 000000000..4beebde5f --- /dev/null +++ b/Week02/src/main/java/IsAnagram.java @@ -0,0 +1,46 @@ +//--------------------- Change Logs---------------------- +//

@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 indexMap; + + public TreeNode myBuildTree(int[] preorder, int[] inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right) { + if (preorder_left > preorder_right) { + return null; + } + + // 前序遍历中的第一个节点就是根节点 + int preorder_root = preorder_left; + // 在中序遍历中定位根节点 + int inorder_root = indexMap.get(preorder[preorder_root]); + + // 先把根节点建立出来 + TreeNode root = new TreeNode(preorder[preorder_root]); + // 得到左子树中的节点数目 + int size_left_subtree = inorder_root - inorder_left; + // 递归地构造左子树,并连接到根节点 + // 先序遍历中「从 左边界+1 开始的 size_left_subtree」个元素就对应了中序遍历中「从 左边界 开始到 根节点定位-1」的元素 + root.left = myBuildTree(preorder, inorder, preorder_left + 1, preorder_left + size_left_subtree, inorder_left, inorder_root - 1); + // 递归地构造右子树,并连接到根节点 + // 先序遍历中「从 左边界+1+左子树节点数目 开始到 右边界」的元素就对应了中序遍历中「从 根节点定位+1 到 右边界」的元素 + root.right = myBuildTree(preorder, inorder, preorder_left + size_left_subtree + 1, preorder_right, inorder_root + 1, inorder_right); + return root; + } + + public TreeNode buildTree(int[] preorder, int[] inorder) { + int n = preorder.length; + // 构造哈希映射,帮助我们快速定位根节点 + indexMap = new HashMap(); + for (int i = 0; i < n; i++) { + indexMap.put(inorder[i], i); + } + return myBuildTree(preorder, inorder, 0, n - 1, 0, n - 1); + } +} \ No newline at end of file diff --git a/Week03/src/main/java/LowestCommonAncestor.java b/Week03/src/main/java/LowestCommonAncestor.java new file mode 100644 index 000000000..ad4518a82 --- /dev/null +++ b/Week03/src/main/java/LowestCommonAncestor.java @@ -0,0 +1,38 @@ +//--------------------- Change Logs---------------------- +//

@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 cache = new HashMap(); + private int size; + private int capacity; + private LinkedNode head, tail; + + public LRUCache(int capacity) { + this.size = 0; + this.capacity = capacity; + head = new LinkedNode(); + tail = new LinkedNode(); + head.next = tail; + tail.prev = head; + } + + public int get(int key) { + LinkedNode node = cache.get(key); + if (node == null) { + return -1; + } + moveToHead(node); + return node.value; + } + + public void put(int key, int value) { + LinkedNode node = cache.get(key); + if (node == null) { + LinkedNode newNode = new LinkedNode(); + newNode.key = key; + newNode.value = value; + cache.put(key, newNode); + addNode(newNode); + ++size; + if (size > capacity) { + LinkedNode tail = popTail(); + cache.remove(tail.key); + --size; + } + } else { + node.value = value; + moveToHead(node); + } + } + + //==============================private linkedNode field================================= + class LinkedNode { + int key; + int value; + LinkedNode prev; + LinkedNode next; + } + + //新节点总放在head后边 + private void addNode(LinkedNode node) { + node.prev = head; + node.next = head.next; + head.next.prev = node; + head.next = node; + } + + //删除一个存在的节点 + private void removeNode(LinkedNode node) { + LinkedNode prev = node.prev; + LinkedNode next = node.next; + prev.next = next; + next.prev = prev; + } + + //移动到头节点 + private void moveToHead(LinkedNode node) { + removeNode(node); + addNode(node); + } + + private LinkedNode popTail() { + LinkedNode res = tail.prev; + removeNode(res); + return res; + } +} \ No newline at end of file diff --git a/Week08/src/main/java/SelectionSort.java b/Week08/src/main/java/SelectionSort.java new file mode 100644 index 000000000..e8c814515 --- /dev/null +++ b/Week08/src/main/java/SelectionSort.java @@ -0,0 +1,37 @@ +//--------------------- Change Logs---------------------- +//

@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 count = new HashMap(); + int n = s.length(); + for (int i = 0; i < n; i++) { + char c = s.charAt(i); + count.put(c, count.get(c) == null ? 1 : count.get(c) + 1); + } + + for (int i = 0; i < n; i++) { + if (count.get(s.charAt(i)) == 1) { + return i; + } + } + return -1; + + } + + //方法2: 如果都是小写字母,则最多26个英文字母;当字符串特别长的时候,可以优化下 + + public static void main(String[] args) { + System.out.println("===>"+firstUniqChar("leetcode")); + } +} \ No newline at end of file diff --git a/Week09/src/main/java/LadderLength.java b/Week09/src/main/java/LadderLength.java new file mode 100644 index 000000000..8f8cf65c2 --- /dev/null +++ b/Week09/src/main/java/LadderLength.java @@ -0,0 +1,80 @@ +//--------------------- Change Logs---------------------- +//

@author ruirui.qu Initial Created at 2020-08-16

+//------------------------------------------------------- +//--------------------- Change Logs---------------------- +//

@author ruirui.qu Initial Created at 2020-08-16

+//------------------------------------------------------- + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LadderLength { + public int ladderLength(String beginWord, String endWord, List wordList) { + //注意此处把首个单词放到了list的最后面,所以start才会是N-1。别搞错了。 + wordList.add(beginWord); + int N = wordList.size(); + int start = N - 1; + int end = 0; + while (end < N && !wordList.get(end).equals(endWord)) { + end++; + } + if (end == N) { + return 0; + } + List[] graphic = buildGraphic(wordList); + return getShortestPath(graphic, start, end); + } + + private List[] buildGraphic(List wordList) { + int N = wordList.size(); + List[] graphic = new List[N]; + for (int i = 0; i < N; i++) { + graphic[i] = new ArrayList(); + for (int j = 0; j < N; j++) { + if (isConnect(wordList.get(i), wordList.get(j))) { + graphic[i].add(j); + } + } + } + return graphic; + } + + + private boolean isConnect(String s1, String s2) { + int diffCnt = 0; + for (int i = 0; i < s1.length() && diffCnt <= 1; i++) { + if (s1.charAt(i) != s2.charAt(i)) { + diffCnt++; + } + } + return diffCnt == 1; + } + + private int getShortestPath(List[] graphic, int start, int end) { + Queue queue = new LinkedList(); + boolean[] marked = new boolean[graphic.length]; + queue.add(start); + marked[start] = true; + int path = 1; + while (!queue.isEmpty()) { + int size = queue.size(); + path++; + while (size-- > 0) { + int cur = queue.poll(); + for (int next : graphic[cur]) { + if (next == end) { + return path; + } + if (marked[next]) { + continue; + } + marked[next] = true; + queue.add(next); + } + } + } + return 0; + } +} \ No newline at end of file diff --git a/Week09/src/main/java/LongestPalindrome.java b/Week09/src/main/java/LongestPalindrome.java new file mode 100644 index 000000000..28c3b982d --- /dev/null +++ b/Week09/src/main/java/LongestPalindrome.java @@ -0,0 +1,35 @@ +//--------------------- Change Logs---------------------- +//

@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 wordList = Arrays.asList(s.split("\\s+")); + Collections.reverse(wordList); + + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 0; i < wordList.size(); i++) { + if (i == (wordList.size() - 1)) { + stringBuffer.append(wordList.get(i)); + } else { + stringBuffer.append(wordList.get(i)).append(" "); + } + } + return stringBuffer.toString(); + } + + //双端队列 + public static String reverseWords(String s) { + + int left = 0, right = s.length() - 1; + // 去掉字符串开头的空白字符 + while (left <= right && s.charAt(left) == ' ') ++left; + + // 去掉字符串末尾的空白字符 + while (left <= right && s.charAt(right) == ' ') --right; + + Deque d = new ArrayDeque(); + StringBuilder word = new StringBuilder(); + + while (left <= right) { + char c = s.charAt(left); + if ((word.length() != 0) && (c == ' ')) { + // 将单词 push 到队列的头部 + d.offerFirst(word.toString()); + word.setLength(0); + } else if (c != ' ') { + word.append(c); + } + ++left; + } + d.offerFirst(word.toString()); + + StringBuffer stringBuffer = new StringBuffer(); + + + Iterator iterator = d.iterator(); + while(iterator.hasNext()){ + String element = (String) iterator.next(); + stringBuffer.append(element).append(" "); + } + return stringBuffer.toString().substring(0,stringBuffer.length()-1); + } + + + public static void main(String[] args) { + String aa = "the sky is blue"; + String result = reverseWords(aa); + System.out.println("===>" + result); + } +} \ No newline at end of file diff --git a/summary.md b/summary.md new file mode 100644 index 000000000..11cb5e9e1 --- /dev/null +++ b/summary.md @@ -0,0 +1,28 @@ +毕业总结: +~~~~ +初衷: +最初选择这门课程,目的是想提高下算法,自己学习刷题的话,又不具有系统性;正好碰见这个课程,抱着试试看的态度选择了这门课程。 +~~~~ + +~~~~ +过程: +跟着超哥的节奏,一个知识点一个知识点的过,中间由于工作原因,请了两周假;刚开始觉得课程还行;后边到动态规划之类的,开始觉得有点难; +自己又木有花太多的时间去细究,导致这块感觉还是属于薄弱区; +~~~~ + +~~~~ +笔记: +网络笔记较少,基本都是纸质随身带笔记;就不拍照贴图了; +~~~~ + +~~~~ +结果: +1:这门课程初步学习完成,没有达到最初预期的效果;但是对数据结构和算法,有了个整体的脑图,接下来的时间好好再复习下; +2:超哥的五毒神掌学习方法很重要;按照这个节奏,改变了之前的学习习惯;希望未来能保持; + +~~~~ + +~~~~ +未来: +课程结束,训练营的视频,还能看段时间,这段时间把超哥的视频再刷几遍,homework争取滚瓜烂熟 +~~~~ \ No newline at end of file