id
stringlengths
36
36
text
stringlengths
1
1.25M
90011389-4dd4-400f-8c1f-78d04008d04a
public String countAndSay(int n) { if(n==0) return ""; if(n==1) return "1"; String res = "1"; for(int i=2; i<=n; i++) { res = countAndSay(res); } return res; }
be9e8d2d-54ed-452a-8ae2-52366ca17c16
private String countAndSay(String seq) { String res = ""; String lastValue = ""; int lastValueNumber = 0; for(int i = 0; i < seq.length(); i++) { if(!seq.substring(i, i+1).equals(lastValue)) { if(lastValueNumber !=0) res += lastValueNumber + lastValue; ...
afa78dd9-1757-48da-b4ee-35e0b8909e5b
public ArrayList<String> anagrams(String[] strs) { HashMap<String, ArrayList<String>> res = new HashMap<String, ArrayList<String>>(); ArrayList<String> result = new ArrayList<String>(); for(String str:strs) { String s = str.replace(" ", ""); char[] arr = s.toCharArray(); Arrays.sor...
ab2dbc0a-b287-4f7e-92b2-2d37ba609906
public int lengthOfLastWord(String s) { s = s.trim(); if(s=="") return 0; String[] arr = s.split(" "); int size = arr.length; return arr[size-1].length(); }
2fe81605-479f-42c8-b61b-b07a5a2e468f
public ArrayList<String> letterCombinations(String digits) { ArrayList<String> map = new ArrayList<String>(); map.add(""); map.add(""); map.add("abc"); map.add("def"); map.add("ghi"); map.add("jkl"); map.add("mno"); map.add("pqrs"); map.add("tuv"); map.add("wxyz");...
f39f8b31-ef5b-44dc-87cf-58d8dd5964b9
private ArrayList<String> combineletter(String digits, ArrayList<String> map) { ArrayList<String> res = new ArrayList<String>(); if(digits.length() == 0) { res.add(""); return res; } int digit = Integer.parseInt(digits.substring(0, 1)); String mapping = map.get(d...
a94aa89f-b9fa-4bd2-95a9-c5b017eef4e7
public int lengthOfLongestSubstring1(String s) { if(s.length()<=1) return s.length(); int i=0, max=1; int[] map=new int[256]; for(int k=0; k<256; k++) map[k] = -1; while(i<s.length()-1) { map[s.charAt(i)] = i; int j=i+1; for(;j<s.length() && ...
c362f1ca-f723-4d0f-b603-4359ea3b4777
public int lengthOfLongestSubstring(String s) { if(s.length()<=1) return s.length(); int i=0, max=1; int[] map=new int[256]; for(int k=0; k<256; k++) map[k] = -1; while(i<s.length()-1) { map[s.charAt(i)] = i; int k=i-1, j=i+1; for(;k>=0 && (map[s.char...
b677860a-4e55-4dc3-b11e-a1c100c8029e
public ArrayList<ArrayList<String>> partition(String s) { ArrayList<ArrayList<String>> res = new ArrayList<ArrayList<String>>(); ArrayList<String> path =new ArrayList<String>(); dfs(s, path, 0, res); return res; }
9652676e-9f81-473d-bb7e-4ddf7ef0c2b5
private void dfs(String s, ArrayList<String> path, int start, ArrayList<ArrayList<String>> res) { if(start==s.length()) { ArrayList<String> newpath = new ArrayList<String>(); newpath.addAll(path); res.add(newpath); return; } for(int j=start; j<s.length(); j++) ...
f883bec5-6f6c-4671-9d50-0b6bef223a14
private boolean isPalindrome(String s, int start, int end) { char[] inputs = s.toCharArray(); while(start<end) { if(Character.toUpperCase(inputs[start])==Character.toUpperCase(inputs[end])) { start++; end--; } else return false; } return tr...
0e6cb816-e96e-4438-9e65-06afb12eb7e5
public int uniquePaths1(int m, int n) { int[][] steps = new int[m][n]; steps[0][0] = 0; for(int i =0; i<m; i++) steps[i][0] =1; for(int i =0; i<n; i++) steps[0][i] =1; if(m==1 || n==1) return steps[m-1][n-1]; int total = getStep(m-1, n-1, steps); return total; }
9c3b1e5d-3c74-4eb1-a385-7c1c9b2aa73b
private int getStep(int m, int n, int[][] steps) { int path1, path2; if(steps[m-1][n]>0) path1 =steps[m-1][n]; else path1 = getStep(m-1, n, steps); if(steps[m][n-1]>0) path2 =steps[m][n-1]; else path2 = getStep(m, n-1, steps); steps[m][n] = path1 +...
e793cdb3-88f5-4225-872f-01643d9ebc8b
public int uniquePaths(int m, int n) { int[][] steps = new int[m][n]; steps[0][0] = 0; for(int i =0; i<m; i++) steps[i][0] =1; for(int i =0; i<n; i++) steps[0][i] =1; for(int i=1; i<m; i++) { for(int j=1; j<n; j++) { steps[i][j] = steps[i-1][j] + steps[i][j-1]; ...
14274f00-56e0-4780-9e33-01a43449c1aa
public int uniquePathsWithObstacles(int[][] obstacleGrid) { int m = obstacleGrid.length, n=obstacleGrid[0].length; int[][] steps = new int[m][n]; steps[0][0] = (obstacleGrid[0][0]==0?1:0); for(int i =1; i<m; i++) steps[i][0] =(obstacleGrid[i][0]==1? 0:steps[i-1][0]); for(int i =...
4adc2a9a-b6fb-4c5d-8388-cd140aec3040
public ArrayList<String[]> solveNQueensTLE(int n) { ArrayList<String[]> res = new ArrayList<String[]>(); int[][] board=new int[n][n]; int currentcolumn = 0; solveTLE(currentcolumn, n, board, res); return res; }
2fe6a190-51aa-41d0-8239-d6572f2f2eab
private void solveTLE(int currentcolumn, int size, int[][] board, ArrayList<String[]> res) { if(currentcolumn == size) { String[] solution =new String[size]; String row=""; for(int i=0; i<size; i++) { row = ""; for(int j=0; j<size; j++) { ro...
64b85db0-705a-4606-897e-94055ee634cb
private boolean validPlaceTLE(int column, int row, int[][] board) { int sum = 0; for(int i=0; i<board.length; i++) sum += board[row][i]; if(sum>0) return false; for(int i=0; i<board.length; i++) sum += board[i][column]; if(sum>0) return false; for(in...
3facdd05-e0ca-4888-8c2c-b41cfefc88a3
public ArrayList<String[]> solveNQueens(int n) { ArrayList<String[]> res = new ArrayList<String[]>(); HashSet<Integer> cols = new HashSet<Integer>(); HashSet<Integer> diag1 = new HashSet<Integer>(); HashSet<Integer> diag2 = new HashSet<Integer>(); int[] board = new int[n]; int...
1fcfcbf6-02f3-4245-ad0a-9ade7a08cfb4
private void solve(int currentrow, int size, int[] board, ArrayList<String[]> res, HashSet<Integer> cols, HashSet<Integer> diag1, HashSet<Integer> diag2) { if(currentrow == size) { String[] solution =new String[size]; for(int i=0; i<size; i++) { char[] chars ...
062b4ba3-4221-4fb4-a257-af593492a12a
private boolean validPlace(int row, int col, HashSet<Integer> cols, HashSet<Integer> diag1, HashSet<Integer> diag2) { if(cols.contains(col) || diag1.contains(row+col)||diag2.contains(col-row)) return false; return true; }
31021890-fbda-4131-9051-ecc9b7ea2e99
public int totalNQueens(int n) { HashSet<Integer> cols = new HashSet<Integer>(); HashSet<Integer> diag1 = new HashSet<Integer>(); HashSet<Integer> diag2 = new HashSet<Integer>(); int[] board = new int[n]; int currentrow = 0; int total = solvetotal(curren...
ccdbd203-b348-4823-8029-3d0f394fc519
private int solvetotal(int currentrow, int size, int[] board, HashSet<Integer> cols, HashSet<Integer> diag1, HashSet<Integer> diag2, int total) { if(currentrow == size) { total ++; return total; } for(int i=0; i<size; i++) { if(!(cols.contains(...
dc214636-148f-4c95-ab71-12fd7fa3d965
public ArrayList<String> restoreIpAddresses(String s) { ArrayList<String> res = new ArrayList<String>(); int part = 0; dfs(s, res, 0, "", part); return res; }
dfb13c41-0547-48b6-bda8-5c22ebdc4b62
private void dfs(String s, ArrayList<String> res, int start, String IP, int part) { if(start == s.length() && part ==4) { res.add(IP); return; } if(part>=4) return; for(int i=start; i<s.length() && i< start+3; i++) { String t = s.substri...
81276b5f-fb0c-4bf9-9b02-634b62755a4d
public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> path = new ArrayList<Integer>(); Arrays.sort(candidates); dfs(res, candidates, target, path, 0); return res; ...
412e7dfb-b58c-4626-9b97-8681a16ee82c
private void dfs(ArrayList<ArrayList<Integer>> res, int[] candidates, int target, ArrayList<Integer> path, int start) { if(target == 0) { ArrayList<Integer> p = new ArrayList<Integer>(); p.addAll(path); res.add(p); return; } int i =start; while...
0528b720-0a45-4964-abf7-c6ad5e5e03e6
public ArrayList<ArrayList<Integer>> combinationSum2(int[] num, int target) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> path = new ArrayList<Integer>(); Arrays.sort(num); dfs2(res, num, target, path, 0); return res; }
47789c26-dd74-41b6-bcbf-f9e647bb897d
private void dfs2(ArrayList<ArrayList<Integer>> res, int[] candidates, int target, ArrayList<Integer> path, int start) { if(target == 0) { ArrayList<Integer> p = new ArrayList<Integer>(); p.addAll(path); res.add(p); return; } int i =start; int ...
8c90ae2f-56da-43b8-a754-c61bf8a13670
public ArrayList<String> generateParenthesis(int n) { ArrayList<String> res = new ArrayList<String>(); String path = ""; Stack<String> st = new Stack<String>(); generate(res, path, 0, n, st); return res; }
45e336a6-c1d1-40a6-b3ee-dd8e4e3f1aa9
private void generate(ArrayList<String> res, String path, int posi, int size, Stack<String> st) { if(posi == size && st.isEmpty()) { res.add(path); return; } if(posi < size) { st.push("("); generate(res, path + "(", posi+1, size, st);...
da8aaaf9-d9fd-460e-8ffb-2ea234981c8b
public void solveSudoku(char[][] board) { fillNumber(board, 0, 0); }
18b4fca6-6fcc-49e2-8a79-81807fee6369
private boolean fillNumber(char[][] board, int row, int col) { int size = board.length; for(int i=row; i<size; i++) { int j=col; if(i==row+1)j=0; for(; j<size; j++) { if(board[i][j]!='.') ...
cb150178-b68a-42b3-9355-3937f38e0bfa
public boolean isValidSudoku(char[][] board, int row, int col, int val) { for(int i=0; i<9; i++) { if(Character.getNumericValue(board[row][i])==val) return false; } for(int i=0; i<9; i++) { if(Character.getNumericValue(boa...
19ea6f5b-aa7c-4ef0-b55d-185cf98fde5f
public boolean exist(char[][] board, String word) { boolean[][] visited = new boolean[board.length][board[0].length]; for(int i=0; i<board.length; i++) { for(int j=0; j<board[0].length; j++) { if(board[i][j] == word.charAt(0)) { ...
7d5ece3b-56a2-4528-ad88-0494e0084d0d
private boolean check(char[][] board, boolean[][] visited, int row, int col, String word, int pos) { if(pos==word.length()) return true; if(row>0 && !visited[row-1][col] && board[row-1][col] == word.charAt(pos)) { visited[row-1][col] = true; if(check(board, visited, ro...
93426a2d-2cd1-40b7-9ae8-d193b9b8e1ff
public String minWindow(String S, String T) { if(T.length() == 0) return ""; int lastBeginIndex = -1; String res = ""; for(int i=0,j=0; i<S.length(); i++) { if(S.charAt(i) == T.charAt(j)) { j++; if(j==1)lastBeginIndex = i; if(j=...
cf132ded-d860-4fcf-8ff0-e475c3eafb38
public String multiply(String num1, String num2) { if(num1.equals("0") || num2.equals("0")) return "0"; String res = ""; String midres = ""; int carryOver = 0; char[] n1 = num1.toCharArray(); char[] n2 = num2.toCharArray(); for(int i=n...
789ac095-552b-4424-96e8-6d9354ad18d4
public String addString(String num1, String num2) { String res = ""; int carryOver = 0; char[] n1 = num1.toCharArray(); char[] n2 = num2.toCharArray(); for(int i=n1.length-1, j=n2.length-1; i>=0 || j>=0; i--, j--) { in...
70db4a53-3e8d-4760-9113-89f4d3283b9d
public ArrayList<Integer> findSubstring(String S, String[] L) { ArrayList<Integer> res = new ArrayList<Integer>(); ArrayList<String> source = new ArrayList<String>(); boolean[] bUsed = new boolean[L.length]; int count = 0; int size = L[0].length(); for(int i=0; i<S.length()-siz...
c75b849c-c9a2-465a-aba2-27e276a047e3
public int minDistance(String word1, String word2) { if(word1.length() ==0) return word2.length(); if(word2.length() ==0) return word1.length(); int[][] res = new int[word1.length()+1][word2.length()+1]; res[0][0] = 0; for(int i=1; i<=word1.length(); i++) res[i][0] = i; ...
33bad0df-ee63-4472-a4b7-aaeebe948e44
public boolean wordBreak(String s, Set<String> dict) { if(s.length()==0) return true; boolean[] res = new boolean[s.length()]; for(int i=0; i<s.length(); i++) { for(String d:dict) { if(i >= d.length()-1 && s.substring(i-d.length(...
0102371e-e308-446a-9e45-19cef9e9b2f5
public static void mergeSort(int[] a){ int[] temp = new int[a.length]; int[][] matrix = new int[a.length][a.length]; mergeSort(a,temp,0,a.length-1); quickSort(a, 0, a.length-1); doInsertionSort(a); binarySearch(a, 1); rec...
8590ca47-444a-4458-a518-c27b3732c49c
public static boolean search2DMatrix(int[][] matrix, int target) { int start = 0; int end = matrix.length-1; int row=0; while(start<=end) { int midrow = (start + end)/2; if(target<matrix[midrow][0]) { end = midrow-1; }else if(target>=ma...
b55a0570-d8f2-4d89-b19f-e0c257ef9b15
public static boolean searchinRotatedArrayWithDup(int[] A, int target) { int start = 0; int end=A.length-1; int mid; while(start<=end) { mid=(start+end)/2; if(A[mid]==target) return true; if(A[start]<A[mid]) ...
2255a388-5e73-491b-ae26-dd79eda297f1
public int numDistinct(String S, String T) { if(S.equals(T)) return 1; if(S.length()<=T.length()) return 0; int[][] res = new int[S.length()+1][T.length()+1]; res[0][0] = 1; for(int i=1; i<S.length()+1; i++) res[i][0] = 1; for(int j=1; j<T.length()+1; j++) res[0][j] = 0...
3a6d5edf-ed9b-4708-8b39-cab14d9dca16
public static int searchinRotatedArray(int[] A, int target) { int start = 0; int end = A.length-1; int mid; while(start<=end) { mid = (start+end)/2; if(A[mid] == target) return mid; else { i...
3d95749b-7848-4f94-a2bf-dd292b5b6b2d
public static int recursiveBinarySearch(int[] sortedArray, int start, int end, int key) { if (start < end) { int mid = start + (end - start) / 2; if (key < sortedArray[mid]) { return recursiveBinarySearch(sortedArray, start, mid, key); ...
abb8e50a-494f-4f43-bb30-58998ed2b260
public static int binarySearch(int[] inputArr, int key) { int start = 0; int end = inputArr.length - 1; while (start <= end) { int mid = (start + end) / 2; if (key == inputArr[mid]) { return mid; } if (key < inputArr[mid]) ...
72a1c9ce-f246-4d57-94c5-56f13303f25f
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(num.length == 0) return res; Arrays.sort(num); ArrayList<Integer> init = new ArrayList<Integer>(); HashSet<String> dup=new HashSet<String...
5990f59c-e615-4d62-a5b5-ed0f58c4bf88
private static void quickSort(int[] inputArr, int lowerIndex, int higherIndex) { int i = lowerIndex; int j = higherIndex; // calculate pivot number, I am taking pivot as middle index number int pivot = inputArr[lowerIndex+(higherIndex-lowerIndex)/2]; // Divide into two ...
095e084a-72d1-4e07-8712-15bc5f3b9ac0
private static void exchangeNumbers(int[] inputArr, int i, int j) { int temp = inputArr[i]; inputArr[i] = inputArr[j]; inputArr[j] = temp; }
398b4c9d-f4e1-4f96-8856-4604cd3828e9
private static void mergeSort(int[] a, int[] temp, int left, int right) { if(left<right){ int center = (left + right)/2; mergeSort(a,temp,left,center); mergeSort(a,temp,center+1,right); merge(a,temp,left,center+1,right); } }
ebd9ba41-995c-4f22-aacb-cb121c6dd7ae
public boolean isScramble(String s1, String s2) { return scramble(s1).get(s1).contains(s2); }
7de99fab-9d4e-4165-9ec4-5fc130afd38b
private static void merge(int[] a, int[] temp, int leftpos, int rightpos, int rightend) { int leftend = rightpos-1; int temppos = leftpos; int sum = rightend - leftpos+1; while(leftpos<=leftend&&rightpos<=rightend) if(a...
8a8cf3a2-a098-4d1d-a653-8eb4dbadc435
public static int[] doInsertionSort(int[] input){ int temp; for (int i = 1; i < input.length; i++) { for(int j = i ; j > 0 ; j--){ if(input[j] < input[j-1]){ temp = input[j]; input[j] = input[j-1]; input[j-1...
67b50f17-7670-413c-ac9a-194e6df92529
private HashMap<String, HashSet<String>> scramble(String s) { HashMap<String, HashSet<String>> res = new HashMap<String, HashSet<String>>(); if(s.length() ==0) return res; for(int i=0; i<s.length(); i++) { String curr = s.substring(i, i+1); if(!res.conta...
fa73330f-8124-49fe-9a08-2f52d445261e
public ArrayList<String> wordBreak2(String s, Set<String> dict) { ArrayList<String> res = new ArrayList<String>(); ArrayList<String> path = new ArrayList<String>(); breakWord(s, dict, path, res); return res; }
2ae859aa-f0f6-4010-8d1c-94bf580fab4f
private void breakWord(String s, Set<String> dict, ArrayList<String> path, ArrayList<String> res) { if(s.length() ==0) { String r=""; for(String p:path) { r = r+ (r.length()==0?p:" "+p); } res.add(r); return; } for(int...
ae3046ab-5c56-4105-ba72-7bc31a21fbfb
public ArrayList<String> fullJustify(String[] words, int L) { ArrayList<String> res = new ArrayList<String>(); String line= ""; for(int i=0; i<words.length; i++) { int ll = line.length()==0?words[i].length():line.length()+1+words[i].length(); if(ll<=L) { l...
e2567622-15e3-42aa-9c49-138f09cd417a
private String dup(int n) { String res = ""; for(int i=0; i<n; i++) { res += " "; } return res; }
3d91f7e2-3426-4667-ab6e-70cf7e96a9b5
public static void main (String args[]) { CCArrayandStrings testA =new CCArrayandStrings(); int[] num = {1,3,2}; //Arrays.sort(word); testA.printfive(250); System.out.println(); }
2106231c-963f-4a18-a00e-407f8cbbc051
public String compress(String input) { if(input.length()==0) return input; StringBuffer res = new StringBuffer(); char[] c = input.toCharArray(); char lastChar=c[0]; int lastIndex = 0; int count = 0; for(int i=1; i<c.length; i++) { if(c[i]!=lastChar) { count = i-lastIndex; res.append(l...
795c3b1c-c6dd-4af6-8ffd-a98acbccb42c
public boolean isUnique(String input) { if(input.length() > 256) return false; boolean[] isUsed = new boolean[256]; for(int i=0; i<input.length(); i++) { if(isUsed[input.charAt(i)]) return false; else isUsed[input.charAt(i)] = true; } return true; }
ba7bc8f3-1c49-41cb-bc9e-b87fb5b0c43d
public String reverseWords2(String s) { char[] c = s.toCharArray(); int start = 0; int end = c.length; while(start<end) { char t = c[start]; c[start] = c[end]; c[end] = t; start++; end--; } return new String(c); }
d2072ec2-8e82-4a48-9ef0-2303ff60fb10
public String reverseWords(String s) { String res = ""; int lastIndex = s.length(); for(int i=s.length()-1; i>=0; i--) { if(s.charAt(i)==' ') { if(i!=lastIndex -1) res = s.substring(i+1, lastIndex); lastIndex = i...
e3585ddb-84a3-41cb-b0d2-dddcc7c6500f
public void printfive(int n) { for(int i=0; i<=n; i++) { String x = String.valueOf(i); for(int j=0; j<x.length(); j++) { if(x.substring(j, j+1).equals("5")) { System.out.println(i); break; } } } }
2dc8e132-93bc-4e2b-b105-9bcdfbf9f05a
public void printfive2(int n) { ArrayList<String> res = new ArrayList<String>(); int div= 1; while(n/div>10) { div*=10; } while(div>=1) { if(n/div >= 5) { res.add("5"); } } }
af6a02ec-4ea4-4c2f-804d-581bc2ce2580
public void printprime(int n) { int j; if(n==2) System.out.println(2); for(int i=1; i<=n; i++) { boolean bPrime = true; for(j=2; j<=Math.sqrt(i); j++) { if(i%j==0) { bPrime = false; break; } } if(bPrime) System.out.println(i); } }
1bac3e6b-f592-486b-a562-7c53e27c4c5b
public ArrayList<ArrayList<Integer>> getcois(int cents) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> path = new ArrayList<Integer>(); getcoinInternal(cents, path, res); return res; }
4b06be4e-87c9-427c-a294-1914c3968ea8
private void getcoinInternal(int centsleft, ArrayList<Integer> path, ArrayList<ArrayList<Integer>> res) { if(centsleft == 0) { ArrayList<Integer> newpath = new ArrayList<Integer>(); newpath.addAll(path); res.add(newpath); return; } if(centsleft >= 25 && (path.size() ==0 || path.get(path.size()-1)==...
cdc6ba18-45e5-497f-b301-6176fa8bfd56
public void nextPermutation(int[] num) { int i; for(i = num.length-2; i>=0; i--) { if(num[i]<num[i+1]) break; } if(i==-1) { Arrays.sort(num); return; } int val = num[i]; num[i] = num[num...
39896918-aeeb-492e-a684-47ecd6cc2ff2
boolean IsUnique(String word) { boolean[] bUsed = new boolean[256]; for(int i=0; i<word.length(); i++) { if(bUsed[word.charAt(i)]) return false; else bUsed[word.charAt(i)] = true; } return true; }
a5a666d8-2861-4535-944a-fa827ea0405c
String reverse(String word) { String res = ""; for(int i=word.length()-1; i>=0; i--) { res = res+word.substring(i, i+1); } return res; }
7f32a55c-3e1f-42e8-b53b-cc28a9f0e914
public void removedup(char[] word) { if(word.length<2) return; int tail = 1; for(int i=1; i<word.length; i++) { int j; for(j=0; j<tail; j++) { if(word[i] == word[j]) { break; } } if(j==tail) { word[tail] = word[i]; tail++; } } word[tail] = 0; }
059d8ab4-c1e6-412b-8d66-9ea34e5965c0
public void removedup2(char[] word) { if(word.length<2) return; int tail = 1; boolean[] bUsed =new boolean[256]; bUsed[word[0]]=true; for(int i=1; i<word.length; i++) { if(!bUsed[word[i]]) { word[tail] = word[i]; bUsed[word[i]] =true; tail++; } } word[tail] = 0; }
99e3403c-6893-4666-88d8-33661e08dc41
public boolean isAnagrams(String s1, String s2) { if(s1.length() != s2.length()) return false; int[] count=new int[256]; for(int i=0; i<s1.length(); i++) { count[s1.charAt(i)] ++; } for(int i=0; i<s2.length(); i++) { if(count[s2.charAt(i)] ==0) return false; count[s2.charAt(i)] --; } re...
810b661d-16c7-4492-b841-143fdfec12e7
public String ReplaceSpace(char s[], int length) { int count = 0; for(int i=s.length-1; i>=0; i--) if(s[i] == ' ') count++; for(int i=s.length-1, j=s.length-1+count*2; i>=0; i--, j--) { if(s[i] == ' ') { s[j] = '0'; s[j-1] = '2'; s[j-2] = '%'; j = j-2; } else s[j] = s[i]; ...
66c1c49d-9a18-4456-a5f0-ad9ee32a7938
public void setZero(int[][] matrix) { if(matrix.length ==0 || matrix[0].length==0) return; int[] row = new int[matrix.length]; int[] col = new int[matrix[0].length]; for(int i=0; i<matrix.length; i++) { for(int j=0; j<matrix[0].length; j++) { if(matrix[i][j] == 0) { row[i] = 1; col...
9a2c46da-535d-4ce5-866c-14bdff46e4a3
public ArrayList<String> getResult(String s) { ArrayList<String> res = new ArrayList<String>(); for(int i=0; i<s.length(); i++) { char curr = s.charAt(i); if(curr == '*') { int size = res.size(); if(size == 0) { res.add("0"); res.add("1"); }else { for(int k=0; k<size;...
2ce822e3-03ce-4324-be70-be51300660f4
public static void printFive(int n) { for(int i=0; i<=n; i++) { String x = String.valueOf(i); //x.lastIndexOf(ch) char[] ar = x.toCharArray(); for(int j=0; j<ar.length; j++) { if(ar[j] == '5') { System.out.println(i); b...
c3bb00ba-4498-4c60-a188-c7156efc5315
ListNode(int x) { val = x; next = null; }
ae5b25f9-9add-44f9-ab7f-5c6aa0078eb9
RandomListNode(int x) { this.label = x; }
fe3c4ca0-0866-4517-8f96-eb4cad9a929c
public static void main (String args[]) { linklist testA =new linklist(); ListNode A = testA.new ListNode(2); A.next = testA.new ListNode(1); //A.next.next = testA.new ListNode(3); //A.next.next.next = testA.new ListNode(4); testA.insertionSortList(A); System.out.print("end"); }
bffa8ccc-11a3-478a-9315-0286f8e850ad
void maxSlidingWindow(int A[], int n, int w, int B[]) { LinkedList<Integer> Q = new LinkedList<Integer>(); for (int i = 0; i < w; i++) { while (!Q.isEmpty() && A[i] >= A[Q.getLast()]) Q.removeLast(); Q.addLast(i); } for (int i = w; i < n; i++) { B[i-w] = A[Q.getFirst()]; ...
6146083d-28c4-4556-be11-c7bc7f5f26f0
private ListNode insertAfterFirst(ListNode newnode, ListNode head) { if(head == null) return head; if(head.next ==null) { head.next = newnode; }else { ListNode next = head.next; head.next = newnode; newnode.next = next; } return head; }
1e264ecb-ff91-4c70-a792-526cef4ce1f7
public ListNode deleteDuplicates1(ListNode head) { if(head == null) return null; ListNode cur = head; int currentValue = head.val; while(cur != null && cur.next != null) { if(currentValue == cur.next.val) { cur.next = cur.next.next...
8c3cd951-06ac-4f4a-81a4-8abd97f48e11
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if(l1 == null) return l2; if(l2 == null) return l1; ListNode res = null; ListNode head = null; while(l1 != null && l2 !=null) { if(l1.val<l2.val) { if(res == null) { res = l1; he...
fd84e73a-b7a2-4209-aea8-6add42f2c4d1
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if(l1 ==null || l2 ==null) return null; ListNode c1 = l1; ListNode c2 = l2; ListNode res = new ListNode(-1); ListNode cur = res; int carryover = 0; while(c1 !=null || c2 !=null) { int val = ((c1==null?0:c1.val)...
6d5f3144-5283-44ce-858b-b2e3ac107aa0
public ListNode reverseBetween(ListNode head, int m, int n) { if(m==n) return head; ListNode start = null; ListNode beforestart = null; ListNode curr = null; for(int i=0; i<m-1; i++) { beforestart = beforestart==null?head:beforestart.next; } start = be...
c396602b-60c8-4c83-9ed5-726ea1a8afef
public ListNode partition(ListNode head, int x) { ListNode dummyleft = new ListNode(-1); ListNode dummyright = new ListNode(-1); ListNode left = dummyleft; ListNode right = dummyright; ListNode curr = head; while(curr != null) { if(curr.val < x) { left.n...
5f18fc20-fcc4-422f-b4a7-8710644c6a61
public ListNode deleteDuplicates(ListNode head) { if(head == null) return null; ListNode dummy = new ListNode(-1); dummy.next = head; ListNode cur = head; ListNode prev = dummy; int currentValue = head.val; boolean isDup = false; while(...
c07a00f8-9306-47ed-b68b-1cb7b71cb866
public ListNode rotateRight(ListNode head, int n) { if(head==null || n ==0 ) return head; int length = 1; ListNode cur = head; ListNode start = null; while(cur.next!=null) { length++; cur = cur.next; } cur.next = head; n = n%length; cur = h...
2ac76242-f307-4c37-9d9f-e6acd8daa73a
public ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(-1); dummy.next = head; ArrayList<ListNode> temp = new ArrayList<ListNode>(); ListNode cur = dummy; while(cur!=null) { temp.add(cur); cur = cur.next; } temp...
217b7eab-cfef-49af-93fd-26d9aba77cf1
public ListNode swapPairs(ListNode head) { ListNode dummy = new ListNode(-1); dummy.next = head; ListNode cur = dummy; ListNode temp = null; while(cur.next!=null && cur.next.next!=null) { temp =cur.next; cur.next = cur.next.next; temp.next = cur.next.next; c...
048cf366-536b-4869-bf63-f3ac3b026929
public ListNode reverseKGroup(ListNode head, int k) { if(k<2) return head; ListNode dummy = new ListNode(-1); dummy.next = head; ListNode prev = dummy; ListNode cur = head; ListNode curInner = head; boolean startreverse = true; while(cur!=null) { curInner = cur; ...
97091ea0-a246-4a18-aa6c-0766a60bda39
public boolean hasCycle(ListNode head) { if(head == null || head.next ==null) return false; HashSet<ListNode> lists = new HashSet<ListNode>(); ListNode curr = head; while(curr != null) { if(lists.contains(curr.next)) return true; lists.add(curr.next); curr=curr.nex...
9f393d32-aabc-4d75-80b5-8e954a096082
public ListNode detectCycle(ListNode head) { if(head == null || head.next ==null) return null; ListNode dummy = new ListNode(-1); dummy.next = head; HashSet<ListNode> lists = new HashSet<ListNode>(); ListNode curr = dummy; while(curr != null) { if(lists.contains(curr.next)) retu...
8325eb55-630f-44ed-82eb-228b673aafac
public void reorderList(ListNode head) { if(head == null || head.next ==null) return; ListNode curr1 = head; int count = 0; while(curr1 != null) { curr1=curr1.next; count ++; } //split after count/2 ListNode second = null; curr1 = head; int i = 1...
63ab94d8-8553-425f-9a57-5ae17877ca5d
public RandomListNode copyRandomList(RandomListNode head) { if(head == null) return null; RandomListNode curr = head; while(curr!=null) { RandomListNode newnode = new RandomListNode(curr.label); newnode.next = curr.next; newnode.random = curr.random; curr.next = newnode...