Zhengyang.Feng2011
BAN USERpublic static void moveZero(int[] arr) {
if (arr == null || arr.length == 0) return;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == 0) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] != 0) {
swap(arr, i, j);
break;
}
}
if (arr[i] == 0) return;
}
}
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void moveZero(int[] arr) {
if (arr == null || arr.length == 0) return;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == 0) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] != 0) {
swap(arr, i, j);
break;
}
}
if (arr[i] == 0) return;
}
}
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
two prefix tree
- Zhengyang.Feng2011 March 14, 2012I think linked list is enough, because you can calculate the index of element to be actually deleted without iterating over the whole list again and again.
In each round, if number of remaining people is greater or equal to 3, then remove the 2nd element in the linked list(index start from 0).
If number of remaining people is less than 3, then remove (n % 3 - 1)th element.
Until there is only one element left.
Am I allowed to reinvest the profit to buy stock?
- Zhengyang.Feng2011 March 13, 2012public static void preorderTraverse(Node root) {
if (root == null) return;
preorderTraverse(root.left());
System.out.print(root.val());
preorderTraverse(root.right());
}
public static int[] output(int[] arr) {
if (arr == null || arr.length == 0) return null;
int[] output = new int[arr.length];
output[0] = arr[arr.length - 1];
for (int i = 1; i < arr.length; i++) {
output[i] = output[i - 1] + arr[arr.length - i - 1];
}
return output;
}
public static void main(String[] args) {
int[] input = {2, 3, 5, 6, 6, 12, 4, 2};
int[] output = output(input);
System.out.println(Arrays.toString(output));
}
public static void mirror(Node root) {
if (root.left() == null && root.right() == null) return;
exchange(root, root.left(), root.right());
mirror(root.left());
mirror(root.right());
}
private static void exchange(Node root, Node left, Node right) {
Node temp = root.left();
root.left(root.right());
root.right(temp);
}
public static int search(int[] arr, int target) {
return search(arr, target, 0, arr.length - 1);
}
private static int search(int[] arr, int target, int start, int end) {
if (start > end) return -1;
int mid = (start + end) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] > target)
return search(arr, target, start, mid - 1);
else
return search(arr, target, mid + 1, end);
}
public static void traverse(Node node) {
if (node == null) return;
List<Node> thisLevel = new ArrayList<Node>();
thisLevel.add(node);
while (!thisLevel.isEmpty()) {
List<Node> nextLevel = new ArrayList<Node>();
for (Node n : thisLevel) {
System.out.print(n.key() + " ");
if (n.left() != null) nextLevel.add(n.left());
if (n.right() != null) nextLevel.add(n.right());
}
thisLevel = nextLevel;
System.out.println();
}
}
- Zhengyang.Feng2011 March 15, 2012