XPath for minimum
/ama/e/@v[not(. > ../../e/@v)]
XPath for Maximum
/ama/e/@v[not(. < ../../e/@v)]
Quick tips or notes that probably reflects 20 percent of knowledge that usually does 80 percent of job.
void reverseString( char str[], int start, int end ){ char temp; while( end > start ){ /* Exchange characters */ temp = str[start]; str[start] = str[end]; str[end] = temp; /* Move indices towards middle */ start++; end--; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public static int getLevels(TreeNode aTree){ if (aTree == null)return 0; int levels = 0; Queue<TreeNode> curLevel = new LinkedList<TreeNode>(); Queue<TreeNode> nextLevel = new LinkedList<TreeNode>(); //starting from root, level 0 will become to 1 after root processing curLevel.add(aTree); while(!curLevel.isEmpty()){ TreeNode curNode = curLevel.remove(); if (curNode.left!=null) nextLevel.add(curNode.left); if (curNode.right!=null) nextLevel.add(curNode.right); if(curLevel.isEmpty()){ if (nextLevel.isEmpty())break; else{ levels++; curLevel.addAll(nextLevel); nextLevel.clear(); } } } return levels; } |
public static int climbStairs(int n) { if(n==1)return 1; if(n==2)return 2; return climbStairs(n-1)+climbStairs(n-2); } public static int climbStairsIterative(int n) { if(n==1)return 1; if(n==2)return 2; int m1=1,m2=2,tmp; for(int i=3;i<=n;i++){ tmp =m1 + m2; m1=m2; m2=tmp; } return m2; }
Binomial theorem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | public static int longestSubStringAtMostTwoDistinctChar(String s){ if(s==null)return -1; if(s.isEmpty())return 0; //this set contains characters that should be in current substring Set<Character> charSet = new HashSet<Character>(); int maxLen=0,h=0,t=h; //having two pointers, one for head adjustment, one for substring tail adjustment //end of substring is marked by first character that makes set size of 3 // while (h<s.length()){ //inner loop stops while t pointing to third different character //this third character will become part of next substring //t only grows from 0 to n, when it reaches, process ends, //so this is O(n) time complexity while(t<s.length() ){ charSet.add(s.charAt(t)); //after adding a character, we need check its size to tell //if this is a third character if(charSet.size()>2)break; t++; } //now the tail is at position after the end of substring maxLen = Math.max(maxLen, t-h); //if tail is already at the end of string, then end the process if(t==s.length())return maxLen; //now adjust head pointer //e.g. abaac, it is actually easier to find the head position for next string //from tail to head. moving cursor from c back to a,a until it hits b, new start //s from a //then moving it towards head until it meets a different characer //adjacent character will be part of next substring //adjust h to t-1 the first sinnce t is now position after current substring h=t-1; while(h>0 && s.charAt(h)==s.charAt(h-1)){ h--; } //when loop ends, h is either 0 or its previous character is different //remove the character from set //it loops back to here because there must be one character need to be removed charSet.remove(s.charAt(h==0?0:h-1)); } return maxLen; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | import java.util.HashSet; import java.util.Set; public class longestNoneRepeat { public static void main(String[] args) { System.out.println(lengthOfLongestSubstringBruteForceModified("abcdefabcfabcdefhijka")); } /** * starting from a position, using inner loop to find tail still using set * to tell if character is repeated, but at start of each outter loop, set * need to be cleared Note: this is working,but inefficient. time limit * exceeded is what being reported by leetcode online judge. * * @param s * @return */ public static int lengthOfLongestSubstringBruteForce(String s) { Set<Character> charSet = new HashSet<Character>(); int i = 0, maxLen = 0; for (int j = 0; j < s.length(); j++) { charSet.clear(); charSet.add(s.charAt(j)); i = j + 1; while (i < s.length() && !charSet.contains(s.charAt(i))) { charSet.add(s.charAt(i)); i++; } // maxLen = Math.max(i-j, maxLen); maxLen = Math.max(charSet.size(), maxLen); } return maxLen; } /** * to avoid some of extra useless work. this will be better than brute * force, but extra loop of inner loop is still there. we need it do not * start over * * @param s * @return */ public static int lengthOfLongestSubstringBruteForceModified(String s) { Set<Character> charSet = new HashSet<Character>(); int j = 0, i = 0, maxLen = 0; while (j < s.length()) { charSet.clear(); charSet.add(s.charAt(j)); i = j + 1; while (i < s.length() && !charSet.contains(s.charAt(i))) { charSet.add(s.charAt(i)); i++; } // maxLen = Math.max(i-j, maxLen); maxLen = Math.max(charSet.size(), maxLen); // reset j, j should be a position after the character being // repeated if (i == s.length()) break; while (s.charAt(j) != s.charAt(i)) j++; j++; } return maxLen; } /** * accepted * * @param s * @return */ public static int lengthOfLongestSubstringBruteForceModified2(String s) { Set<Character> charSet = new HashSet<Character>(); int j = 0, i = 0, maxLen = 0; while (j < s.length()) { charSet.add(s.charAt(j)); i = j + charSet.size(); while (i < s.length() && !charSet.contains(s.charAt(i))) { charSet.add(s.charAt(i)); i++; } // maxLen = Math.max(i-j, maxLen); maxLen = Math.max(charSet.size(), maxLen); // reset j, j should be a position after the character being // repeated if (i == s.length()) break; // adjust header, remove left elements to the repeated character // leave the repeated character in the set because it should be in // next substring while (s.charAt(j) != s.charAt(i)) { charSet.remove(s.charAt(j)); j++; } // move head to next position of the repeated character j++; } return maxLen; } /** * abcdefabcfabcdefhijka 1. The way to remember occurrence of * character/element is to use a character array or set/hashtable as long as * they provide constant time to access 2. repeated or not is only * meaningful to current substring that is worked on 3. if at position j it * has a repeat, it must be repeating a character in i. how to find i? 4. * when find a duplication, it means the current substring is done, try to * modify the max length and remove the characters from set that are no * longer required in next substring's building up 5.how to find current * substring's length: j-i+1 6. how to find position of i: 6.1 with * character array: a[j]=a[i] because they are the repeated character which * is array's index. since all valid positions (building up substring * without repeated character) left to i can not build up a substring longer * than current substring, the new position for next substring is from i+1, * j can continue to move forward. when next repeated character is find, the * next substring's length is j-i+1 6.2 with set from i until character * being repeated, removing from set, this includes the repeated character. * but since repeated character at j is part on next substring, so that we * put it back to set later on 7. technique of innter loop: it uses cleaning * up status or removing from set to control how i is found 8. i is head * position of substring, j is tail position of substring * * @param s * @return */ public static int lengthOfLongestSubstringSet(String s) { Set<Character> charSet = new HashSet<Character>(); int i = 0, maxLen = 0; for (int j = 0; j < s.length(); j++) { while (charSet.contains(s.charAt(j))) { charSet.remove(s.charAt(i)); i++; } charSet.add(s.charAt(j)); maxLen = Math.max(j - i + 1, maxLen); } return maxLen; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public static TreeNode rebuildBinaryTreeRec(List<Integer> preOrder, List<Integer> inOrder){ TreeNode root = null; List<Integer> leftPreOrder; List<Integer> rightPreOrder; List<Integer> leftInorder; List<Integer> rightInorder; int inorderPos; int preorderPos; if ((preOrder.size() != 0) && (inOrder.size() != 0)) { // first element in preorder array is root root = new TreeNode(preOrder.get(0)); // Based upon the root information, manupulate preorder array to separate it into left and right subarrays // find root according to root in preorder inorderPos = inOrder.indexOf(preOrder.get(0)); leftInorder = inOrder.subList(0, inorderPos); rightInorder = inOrder.subList(inorderPos + 1, inOrder.size()); //then according to in order sub tree information, help preorder to determine the left and right tree preorderPos = leftInorder.size(); leftPreOrder = preOrder.subList(1, preorderPos + 1); rightPreOrder = preOrder.subList(preorderPos + 1, preOrder.size()); //then root's left subtree is the subtree built from left preorder and left inorder subtrees root.left = rebuildBinaryTreeRec(leftPreOrder, leftInorder); //root's right tree is the tree built from right part of preorder subtree and right part of in order subtree root.right = rebuildBinaryTreeRec(rightPreOrder, rightInorder); } //return the root of subtrees return root; } |