The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return[0,1,3,2]. Its gray code sequence is:
00 - 0 01 - 1 11 - 3 10 - 2
Note:
For a given n, a gray code sequence is not uniquely defined.
For example,[0,2,3,1]is also a valid gray code sequence according to the above definition.
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.
public static ArrayList<Integer> grayCode(int n) {
ArrayList<Integer> result = new ArrayList<>();
if(n == 0) {
result.add(0);
return result;
}
LinkedList<String> temp = new LinkedList<>();
temp.offer("0");
temp.offer("1");
for(int i = 0; i < n - 1; i++) {
Stack<String> temp1 = new Stack<>();
for(int j = 0; j < Math.pow(2, i + 2 - 1); j++) {
String str1 = temp.poll();
temp.offer("0" + str1);
temp1.push("1" + str1);
}
while(!temp1.isEmpty()) {
temp.offer(temp1.pop());
}
}
while(!temp.isEmpty()) {
Double num = 0D;
char[] chars = temp.poll().toCharArray();
for(Integer i = chars.length - 1; i >= 0; i--) {
if(chars[i] == '1') {
num += Math.pow(2, chars.length - 1 - i);
}
}
result.add(num.intValue());
}
return result;
}
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
public int run(TreeNode root) {
if(root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedBlockingDeque<>();
queue.add(root);
Integer step = 0;
Integer start = 0;
Integer end = 1;
Integer nextEnd = 0;
while(queue.size() > 0) {
Boolean isLast =false;
start++;
if(start == end) {
step++;
start = 0;
end = nextEnd;
nextEnd = 0;
isLast = true;
}
TreeNode node = queue.poll();
if(node.left != null) {
queue.add(node.left);
if(isLast) {
end++;
} else {
nextEnd++;
}
}
if(node.right != null) {
queue.add(node.right);
if(isLast) {
end++;
} else {
nextEnd++;
}
}
if(node.left == null && node.right == null) {
if(isLast) {
return step;
} else {
return ++step;
}
}
}
return step;
}
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are+,-,*,/. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for(Integer num = 0; num < tokens.length; num++) {
if(!tokens[num].equals("+") && !tokens[num].equals("-") && !tokens[num].equals("*") && !tokens[num].equals("/")) {
stack.push(Integer.valueOf(tokens[num]));
} else {
Integer second = stack.pop();
Integer first = stack.pop();
if(tokens[num].equals("+")) {
stack.push(first + second);
}
if(tokens[num].equals("-")) {
stack.push(first - second);
}
if(tokens[num].equals("*")) {
stack.push(first * second);
}
if(tokens[num].equals("/")) {
stack.push(first / second);
}
}
}
return stack.pop();
}
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
public int maxPoints(Point[] points) {
if(points == null || points.length == 0) return 0;
if(points.length == 1) return 1;
Double k;
int result = Integer.MIN_VALUE;
for(int i = 0; i < points.length; i++) {
HashMap<Double, Integer> map = new HashMap<Double, Integer>();
int curMax = 1;
int dup = 0;
int x = points[i].x;
int y = points[i].y;
for(int j = i + 1; j < points.length; j++) {
int num = 1;
if(x == points[j].x && y == points[j].y) {
dup++;
} else {
if(x != points[j].x) {
if(y == points[j].y) {
k = 0.0;
} else {
k = 1.0*(y - points[j].y) / (x - points[j].x);
}
} else {
k = Double.MAX_VALUE;
}
if(map.get(k) != null) {
num = map.get(k) + 1;
} else {
num = 2;
}
map.put(k, num);
}
curMax = Math.max(curMax, num);
}
curMax += dup;
result = Math.max(result, curMax);
}
return result;
}
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
public int singleNumber(int[] A) {
Integer result = 0;
for(int a : A) {
result = result ^ a ;
}
return result;
}
Say you have an array for which the i th element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
public static int maxProfit(int[] prices) {
if (prices.length < 2) return 0;
int result = 0;
int index = 0;
boolean goon = true;
while(goon) {
int start = prices[index];
while(index < prices.length - 1 && prices[index] <= prices[index + 1]) {
index++;
}
result += prices[index] - start;
index++;
if(index >= prices.length - 1) {
goon = false;
}
}
return result;
}