==> we can assign -1 to each number in the array
use it's value as index, so the position it pointed to be assigned as positive, if we found one that pointing to the positive value, we have already found the duplication element.
# Find if a tree is a mirror of itself
==>Use a variant of recursive function for checking sameTree() instead of checking left and left, right and right. Check left with right and right with left.
# An array contain +ve and -ve element, find subarray whose sum =0;
==> Traverse the array,
at position i, recored the up-to-now sum --> put this into a hash table (key = the up-to now sum), value = current position
keep on travers, if to some posiiton j, we found the same value in the hash table, then the
subarray from i to j is the subarray we are looking for
e.g. we have the array
-3, -1, 3, -3, -11, -1, 10
we could generate the hash table by the up-to-now sum:
-3, -4, -1, -4
when face another -4, it mens that during the element -1, to the 4th element -3, there is no increasing, so the sum of this sub-array is ZERO
--
♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥
No comments:
Post a Comment