Skip to content

Commit 4393eb4

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent b3be3f0 commit 4393eb4

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@
347347

348348
[200. Number of Islands](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/200.%20Number%20of%20Islands.md)
349349

350+
[201. Bitwise AND of Numbers Range](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/201.%20Bitwise%20AND%20ofvNumbers%20Range.md)
351+
352+
[202. Happy Number](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/202.%20Happy%20Number.md)
353+
354+
[203. Remove Linked List Elements](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/203.%20Remove%20Linked%20List%20Elements.md)
355+
350356
## Algorithm(4th_Edition)
351357
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
352358
All java realization codes are placed in different packages.

leetcode/201. Bitwise AND of Numbers Range.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,20 @@ class Solution {
3434
}
3535
```
3636

37+
### 二刷
38+
```Java
39+
class Solution {
40+
public int rangeBitwiseAnd(int m, int n) {
41+
int mask = ~0;
42+
while(m != n){
43+
m &= mask;
44+
n &= mask;
45+
mask <<= 1;
46+
}
47+
return m;
48+
}
49+
}
50+
```
51+
3752
### Reference
3853
1.[[LeetCode] Bitwise AND of Numbers Range 数字范围位相与](https://www.cnblogs.com/grandyang/p/4431646.html)

leetcode/202. Happy Number.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,35 @@ class Solution {
4444
}
4545
}
4646
}
47+
```
48+
49+
### 二刷
50+
1. 二刷使用了循环,还是要用一个set去存储已经访问的数字。
51+
```Java
52+
class Solution {
53+
public boolean isHappy(int n) {
54+
if(n <= 0) return false;
55+
Set<Integer> visited = new HashSet<>();
56+
List<Integer> temp = null;
57+
while(!visited.contains(n)){
58+
visited.add(n);
59+
temp = new ArrayList<>();
60+
getDigits(temp, n);
61+
int count = 0;
62+
for(Integer d : temp){
63+
count += d * d;
64+
}
65+
if(count == 1) return true;
66+
n = count;
67+
}
68+
return false;
69+
}
70+
71+
private void getDigits(List<Integer> list, int n){
72+
while(n != 0){
73+
list.add(n % 10);
74+
n /= 10;
75+
}
76+
}
77+
}
4778
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
## 203. Remove Linked List Elements
2+
3+
### Question
4+
Remove all elements from a linked list of integers that have value val.
5+
6+
Example:
7+
```
8+
Input: 1->2->6->3->4->5->6, val = 6
9+
Output: 1->2->3->4->5
10+
```
11+
12+
### 二刷:
13+
* Method 1: 创建一个新的结点,如果符合要求则向这个结点后添加。
14+
```Java
15+
/**
16+
* Definition for singly-linked list.
17+
* public class ListNode {
18+
* int val;
19+
* ListNode next;
20+
* ListNode(int x) { val = x; }
21+
* }
22+
*/
23+
class Solution {
24+
public ListNode removeElements(ListNode head, int val) {
25+
ListNode dummy = new ListNode(0);
26+
ListNode dummyCur = dummy;
27+
ListNode cur = head;
28+
while(cur != null){
29+
if(cur.val != val){
30+
dummyCur.next = cur;
31+
dummyCur = dummyCur.next;
32+
}
33+
cur = cur.next;
34+
}
35+
dummyCur.next = null;
36+
return dummy.next;
37+
}
38+
}
39+
```
40+
41+
* Method 2: 原地去除结点,所以要记录前结点。
42+
```Java
43+
/**
44+
* Definition for singly-linked list.
45+
* public class ListNode {
46+
* int val;
47+
* ListNode next;
48+
* ListNode(int x) { val = x; }
49+
* }
50+
*/
51+
class Solution {
52+
public ListNode removeElements(ListNode head, int val) {
53+
ListNode dummy = new ListNode(0);
54+
dummy.next = head;
55+
ListNode pre = dummy;
56+
ListNode cur = head;
57+
while(cur != null){
58+
while(cur != null && cur.val == val){
59+
cur = cur.next;
60+
}
61+
pre.next = cur;
62+
if(cur == null) return dummy.next;
63+
pre = pre.next;
64+
cur = cur.next;
65+
}
66+
return dummy.next;
67+
}
68+
}
69+
```

0 commit comments

Comments
 (0)