http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=290480&highlight=zenefits
http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=287729&highlight=zenefits
Print Diamond
没想到巧妙的办法
public class PrintSymbol {
// Question1: print
// * 0 - 1 6
// *** 1 - 3 4
// ***** 2 - 5 2
// *******3 - 7 0
// ***** 4 - 5 2
// *** 5 - 3 4
// * 6 - 1 6
public static void printSymbol(int row) {
int max = row * 2 - 1;
int print;
int space;
int print2;
int space2 = 0;
for (int i = 0; i < row * 2 - 1; ++i) {
if (i < row) {
print = i * 2 + 1;
space = (max - print) / 2;
for (int j = 0; j < space; ++j) {
System.out.print(" ");
}
for (int j = 0; j < print; ++j) {
System.out.print("*");
}
for (int j = 0; j < space; ++j) {
System.out.print(" ");
}
System.out.println();
} else {
space2 += 1;
print2 = max - space2 * 2;
for (int j = 0; j < space2; ++j) {
System.out.print(" ");
}
for (int j = 0; j < print2; ++j) {
System.out.print("*");
}
for (int j = 0; j < space2; ++j) {
System.out.print(" ");
}
System.out.println();
}
}
}
public static void main(String[] args) {
printSymbol(3);
}
}
635. Design Log Storage System
class LogSystem {
List<String[]> timestamps;
List<String> time = Arrays.asList("Year", "Month", "Day", "Hour", "Minute", "Second");
int[] indices = {4, 7, 10, 13, 16, 19};
public LogSystem() {
timestamps = new LinkedList<>();
}
public void put(int id, String timestamp) {
timestamps.add(new String[] {Integer.toString(id), timestamp});
}
public List<Integer> retrieve(String s, String e, String gra) {
List<Integer> result = new LinkedList<>();
int index = indices[time.indexOf(gra)];
for (String[] timestamp : timestamps) {
if (timestamp[1].substring(0, index).compareTo(s.substring(0, index)) >= 0
&& timestamp[1].substring(0, index).compareTo(e.substring(0, index)) <= 0) result.add(Integer.parseInt(timestamp[0]));
}
return result;
}
}
409. Longest Palindrome
class Solution {
public int longestPalindrome(String s) {
int[] hash = new int[256];
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; ++i) {
hash[chars[i]]++;
}
int count = 0;
boolean flag = false;
for (int i = 0; i < hash.length; ++i) {
if (hash[i] != 0) {
if (hash[i] % 2 == 0) {
count += hash[i];
} else {
count += hash[i] - 1;
flag = true;
}
}
}
return flag ? count + 1 : count;
}
}
155. Min Stack
public class minStack {
Stack<Integer> stack;
Stack<Integer> minStack;
/**
* initialize your data structure here.
*/
public void MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int x) {
stack.push(x);
if (minStack.isEmpty()) {
minStack.push(x);
} else minStack.push(Math.min(x, minStack.peek()));
}
public void pop() {
stack.pop();
minStack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}