diff --git a/Week01/DesignCircularDeque.java b/Week01/DesignCircularDeque.java new file mode 100644 index 000000000..e2ab58d5f --- /dev/null +++ b/Week01/DesignCircularDeque.java @@ -0,0 +1,90 @@ +class MyCircularDeque { + + public int[] arr; + private int front; + private int rear; + private int capacity; + + + + /** Initialize your data structure here. Set the size of the deque to be k. */ + public MyCircularDeque(int k) { + capacity = k +1; + arr = new int[capacity]; + front = 0; + rear = 0; + } + + /** Adds an item at the front of Deque. Return true if the operation is successful. */ + public boolean insertFront(int value) { + if (isFull()) return false; + + front = (front -1 + capacity)% capacity; + arr[front] = value; + return true; + + } + + /** Adds an item at the rear of Deque. Return true if the operation is successful. */ + public boolean insertLast(int value) { + if (isFull()) return false; + arr[rear] = value; + rear = (rear + 1 ) % capacity; + + return true; + } + + /** Deletes an item from the front of Deque. Return true if the operation is successful. */ + public boolean deleteFront() { + if (isEmpty()) return false; + front = (front +1 ) % capacity; + return true; + } + + /** Deletes an item from the rear of Deque. Return true if the operation is successful. */ + public boolean deleteLast() { + if (isEmpty()) return false; + + rear = (rear -1 + capacity)% capacity; + return true; + } + + /** Get the front item from the deque. */ + public int getFront() { + if (isEmpty()){ + return -1; + } + return arr[front]; + } + + /** Get the last item from the deque. */ + public int getRear() { + if (isEmpty()){ + return -1; + } + return arr[(rear - 1 + capacity) % capacity]; + } + + /** Checks whether the circular deque is empty or not. */ + public boolean isEmpty() { + return front == rear; + } + + /** Checks whether the circular deque is full or not. */ + public boolean isFull() { + return (rear +1 ) % arr.length == front ; + } +} + +/** + * Your MyCircularDeque object will be instantiated and called as such: + * MyCircularDeque obj = new MyCircularDeque(k); + * boolean param_1 = obj.insertFront(value); + * boolean param_2 = obj.insertLast(value); + * boolean param_3 = obj.deleteFront(); + * boolean param_4 = obj.deleteLast(); + * int param_5 = obj.getFront(); + * int param_6 = obj.getRear(); + * boolean param_7 = obj.isEmpty(); + * boolean param_8 = obj.isFull(); + */ diff --git a/Week01/MergeSortedArray.java b/Week01/MergeSortedArray.java new file mode 100644 index 000000000..21e5779bf --- /dev/null +++ b/Week01/MergeSortedArray.java @@ -0,0 +1,19 @@ +class MergeSortedArray { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int i = m - 1 ; + int j = n - 1; + int totalSize= m + n; + for(int k = totalSize-1; k >= 0; k--){ + if(j <0){ + return; + } + if ( i >= 0 && nums1[i] > nums2[j] ) { + nums1[k]= nums1[i]; + i--; + }else { + nums1[k] = nums2[j]; + j--; + } + } + } +} diff --git a/Week01/MergeTwoSorted.java b/Week01/MergeTwoSorted.java new file mode 100644 index 000000000..b2e516c35 --- /dev/null +++ b/Week01/MergeTwoSorted.java @@ -0,0 +1,41 @@ +/** + * 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; } + * } + */ +class MergeTowSortedList { + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + ListNode newHead = new ListNode(); + ListNode pNode = newHead; + + + while(l1 != null && l2 != null){ + if(l1.val <= l2.val){ + pNode.next = l1; + l1 = l1.next; + }else{ + pNode.next = l2; + l2 = l2.next; + } + pNode = pNode.next; + + + } + if(pNode == null){ + return l1 !=null ? l1 :l2; + } + if (l1 != null ) { + pNode.next = l1; + } + if (l2 != null) { + pNode.next = l2; + } + return newHead.next; + + } +} diff --git a/Week01/MoveZeroes.java b/Week01/MoveZeroes.java new file mode 100644 index 000000000..557488c6c --- /dev/null +++ b/Week01/MoveZeroes.java @@ -0,0 +1,17 @@ +class MoveZeroes { + public void moveZeroes(int[] nums) { + int j =0 ; + for (int i = 0; i < nums.length; i++) { + if (nums[i]!= 0 ){ + // 当i==j 时无需交换 + if(i!=j){ + int tmp = nums[i]; + nums[i] = nums[j]; + nums[j++] = tmp; + }else{ + j++; + } + } + } + } +} diff --git a/Week01/PlusOne.java b/Week01/PlusOne.java new file mode 100644 index 000000000..79c398918 --- /dev/null +++ b/Week01/PlusOne.java @@ -0,0 +1,16 @@ +class PlusOne { + public int[] plusOne(int[] digits) { + int size = digits.length; + for(int n = size -1; n >= 0; n--){ + digits[n]++; + digits[n] = digits[n] % 10; + if (digits[n] != 0 ){ + return digits; + } + } + digits = new int[size+1]; + digits[0] = 1; + return digits ; + + } +} diff --git a/Week01/RemoveDuplicatesFromSortedArray.java b/Week01/RemoveDuplicatesFromSortedArray.java new file mode 100644 index 000000000..7bb911a9a --- /dev/null +++ b/Week01/RemoveDuplicatesFromSortedArray.java @@ -0,0 +1,15 @@ +class RemoveDuplicatesFromSortedArray { + + public int removeDuplicates(int[] nums) { + + if (nums.length == 0) return 0; + int i = 0 ; + for(int j = 1; j< nums.length; j++){ + if(nums[j] !=nums[i]){ + i++; + nums[i] = nums[j]; + } + } + return i + 1; + } +} diff --git a/Week01/RotateArray.java b/Week01/RotateArray.java new file mode 100644 index 000000000..570d6038a --- /dev/null +++ b/Week01/RotateArray.java @@ -0,0 +1,16 @@ +class RotateArray { + public void rotate(int[] nums, int k) { + int n = nums.length; + k = k % n; + reverse(nums, 0, n - 1); + reverse(nums, 0, k - 1); + reverse(nums, k, n - 1); + } + public void reverse(int[] nums, int start, int end) { + while(start cache = new HashMap<>(); + int[] res = new int[2]; + for(int i = 0; i < nums.length; i++) { + int secondValue = target - nums[i]; + Integer secondIndex = cache.get(secondValue); + if (secondIndex != null && secondIndex >= 0 && secondIndex != i ){ + if (secondIndex > i ){ + res[0] = i; + res[1] = secondIndex; + }else{ + res[0] = secondIndex; + res[1] = i; + } + + return res; + } + cache.put(nums[i], i); + } + return res; + } +} diff --git a/Week01/TrappingRainWater.java b/Week01/TrappingRainWater.java new file mode 100644 index 000000000..372922bf6 --- /dev/null +++ b/Week01/TrappingRainWater.java @@ -0,0 +1,24 @@ +class TrappingRainWater { + public int trap(int[] height) { + int sum = 0; + Stack stack = new Stack<>(); + int current = 0; + while(current < height.length){ + // 大于开始处理 + while(!stack.isEmpty() && height[current] > height[stack.peek()] ){ + int h = height[stack.peek()]; + stack.pop(); + if(stack.isEmpty()){ + break; + } + int d = current - stack.peek() -1; + + int min = Math.min(height[stack.peek()], height[current]); + sum = sum + d *(min - h); + } + stack.push(current); + current ++; + } + return sum; + } +} diff --git a/Week02/BinaryTreeInorderTraversal.java b/Week02/BinaryTreeInorderTraversal.java new file mode 100644 index 000000000..2369a20bb --- /dev/null +++ b/Week02/BinaryTreeInorderTraversal.java @@ -0,0 +1,24 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class BinaryTreeInorderTraversal { + public List inorderTraversal(TreeNode root) { + List res = new ArrayList<>(); + if(root == null){ + return res; + } + + List leftResList = inorderTraversal(root.left); + List rightResList = inorderTraversal(root.right); + res.addAll(leftResList); + res.add(root.val); + res.addAll(rightResList); + return res; + } +} diff --git a/Week02/BinaryTreePreorderTraversal.java b/Week02/BinaryTreePreorderTraversal.java new file mode 100644 index 000000000..6d054dd0b --- /dev/null +++ b/Week02/BinaryTreePreorderTraversal.java @@ -0,0 +1,24 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public List preorderTraversal(TreeNode root) { + List res = new ArrayList<>(); + if(root == null){ + return res; + } + + List leftResList = preorderTraversal(root.left); + List rightResList = preorderTraversal(root.right); + res.add(root.val); + res.addAll(leftResList); + res.addAll(rightResList); + return res; + } +} diff --git a/Week02/GroupAnagrams.java b/Week02/GroupAnagrams.java new file mode 100644 index 000000000..e914b009c --- /dev/null +++ b/Week02/GroupAnagrams.java @@ -0,0 +1,25 @@ +class GroupAnagrams { + public List> groupAnagrams(String[] strs) { + int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, + 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, + 73, 79, 83, 89, 97, 101}; + + HashMap> map = new HashMap<>(); + for(String str: strs){ + int hashValue = 1; + char[] chars = str.toCharArray(); + for(char c: chars){ + hashValue *= primes[c - 'a']; + } + if (map.containsKey(hashValue)){ + map.get(hashValue).add(str); + }else{ + List newStrList = new ArrayList<>(); + newStrList.add(str); + map.put(hashValue, newStrList); + } + } + return new ArrayList<>( map.values()); + + } +} diff --git a/Week02/NAryTreeLevelOrderTraversal.java b/Week02/NAryTreeLevelOrderTraversal.java new file mode 100644 index 000000000..053cd3b11 --- /dev/null +++ b/Week02/NAryTreeLevelOrderTraversal.java @@ -0,0 +1,44 @@ +/* +// 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; + } +}; +*/ + +class NAryTreeLevelOrderTraversal { + public List> levelOrder(Node root) { + List> res = new ArrayList<>(); + if (root == null){ + return res; + } + + withDepth(root,0, res); + return res; + } + + public void withDepth(Node root, int depth, List> res){ + if(depth+ 1 >res.size()){ + List levelList = new ArrayList<>(); + res.add(levelList); + } + + res.get(depth).add(root.val); + for(Node node: root.children){ + withDepth(node, depth + 1, res); + } + + + } +} diff --git a/Week02/NAryTreePreorderTraversal.java b/Week02/NAryTreePreorderTraversal.java new file mode 100644 index 000000000..3ebafae99 --- /dev/null +++ b/Week02/NAryTreePreorderTraversal.java @@ -0,0 +1,40 @@ + +// 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; + } +}; +*/ + + +class NAryTreePreorderTraversal { + List res = new ArrayList<>(); + + public List preorder(Node root) { + + if(root != null){ + res.add(root.val); + }else{ + return res; + } + List children = root.children; + + if(children.size() > 0){ + for (Node n: children){ + preorder(n); + } + } + return res; + } +} diff --git a/Week02/TopkFrequentElements.java b/Week02/TopkFrequentElements.java new file mode 100644 index 000000000..f7c8860a6 --- /dev/null +++ b/Week02/TopkFrequentElements.java @@ -0,0 +1,25 @@ +class TopkFrequentElements { + public int[] topKFrequent(int[] nums, int k) { + + HashMap frequencyMap = new HashMap<>(); + for(int i =0 ;i < nums.length; i++){ + frequencyMap.put(nums[i],frequencyMap.getOrDefault(nums[i],0)+1); + } + + + Queue heap = new PriorityQueue<>( + ((o1, o2) -> frequencyMap.get(o1) -frequencyMap.get(o2)) + ); + + for(int num : frequencyMap.keySet()){ + heap.add(num); + if(heap.size() > k) heap.poll(); + } + int [] result = new int[heap.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = heap.poll(); + } + return result; + + } +} diff --git a/Week02/TwoSum.java b/Week02/TwoSum.java new file mode 100644 index 000000000..60e712ddc --- /dev/null +++ b/Week02/TwoSum.java @@ -0,0 +1,23 @@ +class TwoSum { + public int[] twoSum(int[] nums, int target) { + HashMap cache = new HashMap<>(); + int[] res = new int[2]; + for(int i = 0; i < nums.length; i++) { + int secondValue = target - nums[i]; + Integer secondIndex = cache.get(secondValue); + if (secondIndex != null && secondIndex >= 0 && secondIndex != i ){ + if (secondIndex > i ){ + res[0] = i; + res[1] = secondIndex; + }else{ + res[0] = secondIndex; + res[1] = i; + } + + return res; + } + cache.put(nums[i], i); + } + return res; + } +} diff --git a/Week02/ValidAnagram.java b/Week02/ValidAnagram.java new file mode 100644 index 000000000..f71439d9a --- /dev/null +++ b/Week02/ValidAnagram.java @@ -0,0 +1,19 @@ +class ValidAnagram { + public boolean isAnagram(String s, String t) { + if(s.length() != t.length()){ + return false; + } + int[] map = new int[26]; + for(int i =0; i < s.length(); i++){ + map[s.charAt(i) -'a'] += 1; + map[t.charAt(i) -'a'] -= 1; + } + for(int v : map){ + if(v!= 0){ + return false; + } + } + + return true; + } +} diff --git a/Week04/AssignCookies.java b/Week04/AssignCookies.java new file mode 100644 index 000000000..f2489960b --- /dev/null +++ b/Week04/AssignCookies.java @@ -0,0 +1,22 @@ +class Solution { + public int findContentChildren(int[] g, int[] s) { + int content = 0; + if(g.length <=0 || s.length <=0){ + return 0 ; + } + Arrays.sort(g); + Arrays.sort(s); + int i = 0 ; + int j = 0; + for( ; j < s.length && i< g.length; ){ + if(s[j]>= g[i]){ + content ++; + i++; + j++; + }else{ + j++; + } + } + return content; + } +} diff --git a/Week04/BestTimeToBuyAndSellStockTwo.java b/Week04/BestTimeToBuyAndSellStockTwo.java new file mode 100644 index 000000000..865461d78 --- /dev/null +++ b/Week04/BestTimeToBuyAndSellStockTwo.java @@ -0,0 +1,11 @@ +class Solution { + public int maxProfit(int[] prices) { + int win = 0; + for(int i = 1 ; i< prices.length; i++){ + if(prices[i-1]< prices[i]){ + win = win + prices[i] - prices[i-1]; + } + } + return win; + } +} diff --git a/Week04/LemonadeChange.java b/Week04/LemonadeChange.java new file mode 100644 index 000000000..a23985a0e --- /dev/null +++ b/Week04/LemonadeChange.java @@ -0,0 +1,26 @@ +class Solution { + public boolean lemonadeChange(int[] bills) { + int five = 0; + int ten = 0; + int twenty = 0; + for(int bill: bills){ + switch(bill){ + case 5: + five++; break; + case 10: + five --; ten ++; break; + case 20: + if(ten > 0){ + ten --; + five --; + }else{ + five = five -3; + } + break; + + } + if(five < 0){return false;} + } + return true; + } +} diff --git a/Week06/DecodeWays.java b/Week06/DecodeWays.java new file mode 100644 index 000000000..7faa8630d --- /dev/null +++ b/Week06/DecodeWays.java @@ -0,0 +1,20 @@ +class Solution { + public int numDecodings(String s) { + + int n = s.length(); + if(n== 0 || s.charAt(0) =='0') return 0; + int [] dp = new int[n+1]; + dp[0] = 1; + dp[1] = 1; + + for(int i = 2 ; i< n+1 ; i++){ + if(s.charAt(i-2) == '1' || s.charAt(i-2) == '2' && s.charAt(i-1) <= '6' ){ + dp[i] += dp[i-2]; + } + if (s.charAt(i - 1) != '0'){ + dp[i] += dp[i-1]; + } + } + return dp[n]; + } +} diff --git a/Week06/MaximalSquare.java b/Week06/MaximalSquare.java new file mode 100644 index 000000000..fd4da76aa --- /dev/null +++ b/Week06/MaximalSquare.java @@ -0,0 +1,25 @@ +class Solution { + public int maximalSquare(char[][] matrix) { + + int maxSide = 0; + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return maxSide; + } + int m = matrix.length; + int n = matrix[0].length; + int [][] dp = new int [m][n]; + for(int i = 0; i < m; i++){ + for(int j = 0; j< n; j++){ + if(matrix[i][j] == '1' ){ + if(i==0 || j== 0) { + dp[i][j]=1; + }else { + dp[i][j] = Math.min(Math.min(dp[i-1][j-1], dp[i-1][j]), dp[i][j-1]) +1 ; + } + maxSide = Math.max(maxSide, dp[i][j]); + } + } + } + return maxSide * maxSide; + } +} diff --git a/Week06/MinimumPathSum.java b/Week06/MinimumPathSum.java new file mode 100644 index 000000000..5f84cbeb9 --- /dev/null +++ b/Week06/MinimumPathSum.java @@ -0,0 +1,20 @@ +class Solution { + public int minPathSum(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + for(int i =0; i< m; i++ ){ + for(int j = 0; j < n; j++){ + if(i == 0 && j == 0) continue; + else if(i == 0 ){ + grid[i][j] = grid[i][j-1] + grid[i][j]; + }else if(j==0){ + grid[i][j] = grid[i-1][j] + grid[i][j] ; + }else{ + grid[i][j] = Math.min(grid[i][j-1], grid[i-1][j]) + grid[i][j]; + } + } + } + return grid[m-1][n-1]; + + } +}