Skip to content

Commit 40e48c3

Browse files
author
Botao Xiao
committed
[Function add]
1. Add leetcode solutions.
1 parent 0afccc2 commit 40e48c3

5 files changed

+168
-5
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,12 @@
401401

402402
[228. Summary Ranges](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/228.%20Summary%20Ranges.md)
403403

404+
[229. Majority Element II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/229.%20Majority%20Element%20II.md)
405+
406+
[230. Kth Smallest Element in a BST](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/230.%20Kth%20Smallest%20Element%20in%20a%20BST.md)
407+
408+
[231. Power of Two](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/231.%20Power%20of%20Two.md)
409+
404410
## Algorithm(4th_Edition)
405411
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
406412
All java realization codes are placed in different packages.

leetcode/169. Majority Element.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,30 @@ class Solution {
9393
return save;
9494
}
9595
}
96-
```
96+
```
97+
98+
### 三刷
99+
1. Redo the method 3 however think in a different way:
100+
* when we see 2 different numbers, we will remove both of them;
101+
* when we see 2 same numbers, we will increase the number of appears of current number.
102+
* the majority number will remove all other numbers and finally leaves as a result;
103+
```Java
104+
class Solution {
105+
public int majorityElement(int[] nums) {
106+
int result = nums[0], cnt = 1;
107+
for(int i = 1; i < nums.length; i++){
108+
if(nums[i] == result){
109+
cnt++;
110+
}else{
111+
if(cnt == 0){
112+
result = nums[i];
113+
cnt = 1;
114+
}else{
115+
cnt--;
116+
}
117+
}
118+
}
119+
return result;
120+
}
121+
}
122+
```

leetcode/229. Majority Element II.md

+66-1
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,69 @@ class Solution {
8181
return result;
8282
}
8383
}
84-
```
84+
```
85+
86+
### 二刷
87+
1. Use map to save the appearance of current number and it appears times.
88+
```Java
89+
class Solution {
90+
public List<Integer> majorityElement(int[] nums) {
91+
List<Integer> result = new LinkedList<>();
92+
if(nums == null || nums.length == 0) return result;
93+
Map<Integer, Integer> map = new HashMap<>();
94+
for(Integer num : nums){
95+
map.put(num, map.containsKey(num) ? map.get(num) + 1: 1);
96+
}
97+
int cmp = nums.length / 3;
98+
Set<Map.Entry<Integer, Integer>> entrySet = map.entrySet();
99+
for(Map.Entry<Integer, Integer> entry : entrySet){
100+
if(entry.getValue() > cmp){
101+
result.add(entry.getKey());
102+
}
103+
}
104+
return result;
105+
}
106+
}
107+
```
108+
109+
2. This question is a improvement of question [169. Majority Element](https://seanforfun.github.io/leetcode/2018/12/21/169.-Majority-Element.html).
110+
* we use two counts to record the appearance number of majority.
111+
* if same, we increase the count and not same, we minus the count number.
112+
* this method will only record the most two majority numbers but won't check if appears more than L / 3.
113+
```Java
114+
class Solution {
115+
public List<Integer> majorityElement(int[] nums) {
116+
List<Integer> result = new LinkedList<>();
117+
if(nums == null || nums.length == 0) return result;
118+
else if(nums.length == 1){
119+
result.add(nums[0]);
120+
return result;
121+
}
122+
int major1 = 0, major2 = 0, cnt1 = 0, cnt2 = 0;
123+
for(int num : nums){
124+
if(num == major1){
125+
cnt1 ++;
126+
}else if(num == major2){
127+
cnt2 ++;
128+
}else if(cnt1 == 0){
129+
major1 = num;
130+
cnt1 = 1;
131+
}else if(cnt2 == 0){
132+
major2 = num;
133+
cnt2 = 1;
134+
}else{
135+
cnt1 --;
136+
cnt2 --;
137+
}
138+
}
139+
cnt1 = 0; cnt2 = 0;
140+
for(int i = 0; i < nums.length; i++){
141+
if(nums[i] == major1) cnt1++;
142+
else if(nums[i] == major2) cnt2++;
143+
}
144+
if(cnt1 > nums.length / 3) result.add(major1);
145+
if(cnt2 > nums.length / 3) result.add(major2);
146+
return result;
147+
}
148+
}
149+
```

leetcode/230. Kth Smallest Element in a BST.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,34 @@ class Solution {
5959
inorder(root.right, result, k);
6060
}
6161
}
62-
```
62+
```
63+
64+
### 二刷
65+
1. The method of this question is to add all numbers into the list by inorder traversal.
66+
2. I added another quit point for recursive so it will significantly increase the efficiency.
67+
```Java
68+
/**
69+
* Definition for a binary tree node.
70+
* public class TreeNode {
71+
* int val;
72+
* TreeNode left;
73+
* TreeNode right;
74+
* TreeNode(int x) { val = x; }
75+
* }
76+
*/
77+
class Solution {
78+
public int kthSmallest(TreeNode root, int k) {
79+
List<Integer> result = new LinkedList<>();
80+
inorder(result, root, k);
81+
return result.get(k - 1);
82+
}
83+
private void inorder(List<Integer> result, TreeNode node, int k){
84+
if(node == null || result.size() >= k){
85+
return;
86+
}
87+
inorder(result, node.left, k);
88+
result.add(node.val);
89+
inorder(result, node.right, k);
90+
}
91+
}
92+
```

leetcode/231. Power of Two.md

+38-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Given an integer, write a function to determine if it is a power of two.
77
Example 1:
88
99
Input: 1
10-
Output: true
10+
Output: true
1111
Explanation: 20 = 1
1212
1313
Example 2:
@@ -39,4 +39,40 @@ class Solution {
3939
return count == 1;
4040
}
4141
}
42-
```
42+
```
43+
44+
### 二刷
45+
1. I just checked if a number can be fully divided by 2.
46+
```Java
47+
class Solution {
48+
public boolean isPowerOfTwo(int n) {
49+
if(n == 1) return true;
50+
else if(n <= 0) return false;
51+
while(n != 1){
52+
if(n % 2 != 0) return false;
53+
n /= 2;
54+
}
55+
return true;
56+
}
57+
}
58+
```
59+
60+
2. bit manipulation
61+
```Java
62+
class Solution {
63+
public boolean isPowerOfTwo(int n) {
64+
if(n <= 0) return false;
65+
int temp = 0;
66+
int count = 0;
67+
for(int i = 0; i < 31; i++){
68+
temp = n & 1;
69+
if(temp == 1){
70+
count ++;
71+
if(count > 1) return false;
72+
}
73+
n >>= 1;
74+
}
75+
return true;
76+
}
77+
}
78+
```

0 commit comments

Comments
 (0)