This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello World! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MinimumDepthBinaryTreeBFS { | |
public int minDepth(TreeNode root) { | |
if(root == null) { | |
return 0; | |
} | |
Queue<TreeNode> queue = new LinkedList<>(); | |
queue.add(root); | |
int level = 0; | |
while(!queue.isEmpty()) { | |
level++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MinimumDepthBinaryTreeDFS { | |
public int minDepth(TreeNode root) { | |
if(root == null) { | |
return 0; | |
} | |
int leftHeight = minDepth(root.left); | |
int rightHeight = minDepth(root.right); | |
if(leftHeight == 0 || rightHeight == 0) { | |
return leftHeight + rightHeight + 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RomanToInteger { | |
public int romanToInt(String s) { | |
int prev = 0; | |
int result = 0; | |
for(int i = s.length() -1; i >= 0; i--) { | |
char cur = s.charAt(i); | |
int curVal = getInteger(cur); | |
if(curVal < prev) { | |
result -= curVal; | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArrangingCoinsBS { | |
public int arrangeCoins(int n) { | |
int low = 1; | |
int high = n; | |
while(low < high) { | |
int mid = low +(high -low+1)/2; | |
long num = (((long)mid * ((long)mid +1l))/2l); | |
if(num == n) { | |
return mid; | |
} else if(n < num){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArrangingCoins { | |
public int arrangeCoins(int n) { | |
double result = (-1.0 + Math.sqrt(1.0+8.0*n))/2.0; | |
return (int)Math.floor(result); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SubarrayProductLessThanK { | |
public int numSubarrayProductLessThanK(int[] nums, int k) { | |
if(k <= 1) { | |
return 0; | |
} | |
int count = 0; | |
int prod = 1; | |
for(int i =0, j =0; j < nums.length; j++) { | |
prod *= nums[j]; | |
while(prod >= k) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchInRotatedArray { | |
public int search(int[] nums, int target) { | |
return search(nums, 0, nums.length - 1, target); | |
} | |
private int search(int[] nums, int start, int end, int target) { | |
if (start > end) return -1; | |
int midIdx = start + (end - start) / 2; | |
int mid = nums[midIdx]; | |
if (target == mid) return midIdx; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int[] twoSum(int[] nums, int target) { | |
Map<Integer, Integer> map = new HashMap<>(); | |
for(int i = 0; i < nums.length; i++) { | |
int num = nums[i]; | |
if(map.containsKey(num)) { | |
return new int[]{i, map.get(num)}; | |
} else { | |
map.put(target - num, i); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BullsAndCows { | |
public String getHint(String secret, String guess) { | |
int n = secret.length(); | |
int[] map = new int[10]; | |
int a = 0; | |
int b = 0; | |
for (int i = 0; i < n; i++) { | |
int s = secret.charAt(i) - '0'; | |
int g = guess.charAt(i) - '0'; | |
if (s == g) { |
NewerOlder