diff --git a/Week_02/id_33/Leetcode_111_33.java b/Week_02/id_33/Leetcode_111_33.java new file mode 100644 index 00000000..4b30b8ba --- /dev/null +++ b/Week_02/id_33/Leetcode_111_33.java @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode id=111 lang=java + * + * [111] Minimum Depth of Binary Tree + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public int minDepth(TreeNode root) { + if(root == null) + return 0; + if(root.left == null && root.right == null) + return 1; + if(root.left == null) + return minDepth(root.right) + 1; + if(root.right == null) + return minDepth(root.left) + 1; + return Math.min(minDepth(root.left), minDepth(root.right)) + 1; + } +} + diff --git a/Week_02/id_33/Leetcode_1_33.java b/Week_02/id_33/Leetcode_1_33.java new file mode 100644 index 00000000..31db9c58 --- /dev/null +++ b/Week_02/id_33/Leetcode_1_33.java @@ -0,0 +1,21 @@ +/* + * @lc app=leetcode id=1 lang=java + * + * [1] Two Sum + */ +class Solution { + public int[] twoSum(int[] nums, int target) { + Map map = new HashMap<>(); + if (nums != null && nums.length > 0) { + for (int i = 0; i < nums.length; i++) { + int result = target - nums[i]; + if (map.containsKey(result)) { + return new int[]{map.get(result), i}; + } + map.put(nums[i], i); + } + } + return new int[]{}; + } +} + diff --git a/Week_02/id_33/Leetcode_3_33.java b/Week_02/id_33/Leetcode_3_33.java new file mode 100644 index 00000000..259adc27 --- /dev/null +++ b/Week_02/id_33/Leetcode_3_33.java @@ -0,0 +1,23 @@ + + +/* + * @lc app=leetcode id=3 lang=java + * + * [3] Longest Substring Without Repeating Characters + */ +class Solution { + public int lengthOfLongestSubstring(String s) { + int maxStr = 0; + Map map = new HashMap<>(); + int temp = 0; + for(int i = 0;i < s.length(); i++){ + if(map.containsKey(s.charAt(i))){ + temp = Math.max(map.get(s.charAt(i)), temp); + } + maxStr = Math.max(maxStr, i-temp+1); + map.put(s.charAt(i),i+1); + } + return maxStr; + } +} + diff --git a/Week_02/id_33/Leetcode_98_33.java b/Week_02/id_33/Leetcode_98_33.java new file mode 100644 index 00000000..334249e6 --- /dev/null +++ b/Week_02/id_33/Leetcode_98_33.java @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode id=98 lang=java + * + * [98] Validate Binary Search Tree + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public boolean isValidBST(TreeNode root) { + return isBST(root, Long.MIN_VALUE, Long.MAX_VALUE); + } + + public boolean isBST(TreeNode root, long min, long max) { + if (root == null) + return true; + if (root.val <= min || root.val >= max) + return false; + return isBST(root.left, min, root.val) && isBST(root.right, root.val, max); + } +} + diff --git a/Week_03/id_33/Leetcode_102_33.java b/Week_03/id_33/Leetcode_102_33.java new file mode 100644 index 00000000..2a9524d1 --- /dev/null +++ b/Week_03/id_33/Leetcode_102_33.java @@ -0,0 +1,35 @@ +/* + * @lc app=leetcode id=102 lang=java + * + * [102] Binary Tree Level Order Traversal + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public List> levelOrder(TreeNode root) { + List> list = new ArrayList>(); + if(root == null) return list; + Queue queue = new LinkedList<>(); + queue.offer(root); + while(!queue.isEmpty()){ + int size = queue.size(); + List levelList=new ArrayList(); + for(int i = 0; i < size; i++){ + TreeNode node = queue.poll(); + levelList.add(node.val); + if(node.left != null) queue.offer(node.left); + if(node.right != null) queue.offer(node.right); + } + list.add(levelList); + } + return list; + } +} + diff --git a/Week_03/id_33/Leetcode_127_33.java b/Week_03/id_33/Leetcode_127_33.java new file mode 100644 index 00000000..b8aa9cb7 --- /dev/null +++ b/Week_03/id_33/Leetcode_127_33.java @@ -0,0 +1,42 @@ +/* + * @lc app=leetcode id=127 lang=java + * + * [127] Word Ladder + */ +class Solution { + public int ladderLength(String beginWord, String endWord, List wordList) { + if (!wordList.contains(endWord)) return 0; + int level = 1; + Set set = new HashSet<>(); + for(String word: wordList){ + set.add(word); + } + Queue queue = new LinkedList<>(); + queue.offer(beginWord); + while(!queue.isEmpty()){ + int size = queue.size(); + for(int i = 0; i < size; i++){ + String tmpWord = queue.poll(); + char[] array = tmpWord.toCharArray(); + for(int j = 0; j < array.length; j++){ + char c = array[j]; + for(char k = 'a'; k <= 'z'; k++){ + array[j] = k; + String newWord = String.valueOf(array); + if(k != c &&set.contains(newWord)){ + queue.offer(newWord); + set.remove(newWord); + if(newWord.equals(endWord)){ + return ++level; + } + } + } + array[j] = c; + } + } + level++; + } + return 0; + } +} + diff --git a/Week_03/id_33/Leetcode_200_33.java b/Week_03/id_33/Leetcode_200_33.java new file mode 100644 index 00000000..f863c1c4 --- /dev/null +++ b/Week_03/id_33/Leetcode_200_33.java @@ -0,0 +1,31 @@ +/* + * @lc app=leetcode id=200 lang=java + * + * [200] Number of Islands + */ +class Solution { + public int numIslands(char[][] grid) { + int island = 0; + if (grid.length == 0 || grid[0].length == 0) return 0; + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == '1') { + island++; + dfs(grid, i, j); + } + } + } + return island; + } + + public static void dfs (char[][] grid, int i, int j) { + if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length || grid[i][j] == '0') + return; + grid[i][j] = '0'; + dfs(grid, i - 1, j); + dfs(grid, i, j - 1); + dfs(grid, i, j + 1); + dfs(grid, i + 1, j); + } +} + diff --git a/Week_03/id_33/Leetcode_295_33.java b/Week_03/id_33/Leetcode_295_33.java new file mode 100644 index 00000000..542c0669 --- /dev/null +++ b/Week_03/id_33/Leetcode_295_33.java @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode id=295 lang=java + * + * [295] Find Median from Data Stream + */ +class MedianFinder { + + /** initialize your data structure here. */ + public MedianFinder() { + + } + + public void addNum(int num) { + + } + + public double findMedian() { + + } +} + +/** + * Your MedianFinder object will be instantiated and called as such: + * MedianFinder obj = new MedianFinder(); + * obj.addNum(num); + * double param_2 = obj.findMedian(); + */ + diff --git a/Week_03/id_33/Leetcode_703_33.java b/Week_03/id_33/Leetcode_703_33.java new file mode 100644 index 00000000..645b546f --- /dev/null +++ b/Week_03/id_33/Leetcode_703_33.java @@ -0,0 +1,33 @@ +/* + * @lc app=leetcode id=703 lang=java + * + * [703] Kth Largest Element in a Stream + */ +class KthLargest { + + private PriorityQueue minHeap; + private int k; + public KthLargest(int k, int[] nums) { + this.k = k; + this.minHeap = new PriorityQueue<>(); + for (Integer n : nums) { + minHeap.add(n); + if(minHeap.size()>k) + minHeap.poll(); + } + } + + public int add(int val) { + minHeap.add(val); + if(minHeap.size() > k) + minHeap.poll(); + return minHeap.peek(); + } +} + +/** + * Your KthLargest object will be instantiated and called as such: + * KthLargest obj = new KthLargest(k, nums); + * int param_1 = obj.add(val); + */ +