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; lastValue = seq.substring(i, i+1); lastValueNumber = 1; }else { lastValueNumber++; } } res += lastValueNumber + lastValue; return res; }
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.sort(arr); s = Arrays.toString(arr); if(res.containsKey(s)) { ArrayList<String> t = res.get(s); t.add(str); }else { ArrayList<String> t = new ArrayList<String>(); t.add(str); res.put(s, t); } } for(String key:res.keySet()) { if(res.get(key).size() > 1) result.addAll(res.get(key)); } return result; }
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"); return combineletter(digits, map); }
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(digit); String rest = digits.substring(1); ArrayList<String> prev = combineletter(rest, map); for(char a:mapping.toCharArray()) { for(String r:prev) { String t = a + r; res.add(t); } } return res; }
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() && (map[s.charAt(j)] < i || map[s.charAt(j)]== j); j++){ map[s.charAt(j)] = j; } max=Math.max(max, j-i); if(j==s.length()) return max; i=map[s.charAt(j)] +1; } return max; }
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.charAt(k)] < k || map[s.charAt(k)] > i||map[s.charAt(k)]==k) ; k--){ map[s.charAt(k)] = k; } //k=k+2; for(;j<s.length() && (map[s.charAt(j)] < k+1 || map[s.charAt(j)] > j ||map[s.charAt(j)]==j); j++){ map[s.charAt(j)] = j; } max = Math.max(max, j-(k+1)); i=j; } return max; }
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++) { if(isPalindrome(s, start, j)) { path.add(s.substring(start, j+1)); dfs(s, path, j+1, res); path.remove(path.size()-1); } } }
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 true; }
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 + path2; return steps[m][n]; }
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]; } } return steps[m-1][n-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 =1; i<n; i++) steps[0][i] =(obstacleGrid[0][i]==1? 0:steps[0][i-1]); for(int i=1; i<m; i++) { for(int j=1; j<n; j++) { if(obstacleGrid[i][j]==1) steps[i][j] = 0; else steps[i][j] = steps[i-1][j] + steps[i][j-1]; } } return steps[m-1][n-1]; }
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++) { row += (board[i][j] >0? String.valueOf(board[i][j]):"."); } solution[i] = row; } res.add(solution); return; } for(int i=0; i<size; i++) { if(validPlaceTLE(currentcolumn, i, board)) { board[i][currentcolumn] =1; currentcolumn++; solveTLE(currentcolumn, size, board, res); currentcolumn--; board[i][currentcolumn] =0; } } }
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(int i=0; i<board.length; i++) sum += board[i][i]; if(sum>0) return false; return true; }
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 currentrow = 0; solve(currentrow, n, board, res, cols, diag1, diag2); return res; }
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 = new char[size]; Arrays.fill(chars, '.'); chars[board[i]] = 'Q'; String row = new String(chars); solution[i] = row; } res.add(solution); return; } for(int i=0; i<size; i++) { if(validPlace(currentrow, i, cols, diag1, diag2)) { board[currentrow] =i; cols.add(i); diag1.add(i+currentrow); diag2.add(i-currentrow); currentrow++; solve(currentrow, size, board, res, cols, diag1, diag2); currentrow--; cols.remove(i); diag1.remove(i+currentrow); diag2.remove(i-currentrow); board[currentrow] =0; } } }
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(currentrow, n, board, cols, diag1, diag2, 0); return total; }
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(i) || diag1.contains(currentrow+i)||diag2.contains(i-currentrow))) { board[currentrow] =i; cols.add(i); diag1.add(i+currentrow); diag2.add(i-currentrow); currentrow++; total = solvetotal(currentrow, size, board, cols, diag1, diag2, total); currentrow--; cols.remove(i); diag1.remove(i+currentrow); diag2.remove(i-currentrow); board[currentrow] =0; } } return total; }
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.substring(start, i+1); if(Integer.parseInt(t)<=255 && part<4 && (!t.startsWith("0") ||t.equals("0"))) { int old = start; String oldIP = IP; if(IP == "") IP = t; else IP+="."+t; part++; start = i+1; dfs(s, res, start, IP, part); start = old; IP = oldIP; part--; } } }
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(i<candidates.length && candidates[i]<=target) { path.add(candidates[i]); dfs(res, candidates, target - candidates[i], path, i); path.remove(path.size()-1); i++; } }
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 prev = -1; while(i<candidates.length && candidates[i]<=target) { if(prev == candidates[i]) { i++; continue; } prev = candidates[i]; path.add(candidates[i]); dfs2(res, candidates, target - candidates[i], path, i+1); path.remove(path.size()-1); i++; } }
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); st.pop(); } if(!st.empty()) { st.pop(); generate(res, path + ")", posi, size, st); st.push("("); } }
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]!='.') continue; else { for(int k=1; k<10; k++) { if(isValidSudoku(board, i, j, k)) { board[i][j] = String.valueOf(k).charAt(0); if(fillNumber(board, (j==size-1?i+1:i), (j==size-1?0:j+1))) return true; board[i][j] = '.'; } } return false; } } } return true; }
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(board[i][col])==val) return false; } int x= row/3; int y = col/3; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(Character.getNumericValue(board[i+x*3][j+y*3])==val) return false; } } return true; }
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)) { visited[i][j] = true; if(check(board, visited, i, j, word, 1)) return true; visited[i][j] = false; } } } return false; }
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, row-1, col, word, pos+1)) return true; visited[row-1][col] = false; } if(row<board.length-1 && !visited[row+1][col] && board[row+1][col] == word.charAt(pos)) { visited[row+1][col] = true; if(check(board, visited, row+1, col, word, pos+1)) return true; visited[row+1][col] = false; } if(col>0 && !visited[row][col-1] && board[row][col-1] == word.charAt(pos)) { visited[row][col-1] = true; if( check(board, visited, row, col-1, word, pos+1)) return true; visited[row][col-1] = false; } if(col<board[0].length-1 && !visited[row][col+1] && board[row][col+1] == word.charAt(pos)) { visited[row][col+1] = true; if(check(board, visited, row, col+1, word, pos+1)) return true; visited[row][col+1] = false; } return false; }
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==T.length()) { j=0; String sub = S.substring(lastBeginIndex, i+1); res = sub.length() < res.length() || res.length()==0 ? sub:res; } } } return res; }
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=n1.length-1; i>=0; i--) { carryOver = 0; midres = ""; for(int j=n2.length-1; j>=0; j--) { int l1 = n1[i] - '0'; int l2 = n2[j] - '0'; int r = l1*l2+carryOver; int val = r%10; carryOver = r/10; midres =String.valueOf(val) + midres; } if(carryOver>0)midres = String.valueOf(carryOver) + midres; for(int k=i; k<n1.length-1; k++) { midres = midres + "0"; } res = addString(res, midres); } return res; }
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--) { int l1 = i<0? 0 : n1[i] - '0'; int l2 = j<0? 0 : n2[j] - '0'; int r = l1+l2+carryOver; int val = r%10; carryOver = r/10; res = String.valueOf(val) + res; } if(carryOver>0)res = String.valueOf(carryOver) + res; return res; }
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()-size; i=i+size) { source.add(S.substring(i, i+size)); } for(int i = 0; i<source.size(); i++) { for(int j =0; j<L.length; j++) { if(source.get(i).equals(L[j])) { if( bUsed[j] == false) { count++; bUsed[j] = true; if(count ==L.length) { res.add((i-L.length+1)*size); count =0; Arrays.fill(bUsed, false); } }else { //more than once count =0; Arrays.fill(bUsed, false); } } } } return res; }
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; for(int j=1; j<=word2.length(); j++) res[0][j] = j; for(int i=1; i<=word1.length(); i++) { for(int j=1; j<=word2.length(); j++) { if(word1.charAt(i-1) == word2.charAt(j-1)) { res[i][j] = res[i-1][j-1]; }else { res[i][j] = Math.min(Math.min(res[i-1][j-1], res[i][j-1]), res[i-1][j])+1; } } } return res[word1.length()][word2.length()]; }
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()+1, i+1).equals(d)) { res[i] = i-d.length()<0? true:res[i-d.length()]; if(res[i]==true) break; } } } return res[s.length()-1]; }
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); recursiveBinarySearch(a, 0, a.length-1, 1); searchinRotatedArray(a,1); searchinRotatedArrayWithDup(a, 1); search2DMatrix(matrix, 1); ArrayList<String> list = new ArrayList<String>(); Collections.sort(list); }
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>=matrix[midrow][0] && (midrow==matrix.length-1 || target<matrix[midrow+1][0])) { row = midrow; break; }else { start = midrow+1; } } start = 0; end = matrix[0].length-1; while(start<=end) { int mid = (start + end)/2; if(target<matrix[row][mid]) { end = mid-1; }else if(target==matrix[row][mid]) { return true; }else { start = mid+1; } } return false; }
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]) { if(target >= A[start] && target < A[mid]) end = mid-1; else start = mid+1; }else if(A[start]>A[mid]) { if(target > A[mid] && target <= A[end]) start = mid+1; else end = mid-1; }else { start++; } } return false; }
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; for(int i=0; i<S.length(); i++) { for(int j=0; j<T.length(); j++) { if(i<j) res[i+1][j+1] = 0; if(i==j) res[i+1][j+1] = S.substring(0, i+1).equals(T.substring(0, j+1))?1:0; if(i>j) { if(S.charAt(i)==T.charAt(j)) { res[i+1][j+1] = res[i][j]+res[i][j+1]; }else { res[i+1][j+1] = res[i][j+1]; } } } } return res[S.length()][T.length()]; }
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 { if(A[start]<=A[mid]) { if(target<A[start] || target >A[mid]) start = mid+1; else end = mid-1; }else { if(target<A[mid] || target >A[end]) end = mid-1; else start = mid+1; } } } return -1; }
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); } else if (key > sortedArray[mid]) { return recursiveBinarySearch(sortedArray, mid+1, end , key); } else { return mid; } } return -(start + 1); }
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]) { end = mid - 1; } else { start = mid + 1; } } return -1; }
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>(); init.add(num[0]); res.add(init); dup.add(init.toString()); for(int i=1; i<num.length; i++) { int n = num[i]; int size = res.size(); for(int k=0; k<size; k++) { ArrayList<Integer> t = res.get(0); res.remove(0); for(int j=0; j<=t.size(); j++) { ArrayList<Integer> nt=new ArrayList<Integer>(); nt.addAll(t); nt.add(j, n); if(!dup.contains(nt.toString())) { res.add(nt); dup.add(nt.toString()); } } } } return res; }
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 arrays while (i <= j) { while (inputArr[i] < pivot) { i++; } while (inputArr[j] > pivot) { j--; } if (i <= j) { exchangeNumbers(inputArr, i, j); //move index to next position on both sides i++; j--; } } // call quickSort() method recursively if (lowerIndex < j) quickSort(inputArr, lowerIndex, j); if (i < higherIndex) quickSort(inputArr, i, higherIndex); }
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[leftpos]<a[rightpos]) temp[temppos++] = a[leftpos++]; else temp[temppos++] = a[rightpos++]; while(leftpos<=leftend) temp[temppos++] = a[leftpos++]; while(rightpos<=rightend) temp[temppos++] = a[rightpos++]; for(int i = 0;i<sum;i++,rightend--) a[rightend] = temp[rightend]; }
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] = temp; } } } return input; }
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.containsKey(curr)) { HashSet<String> path = new HashSet<String>(); path.add(curr); res.put(curr, path); } } for(int x=2; x<=s.length(); x++) { for(int i=0; i<=s.length()-x; i++) { String curr = s.substring(i, i+x); if(!res.containsKey(curr)) { HashSet<String> path = new HashSet<String>(); for(int k=1; k<curr.length(); k++) { HashSet<String> left = res.get(curr.substring(0, k)); HashSet<String> right = res.get(curr.substring(k, curr.length())); for(String l:left) { for(String r:right) { if(!path.contains(l+r)) path.add(l+r); if(!path.contains(r+l)) path.add(r+l); } } } res.put(curr, path); } } } return res; }
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 i=1; i<=s.length(); i++) { String curr = s.substring(0, i); for(String d:dict) { if(curr.equals(d)) { path.add(d); breakWord(s.substring(i), dict, path, res); path.remove(d); } } } }
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) { line = line.length()==0?words[i]:line+" "+words[i]; }else { res.add(line); line=words[i]; } if(i==words.length-1) res.add(line); } if(res.size() ==0) res.add(""); for(int i=0; i<res.size()-1; i++) { line = res.get(i); int extra = L-line.length(); String[] w = line.split(" "); int spaces = w.length==1?extra: extra/(w.length-1); int mode = w.length==1?0:extra%(w.length-1); line = w[0]; for(int j=1; j<w.length; j++) { if(j<mode) line = line + dup(spaces+2) + w[j]; else line = line + dup(spaces+1) + w[j]; res.set(i, line); } } line = res.get(res.size()-1); int extra = L-line.length(); line = line + dup(extra); res.set(res.size()-1, line); return res; }
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(lastChar); res.append(count); lastIndex = i; lastChar = c[i]; } } res.append(lastChar); res.append(count); return (res.length()<input.length()?res.toString():input); }
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; } } return res; }
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)==25)) { path.add(25); getcoinInternal(centsleft-25, path, res); path.remove(path.size()-1); } if(centsleft >= 10 && (path.size() ==0 || path.get(path.size()-1)>=10)) { path.add(10); getcoinInternal(centsleft-10, path, res); path.remove(path.size()-1); } if(centsleft >= 5 && (path.size() ==0 || path.get(path.size()-1)>=5)) { path.add(5); getcoinInternal(centsleft-5, path, res); path.remove(path.size()-1); } if(centsleft >= 1 && (path.size() ==0 || path.get(path.size()-1)>=1)) { path.add(1); getcoinInternal(centsleft-1, path, res); path.remove(path.size()-1); } return; }
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.length-1]; num[num.length-1] = val; Arrays.sort(num, i+1, num.length-1); return; }
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)] --; } return true; }
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]; } return new String(s); }
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[j] = 1; } } } for(int i=0; i<matrix.length; i++) { for(int j=0; j<matrix[0].length; j++) { if(row[i] == 1||col[j] == 1) { matrix[i][j] = 0; } } } }
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; k++) { String old = res.get(0); res.remove(0); res.add(old+"0"); res.add(old+"1"); } } }else { if(res.size() == 0) res.add(s.substring(i, i+1)); else for(int k=0; k<res.size(); k++) { String old = res.get(k); res.set(k, old+s.substring(i, i+1)); } } } return res; //Arrays.sort(arg0, arg1, arg2); }
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); break; } } } }
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()]; while (!Q.isEmpty() && A[i] >= A[Q.getLast()]) Q.removeLast(); while (!Q.isEmpty() && Q.getFirst() <= i-w) Q.removeFirst(); Q.addLast(i); } B[n-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; }else { currentValue = cur.next.val; cur = cur.next; } } return head; }
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; head = l1; } else res.next =l1; l1=l1.next; }else { if(res == null) { res = l2; head = l2; } else res.next =l2; l2=l2.next; } } if(l1 == null) res.next= l2; if(l2 == null) res.next= l1; return head; }
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) + (c2==null?0:c2.val) + carryover)%10; carryover = ((c1==null?0:c1.val) + (c2==null?0:c2.val) + carryover)/10; ListNode x = new ListNode(val); cur.next = x; cur =cur.next; if(c1!=null) c1 = c1.next; if(c2!=null) c2 = c2.next; } if(carryover > 0) { ListNode x = new ListNode(carryover); cur.next = x; } return res.next; }
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 = beforestart==null?head:beforestart.next; ListNode end = start; curr = start.next; ListNode oldNext = curr.next; for(int i=0; i<n-m; i++) { oldNext = curr.next; curr.next = start; start = curr; curr = oldNext; } end.next = oldNext; if(beforestart!= null) beforestart.next = start; return (beforestart== null?start:head); }
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.next =curr; left = left.next; }else { right.next = curr; right = right.next; } curr = curr.next; } left.next = dummyright.next; right.next = null; return dummyleft.next; }
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(cur.next != null) { if(currentValue != cur.next.val) { if(isDup) prev.next = cur.next; else prev=cur; //prev currentValue = cur.next.val; isDup = false; }else { isDup = true; } cur = cur.next; } if(isDup) prev.next = null; return dummy.next; }
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 = head; for(int i=1; i<length-n; i++) { cur = cur.next; } start = cur.next; cur.next = null; return start; }
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.add(null); int k = temp.size() - n-1; temp.get(k-1).next = temp.get(k+1); return dummy.next; }
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; cur.next.next = temp; cur=cur.next.next; } return dummy.next; }
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; for(int i=1; i<=k; i++) { if(curInner != null) curInner = curInner.next; else { startreverse=false; break; } } curInner = cur; if(startreverse) { ListNode temp = null; ListNode start = cur; for(int i=1; i<k; i++) { temp = curInner.next.next; curInner.next.next = start; start = curInner.next; curInner.next = temp; } prev.next = start; prev = curInner; } //index++; cur=curInner.next; } return dummy.next; }
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.next; } return false; }
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)) return curr.next; lists.add(curr.next); curr=curr.next; } return null; }
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; while(i < (count+1)/2) { curr1=curr1.next; i++; } second = curr1.next; curr1.next = null; //revert the second ListNode dummy2 = new ListNode(-1); dummy2.next = second; ListNode curr2 = second.next; while(curr2 != null) { ListNode x = curr2.next; second.next = curr2.next; curr2.next = dummy2.next; dummy2.next = curr2; curr2 = x; } //merge ListNode dummy = new ListNode(-1); ListNode curr = dummy; curr1 = head; curr2 = dummy2.next; while(curr1 != null && curr2 != null) { ListNode x =curr1.next; ListNode y =curr2.next; curr.next = curr1; curr1.next = curr2; curr = curr2; curr1 = x; curr2 = y; } if(curr1 != null) curr.next = curr1; if(curr2 != null) curr.next = curr2; head = dummy.next; }
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; curr = newnode.next; } curr = head.next; while(curr!=null) { if(curr.random!=null) curr.random = curr.random.next; if(curr.next!=null) curr = curr.next.next; else curr = null; } RandomListNode copyHead = new RandomListNode(-1); RandomListNode copyCur = copyHead; RandomListNode srcHead = new RandomListNode(-1); RandomListNode srcCur = srcHead; curr = head; while(curr!=null && curr.next!= null) { srcCur.next = curr; copyCur.next = curr.next; srcCur =srcCur.next; copyCur = copyCur.next; curr.next = curr.next.next; curr = curr.next; } head = srcHead.next; return copyHead.next; }