Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Week_02/id_33/Leetcode_111_33.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

21 changes: 21 additions & 0 deletions Week_02/id_33/Leetcode_1_33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* @lc app=leetcode id=1 lang=java
*
* [1] Two Sum
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> 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[]{};
}
}

23 changes: 23 additions & 0 deletions Week_02/id_33/Leetcode_3_33.java
Original file line number Diff line number Diff line change
@@ -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<Character, Integer> 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;
}
}

28 changes: 28 additions & 0 deletions Week_02/id_33/Leetcode_98_33.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

35 changes: 35 additions & 0 deletions Week_03/id_33/Leetcode_102_33.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if(root == null) return list;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> levelList=new ArrayList<Integer>();
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;
}
}

42 changes: 42 additions & 0 deletions Week_03/id_33/Leetcode_127_33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* @lc app=leetcode id=127 lang=java
*
* [127] Word Ladder
*/
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
if (!wordList.contains(endWord)) return 0;
int level = 1;
Set<String> set = new HashSet<>();
for(String word: wordList){
set.add(word);
}
Queue<String> 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;
}
}

31 changes: 31 additions & 0 deletions Week_03/id_33/Leetcode_200_33.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

28 changes: 28 additions & 0 deletions Week_03/id_33/Leetcode_295_33.java
Original file line number Diff line number Diff line change
@@ -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();
*/

33 changes: 33 additions & 0 deletions Week_03/id_33/Leetcode_703_33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* @lc app=leetcode id=703 lang=java
*
* [703] Kth Largest Element in a Stream
*/
class KthLargest {

private PriorityQueue<Integer> 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);
*/