




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































1 Rotate Array in Java
1.1 Solution 1 - Intermediate Array
public void rotate(int[] nums, int k) { if(k > nums.length) k=k%nums.length;
int[] result = new int[nums.length];
for(int i=0; i < k; i++){ result[i] = nums[nums.length-k+i]; }
int j=0; for(int i=k; i<nums.length; i++){ result[i] = nums[j]; j++; }
System.arraycopy( result, 0, nums, 0, nums.length ); }
1.2 Solution 2 - Bubble Rotate
public static void rotate(int[] arr, int order) { if (arr == null || order < 0) {
throw new IllegalArgumentException("Illegal argument!"); }
for (int i = 0; i < order; i++) { for (int j = arr.length - 1; j > 0; j--) { int temp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = temp; } } }
i= 0 1 2 3 4 5 6 0 1 2 3 4 6 5 ... 6 0 1 2 3 4 5 i= 6 0 1 2 3 5 4 ... 5 6 0 1 2 3 4 i= 5 6 0 1 2 4 3 ... 4 5 6 0 1 2 3
1.3 Solution 3 - Reversal
public static void rotate(int[] arr, int order) { if (arr == null || arr.length==0 || order < 0) { throw new IllegalArgumentException("Illegal argument!"); }
if(order > arr.length){ order = order %arr.length; }
2 Evaluate Reverse Polish Notation
2.1 Naive Approach
public class Test {
public static void main(String[] args) throws IOException { String[] tokens = new String[] { "2", "1", "+", "3", "*" }; System.out.println(evalRPN(tokens)); }
2 Evaluate Reverse Polish Notation
public static int evalRPN(String[] tokens) { int returnValue = 0; String operators = "+-*/";
Stack
for (String t : tokens) { if (!operators.contains(t)) { //push to stack if it is a number stack.push(t); } else {//pop numbers from stack if it is an operator int a = Integer.valueOf(stack.pop()); int b = Integer.valueOf(stack.pop()); switch (t) { case "+": stack.push(String.valueOf(a + b)); break; case "-": stack.push(String.valueOf(b - a)); break; case "*": stack.push(String.valueOf(a (^) * b)); break; case "/": stack.push(String.valueOf(b / a)); break; } } }
returnValue = Integer.valueOf(stack.pop());
return returnValue; } }
2.2 Accepted Solution
public class Solution { public int evalRPN(String[] tokens) {
int returnValue = 0;
3 Isomorphic Strings
3.1 Analysis
3.2 Java Solution
public boolean isIsomorphic(String s, String t) { if(s==null || t==null) return false;
if(s.length() != t.length()) return false;
if(s.length()==0 && t.length()==0) return true;
HashMap<Character, Character> map = new HashMap<Character,Character>(); for(int i=0; i<s.length(); i++){ char c1 = s.charAt(i); char c2 = t.charAt(i);
Character c = getKey(map, c2); if(c != null && c!=c1){ return false; }else if(map.containsKey(c1)){ if(c2 != map.get(c1)) return false; }else{ map.put(c1,c2); } }
return true; }
3 Isomorphic Strings
// a method for getting key of a target value public Character getKey(HashMap<Character,Character> map, Character target){ for (Map.Entry<Character,Character> entry : map.entrySet()) { if (entry.getValue().equals(target)) { return entry.getKey(); } }
return null; }
4 Word Ladder
public class Solution { public int ladderLength(String beginWord, String endWord, Set
wordDict.add(endWord);
while(!queue.isEmpty()){ WordNode top = queue.remove(); String word = top.word;
if(word.equals(endWord)){ return top.numSteps; }
char[] arr = word.toCharArray(); for(int i=0; i<arr.length; i++){ for(char c=’a’; c<=’z’; c++){ char temp = arr[i]; if(arr[i]!=c){ arr[i]=c; }
String newWord = new String(arr); if(wordDict.contains(newWord)){ queue.add(new WordNode(newWord, top.numSteps+1)); wordDict.remove(newWord); }
arr[i]=temp; } } }
return 0; } }
5 Word Ladder II
["hit","hot","dot","dog","cog"], ["hit","hot","lot","log","cog"] ]
5.1 Analysis
5.2 Java Solution
class WordNode{ String word; int numSteps; WordNode pre;
public WordNode(String word, int numSteps, WordNode pre){ this.word = word; this.numSteps = numSteps; this.pre = pre; } }
public class Solution { public List<List
String newWord = new String(arr); if(unvisited.contains(newWord)){ queue.add(new WordNode(newWord, top.numSteps+1, top)); visited.add(newWord); }
arr[i]=temp; } }
return result; } }
6 Median of Two Sorted Arrays
6.1 Java Solution 1
public static double findMedianSortedArrays(int A[], int B[]) { int m = A.length; int n = B.length;
if ((m + n) % 2 != 0) // odd return (double) findKth(A, B, (m + n) / 2, 0, m - 1, 0, n - 1); else { // even return (findKth(A, B, (m + n) / 2, 0, m - 1, 0, n - 1)
public static int findKth(int A[], int B[], int k, int aStart, int aEnd, int bStart, int bEnd) {
int aLen = aEnd - aStart + 1; int bLen = bEnd - bStart + 1;
// Handle special cases if (aLen == 0) return B[bStart + k]; if (bLen == 0) return A[aStart + k]; if (k == 0) return A[aStart] < B[bStart]? A[aStart] : B[bStart];
int aMid = aLen (^) * k / (aLen + bLen); // a’s middle count int bMid = k - aMid - 1; // b’s middle count