Skip to content

Commit bb7d16e

Browse files
committed
start review
1 parent 4089fef commit bb7d16e

6 files changed

Lines changed: 486 additions & 245 deletions

File tree

Java/292. Nim Game.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
著名Nim游戏
2+
写一些发现n=4,5,6,7,8...etc之后的情况有规律性
3+
最终很简单n%4!=0就可以了
4+
```
5+
/*
6+
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
7+
8+
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
9+
10+
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
11+
12+
Hint:
13+
14+
If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?
15+
16+
17+
Hide Similar Problems (M) Flip Game II
18+
19+
*/
20+
21+
22+
/*
23+
Thoughts:
24+
If n = 4, we can do the following:
25+
1 0 0 0
26+
1 1 0 0
27+
1 1 1 0
28+
But we'll fail.
29+
30+
n = 5, we pick 1, 2nd player gets n = 4.
31+
n = 6, we pick 2, 2nd player gets n = 4.
32+
n = 7, we pick 3, 2nd player gets n = 4.
33+
n = 8, regarless whatever we pick, the opponent can make 1st gets n = 4, we fail.
34+
...
35+
...
36+
whenever n % 4 = 0, 1st player fail.
37+
38+
*/
39+
40+
public class Solution {
41+
public boolean canWinNim(int n) {
42+
return n % 4 != 0;
43+
}
44+
}
45+
```

Java/Happy Number.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Write an algorithm to determine if a number is happy.
3+
4+
A happy number is a number defined by the following process:
5+
Starting with any positive integer, replace the number by the sum of the squares of its digits,
6+
and repeat the process until the number equals 1 (where it will stay),
7+
or it loops endlessly in a cycle which does not include 1.
8+
Those numbers for which this process ends in 1 are happy numbers.
9+
10+
Example
11+
19 is a happy number
12+
13+
1^2 + 9^2 = 82
14+
8^2 + 2^2 = 68
15+
6^2 + 8^2 = 100
16+
1^2 + 0^2 + 0^2 = 1
17+
Tags Expand
18+
Hash Table Mathematics
19+
*/
20+
21+
/*
22+
Thoughts:
23+
Try some examples then find out: if it's not happy number, the 'sum of square of its digits' will
24+
repeatedly occur. Use hashset to track existance.
25+
*/
26+
public class Solution {
27+
public boolean isHappy(int n) {
28+
if (n <= 0) {
29+
return false;
30+
}
31+
long sum = n;
32+
HashSet<Long> set = new HashSet<Long>();
33+
while (sum != 1) {
34+
String s = String.valueOf(sum);
35+
sum = 0;
36+
for (char c : s.toCharArray()){
37+
sum += (c-'0')*(c-'0');
38+
}
39+
if (set.contains(sum)) {
40+
return false;
41+
} else {
42+
set.add(sum);
43+
}
44+
}
45+
return true;
46+
}
47+
}
48+
49+
50+
51+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
两个Queue,交互倒水
2+
用一个Temp做swap
3+
4+
做法1:
5+
逻辑在top()/pop(), 每次换水查看末尾项.
6+
7+
做法2:
8+
逻辑在push里面:
9+
1. x 放q2
10+
2. q1全部offer/append到q2.
11+
3. 用一个Temp做swap q1, q2.
12+
q1的头就一直是最后加进去的值.
13+
```
14+
/*
15+
Implement Stack by Two Queues
16+
17+
Implement a stack by two queues. The queue is first in first out (FIFO).
18+
That means you can not directly pop the last element in a queue.
19+
20+
Have you met this question in a real interview? Yes
21+
Example
22+
push(1)
23+
pop()
24+
push(2)
25+
isEmpty() // return false
26+
top() // return 2
27+
pop()
28+
isEmpty() // return true
29+
Tags Expand
30+
Stack Queue
31+
32+
*/
33+
34+
/*
35+
Thoughts:
36+
2 queue are like two cups. We are fliping water into/out between q1 and q2.
37+
pop and top are fliping water.
38+
Use p1 as the base.
39+
*/
40+
41+
class Stack {
42+
private Queue<Integer> q1 = new LinkedList<Integer>();
43+
private Queue<Integer> q2 = new LinkedList<Integer>();
44+
// Push a new item into the stack
45+
public void push(int x) {
46+
q1.offer(x);
47+
}
48+
49+
// Pop the top of the stack
50+
public void pop() {
51+
while (q1.size() > 1) {
52+
q2.offer(q1.poll());
53+
}
54+
q1.poll();
55+
swap();
56+
}
57+
58+
// Return the top of the stack
59+
public int top() {
60+
while (q1.size() > 1) {
61+
q2.offer(q1.poll());
62+
}
63+
int rst = q1.poll();
64+
q2.offer(rst);
65+
swap();
66+
return rst;
67+
}
68+
69+
public void swap(){
70+
Queue<Integer> temp = q1;
71+
q1 = q2;
72+
q2 = temp;
73+
}
74+
75+
// Check the stack is empty or not.
76+
public boolean isEmpty() {
77+
return q1.isEmpty();
78+
}
79+
}
80+
```

Java/Implement Stack.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
stack 后入先出.
2+
Data Structure: ArrayList
3+
return/remove ArrayList的末尾项
4+
5+
```
6+
/*
7+
Implement Stack
8+
9+
Implement a stack. You can use any data structure inside a stack except stack itself to implement it.
10+
11+
12+
Example
13+
push(1)
14+
pop()
15+
push(2)
16+
top() // return 2
17+
pop()
18+
isEmpty() // return true
19+
push(3)
20+
isEmpty() // return false
21+
Tags Expand
22+
Array Stack
23+
*/
24+
25+
/*
26+
Thoughts:
27+
use arraylist and a index tracker - leng
28+
push: add to end
29+
pop: remove end
30+
top: get end.
31+
isEmpty: return length
32+
*/
33+
34+
class Stack {
35+
private ArrayList<Integer> list = new ArrayList<Integer>();
36+
// Push a new item into the stack
37+
public void push(int x) {
38+
list.add(x);
39+
}
40+
41+
// Pop the top of the stack
42+
public void pop() {
43+
if (list.size() > 0) {
44+
list.remove(list.size() - 1);
45+
}
46+
}
47+
48+
// Return the top of the stack
49+
public int top() {
50+
if (list.size() > 0) {
51+
return list.get(list.size() - 1);
52+
}
53+
return -1;
54+
}
55+
56+
// Check the stack is empty or not.
57+
public boolean isEmpty() {
58+
return list.size() == 0;
59+
}
60+
}
61+
```

0 commit comments

Comments
 (0)