Skip to content

Instantly share code, notes, and snippets.

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;
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;
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);
}
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);
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]){
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<>();
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;
@sp04tw
sp04tw / 200NumberOfIslands_BFS.java
Last active April 21, 2022 10:08
Graph DFS #200
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'){
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);
@sp04tw
sp04tw / 01Matrix.java
Created April 20, 2022 22:20
Graph BFS #542
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];