Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b6af50

Browse files
feat: add solutions to lcof2 problems: No.004,005,006
1 parent acb1e14 commit 8b6af50

File tree

13 files changed

+281
-4
lines changed

13 files changed

+281
-4
lines changed

‎lcof2/剑指 Offer II 002. 二进制加法/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44-
模拟笔算加法的过程,注意进位
44+
统计所有数字每个位中 1 出现的次数,对于某个位,1 出现的次数一定是 3 的倍数 +1 或 0。对这个数 %3 得到的结果就是那个出现一次的数字在该位上的值。
4545

4646
<!-- tabs:start -->
4747

‎lcof2/剑指 Offer II 004. 只出现一次的数字/README.md‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,66 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
一位一位计算出结果
49+
4850
<!-- tabs:start -->
4951

5052
### **Python3**
5153

5254
<!-- 这里可写当前语言的特殊实现逻辑 -->
5355

5456
```python
55-
57+
class Solution:
58+
def singleNumber(self, nums: List[int]) -> int:
59+
ans = 0
60+
for i in range(0, 32):
61+
cnt = sum(num >> i & 1 for num in nums)
62+
if cnt % 3:
63+
if i == 31:
64+
ans -= 1 << i
65+
else:
66+
ans |= 1 << i
67+
return ans
5668
```
5769

5870
### **Java**
5971

6072
<!-- 这里可写当前语言的特殊实现逻辑 -->
6173

6274
```java
75+
class Solution {
76+
public int singleNumber(int[] nums) {
77+
int ans = 0;
78+
for (int i = 0; i < 32; i++) {
79+
int cnt = 0;
80+
for (int num : nums) {
81+
cnt += num >> i & 1;
82+
}
83+
cnt %= 3;
84+
ans |= cnt << i;
85+
}
86+
return ans;
87+
}
88+
}
89+
```
6390

91+
### **Go**
92+
93+
需要注意 Golang 中的 `int` 在 64 位平台上相当于 `int64`
94+
95+
```go
96+
func singleNumber(nums []int) int {
97+
ans := int32(0)
98+
for i := 0; i < 32; i++ {
99+
cnt := int32(0)
100+
for _, num := range nums {
101+
cnt += int32(num) >> i & 1
102+
}
103+
cnt %= 3
104+
ans |= cnt << i
105+
}
106+
return int(ans)
107+
}
64108
```
65109

66110
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func singleNumber(nums []int) int {
2+
ans := int32(0)
3+
for i := 0; i < 32; i++ {
4+
cnt := int32(0)
5+
for _, num := range nums {
6+
cnt += int32(num) >> i & 1
7+
}
8+
cnt %= 3
9+
ans |= cnt << i
10+
}
11+
return int(ans)
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int singleNumber(int[] nums) {
3+
int ans = 0;
4+
for (int i = 0; i < 32; i++) {
5+
int cnt = 0;
6+
for (int num : nums) {
7+
cnt += num >> i & 1;
8+
}
9+
cnt %= 3;
10+
ans |= cnt << i;
11+
}
12+
return ans;
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def singleNumber(self, nums: List[int]) -> int:
3+
ans = 0
4+
for i in range(0, 32):
5+
cnt = sum(num >> i & 1 for num in nums)
6+
if cnt % 3:
7+
if i == 31:
8+
ans -= 1 << i
9+
else:
10+
ans |= 1 << i
11+
return ans

‎lcof2/剑指 Offer II 005. 单词长度的最大乘积/README.md‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,85 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
因为只有 26 个小写字符,所以可以用一个 `int32` 存储字符的出现情况,然后枚举最大乘积
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
5557

5658
<!-- 这里可写当前语言的特殊实现逻辑 -->
5759

5860
```python
59-
61+
class Solution:
62+
def maxProduct(self, words: List[str]) -> int:
63+
n = len(words)
64+
mask = [0 for _ in range(n)]
65+
for i, word in enumerate(words):
66+
for ch in word:
67+
mask[i] |= 1 << (ord(ch) - ord('a'))
68+
ans = 0
69+
for i in range(0, n - 1):
70+
for j in range(i + 1, n):
71+
if mask[i] & mask[j] == 0:
72+
ans = max(ans, len(words[i]) * len(words[j]))
73+
return ans
6074
```
6175

6276
### **Java**
6377

6478
<!-- 这里可写当前语言的特殊实现逻辑 -->
6579

6680
```java
81+
class Solution {
82+
public int maxProduct(String[] words) {
83+
int n = words.length;
84+
int[] mask = new int[n];
85+
for (int i = 0; i < n; i++) {
86+
for (char ch : words[i].toCharArray()) {
87+
mask[i] |= 1 << (ch - 'a');
88+
}
89+
}
90+
int ans = 0;
91+
for (int i = 0; i < n - 1; i++) {
92+
for (int j = i + 1; j < n; j++) {
93+
if ((mask[i] & mask[j]) == 0) {
94+
ans = Math.max(ans, words[i].length() * words[j].length());
95+
}
96+
}
97+
}
98+
return ans;
99+
}
100+
}
101+
```
67102

103+
### **Go**
104+
105+
```go
106+
func maxProduct(words []string) int {
107+
n := len(words)
108+
mask := make([]int32, n)
109+
for i, word := range words {
110+
for _, r := range word {
111+
mask[i] |= 1 << (r - 'a')
112+
}
113+
}
114+
ans := 0
115+
for i := 0; i < n-1; i++ {
116+
for j := i + 1; j < n; j++ {
117+
if mask[i]&mask[j] == 0 {
118+
ans = max(ans, len(words[i])*len(words[j]))
119+
}
120+
}
121+
}
122+
return ans
123+
}
124+
125+
func max(a, b int) int {
126+
if a > b {
127+
return a
128+
}
129+
return b
130+
}
68131
```
69132

70133
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func maxProduct(words []string) int {
2+
n := len(words)
3+
mask := make([]int32, n)
4+
for i, word := range words {
5+
for _, r := range word {
6+
mask[i] |= 1 << (r - 'a')
7+
}
8+
}
9+
ans := 0
10+
for i := 0; i < n-1; i++ {
11+
for j := i + 1; j < n; j++ {
12+
if mask[i]&mask[j] == 0 {
13+
ans = max(ans, len(words[i])*len(words[j]))
14+
}
15+
}
16+
}
17+
return ans
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int maxProduct(String[] words) {
3+
int n = words.length;
4+
int[] mask = new int[n];
5+
for (int i = 0; i < n; i++) {
6+
for (char ch : words[i].toCharArray()) {
7+
mask[i] |= 1 << (ch - 'a');
8+
}
9+
}
10+
int ans = 0;
11+
for (int i = 0; i < n - 1; i++) {
12+
for (int j = i + 1; j < n; j++) {
13+
if ((mask[i] & mask[j]) == 0) {
14+
ans = Math.max(ans, words[i].length() * words[j].length());
15+
}
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maxProduct(self, words: List[str]) -> int:
3+
n = len(words)
4+
mask = [0 for _ in range(n)]
5+
for i, word in enumerate(words):
6+
for ch in word:
7+
mask[i] |= 1 << (ord(ch) - ord('a'))
8+
ans = 0
9+
for i in range(0, n - 1):
10+
for j in range(i + 1, n):
11+
if mask[i] & mask[j] == 0:
12+
ans = max(ans, len(words[i]) * len(words[j]))
13+
return ans

‎lcof2/剑指 Offer II 006. 排序数组中两个数字之和/README.md‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,62 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
双指针
59+
5860
<!-- tabs:start -->
5961

6062
### **Python3**
6163

6264
<!-- 这里可写当前语言的特殊实现逻辑 -->
6365

6466
```python
65-
67+
class Solution:
68+
def twoSum(self, numbers: List[int], target: int) -> List[int]:
69+
i, j = 0, len(numbers) - 1
70+
while True:
71+
if numbers[i] + numbers[j] < target:
72+
i += 1
73+
elif numbers[i] + numbers[j] > target:
74+
j -= 1
75+
else:
76+
return [i, j]
6677
```
6778

6879
### **Java**
6980

7081
<!-- 这里可写当前语言的特殊实现逻辑 -->
7182

7283
```java
84+
class Solution {
85+
public int[] twoSum(int[] numbers, int target) {
86+
int i = 0, j = numbers.length - 1;
87+
for (;;) {
88+
if (numbers[i] + numbers[j] < target) {
89+
i++;
90+
} else if (numbers[i] + numbers[j] > target) {
91+
j--;
92+
} else {
93+
return new int[]{i, j};
94+
}
95+
}
96+
}
97+
}
98+
```
7399

100+
### **Go**
101+
102+
```go
103+
func twoSum(numbers []int, target int) []int {
104+
for i, j := 0, len(numbers)-1; ; {
105+
if numbers[i]+numbers[j] < target {
106+
i++
107+
} else if numbers[i]+numbers[j] > target {
108+
j--
109+
} else {
110+
return []int{i, j}
111+
}
112+
}
113+
}
74114
```
75115

76116
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /