This file contains 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 missingNumber_OneByOne(int[] nums) { | |
//approach1. sort first, then I compare one by one, if the current element is not previous element +1, return the number | |
// time complexity is o(n) and space is O(1) | |
Arrays.sort(nums); | |
for(int i=1;i<nums.length;i++){ | |
System.out.println(nums[i]+" "+nums[i-1]); | |
if(nums[i-1]!=nums[i]-1) return nums[i]-1; | |
} | |
return 0; |
This file contains 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 longestConsecutive(int[] nums) { | |
Set<Integer> set=new HashSet<>(); | |
Map<Integer,Integer> map=new HashMap<>(); | |
int max=0; | |
for(int i=0;i<nums.length;i++){ | |
if(set.contains(nums[i])) continue; | |
int minLeng=map.getOrDefault(nums[i]-1,0); | |
int maxLeng=map.getOrDefault(nums[i]+1,0); | |
int length=maxLeng+minLeng+1; |
This file contains 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 String removeOuterParentheses(String s) { | |
//(()()) | (()) | (()(())) | |
//()() | () | ()(()) | |
Deque<Character> stack=new ArrayDeque<>(); | |
String result=""; | |
for(char sub:s.toCharArray()){ | |
if(sub=='(' && stack.isEmpty()){ | |
stack.push(sub); | |
} |
This file contains 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 String getHint(String secret, String guess) { | |
//create a map to keep value and index , value is the key and index is the value | |
Map<Character,Integer> secretMap=new HashMap<>(); | |
int numA=0,numB=0; | |
char[] charS=secret.toCharArray(),charG=guess.toCharArray(); | |
for(int i=0;i<charS.length;i++){ | |
if(charS[i]==charG[i]) numA++; | |
else{ | |
int count=secretMap.getOrDefault(charS[i],0); |
This file contains 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[] nextGreaterElement(int[] nums1, int[] nums2) { | |
//create a hashmap to keep each element and its next greater element from nums2 | |
Map<Integer,Integer> map=new HashMap<>(); | |
//create a stack to keep the greatest element | |
Deque<Integer> stack=new ArrayDeque<>(); | |
//traverse nums2 from last one forward to first one | |
//4,1,2,5,3 | |
for(int i=nums2.length-1;i>=0;i--){ | |
while(!stack.isEmpty() && stack.peek()<nums2[i]){ |
This file contains 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[] asteroidCollision(int[] asteroids) { | |
/*test Case: | |
[10,5,-10,11,12] expect: [11,12] | |
[-10,11,12] expect: [-10,11,12] | |
just only top of stack is positive and current element is negative, they will meet | |
*/ | |
//traversal elements of asteroids | |
//stack LIFO | |
Deque<Integer> stack=new ArrayDeque<>(); |
This file contains 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[] dailyTemperatures(int[] temperatures) { | |
Deque<Integer> stack=new ArrayDeque<>(); | |
int[] result=new int[temperatures.length]; | |
for(int i=temperatures.length-1;i>=0;i--){ | |
while(!stack.isEmpty() && temperatures[i]>=temperatures[stack.peek()]){ | |
stack.pop(); | |
} | |
if(stack.isEmpty()) result[i]=0; | |
else result[i]=stack.peek()-i; |
This file contains 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 { | |
//set directions | |
int[][] dirs=new int[][]{{1,0},{0,1},{-1,0},{0,-1}}; | |
public int numIslands(char[][] grid) { | |
int count=0; | |
//BFS | |
int m=grid.length,n=grid[0].length; | |
for(int i=0;i<m;i++){ | |
for(int j=0;j<n;j++){ | |
if(grid[i][j]=='1'){ |
This file contains 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 ladderLength(String beginWord, String endWord, List<String> wordList) { | |
//build graph-->graph(List<String> wordList) | |
//check beginWord in wordList | |
if(!wordList.contains(beginWord)) wordList.add(beginWord); | |
//check endWord | |
if(!wordList.contains(endWord)) return 0; | |
//invoke graph | |
Map<String,List<String>> map=graph(wordList); | |
This file contains 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[][] updateMatrix(int[][] mat) { | |
/*test case: | |
[[1,1,1],[1,1,1],[1,0,1]] | |
[[0,0,0],[0,0,0],[0,0,1]] | |
*/ | |
//creat m and n to be mat withe and heigh | |
int m=mat.length, n=mat[0].length; | |
//creat a int[][] data type to be a result | |
int[][] result=new int[m][n]; |
NewerOlder