Skip to content

Commit 0fb11f3

Browse files
committed
feat: add solutions to lcci problem: No.01.01
No.01.01.Is Unique
1 parent 8dde1d5 commit 0fb11f3

File tree

8 files changed

+151
-104
lines changed

8 files changed

+151
-104
lines changed

lcci/01.01.Is Unique/README.md

+61-41
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@
2929

3030
<!-- 这里可写通用的实现逻辑 -->
3131

32+
**方法一:位运算**
33+
3234
根据示例,可以假定字符串中只包含小写字母(实际验证,也符合假设)。
3335

34-
用 bitmap 标记小写字母是否出现过。
36+
因此,我们可以使用一个 $32$ 位整数 `mask` 的每一位来表示字符串中的每一个字符是否出现过。
37+
38+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串长度。
3539

3640
<!-- tabs:start -->
3741

@@ -42,12 +46,12 @@
4246
```python
4347
class Solution:
4448
def isUnique(self, astr: str) -> bool:
45-
bitmap = 0
49+
mask = 0
4650
for c in astr:
47-
pos = ord(c) - ord('a')
48-
if (bitmap & (1 << pos)) != 0:
51+
i = ord(c) - ord('a')
52+
if (mask >> i) & 1:
4953
return False
50-
bitmap |= 1 << pos
54+
mask |= 1 << i
5155
return True
5256
```
5357

@@ -58,74 +62,90 @@ class Solution:
5862
```java
5963
class Solution {
6064
public boolean isUnique(String astr) {
61-
int bitmap = 0;
65+
int mask = 0;
6266
for (char c : astr.toCharArray()) {
63-
int pos = c - 'a';
64-
if ((bitmap & (1 << pos)) != 0) {
67+
int i = c - 'a';
68+
if (((mask >> i) & 1) == 1) {
6569
return false;
6670
}
67-
bitmap |= (1 << pos);
71+
mask |= 1 << i;
6872
}
6973
return true;
7074
}
7175
}
7276
```
7377

74-
### **JavaScript**
78+
### **C++**
7579

76-
```js
77-
/**
78-
* @param {string} astr
79-
* @return {boolean}
80-
*/
81-
var isUnique = function (astr) {
82-
let bitmap = 0;
83-
for (let i = 0; i < astr.length; ++i) {
84-
const pos = astr[i].charCodeAt() - 'a'.charCodeAt();
85-
if ((bitmap & (1 << pos)) != 0) {
86-
return false;
80+
```cpp
81+
class Solution {
82+
public:
83+
bool isUnique(string astr) {
84+
int mask = 0;
85+
for (char c : astr) {
86+
int i = c - 'a';
87+
if (mask >> i & 1) {
88+
return false;
89+
}
90+
mask |= 1 << i;
8791
}
88-
bitmap |= 1 << pos;
92+
return true;
8993
}
90-
return true;
9194
};
9295
```
9396
9497
### **Go**
9598
9699
```go
97100
func isUnique(astr string) bool {
98-
bitmap := 0
99-
for _, r := range astr {
100-
pos := r - 'a'
101-
if (bitmap & (1 << pos)) != 0 {
101+
mask := 0
102+
for _, c := range astr {
103+
i := c - 'a'
104+
if mask>>i&1 == 1 {
102105
return false
103106
}
104-
bitmap |= (1 << pos)
107+
mask |= 1 << i
105108
}
106109
return true
107110
}
108111
```
109112

110-
### **C++**
113+
### **JavaScript**
111114

112-
```cpp
113-
class Solution {
114-
public:
115-
bool isUnique(string astr) {
116-
int bitmap = 0;
117-
for (char c : astr) {
118-
int pos = c - 'a';
119-
if ((bitmap & (1 << pos)) != 0) {
120-
return false;
121-
}
122-
bitmap |= (1 << pos);
115+
```js
116+
/**
117+
* @param {string} astr
118+
* @return {boolean}
119+
*/
120+
var isUnique = function (astr) {
121+
let mask = 0;
122+
for (const c of astr) {
123+
const i = c.charCodeAt() - 'a'.charCodeAt();
124+
if ((mask >> i) & 1) {
125+
return false;
123126
}
124-
return true;
127+
mask |= 1 << i;
125128
}
129+
return true;
126130
};
127131
```
128132

133+
### **TypeScript**
134+
135+
```ts
136+
function isUnique(astr: string): boolean {
137+
let mask = 0;
138+
for (let j = 0; j < astr.length; ++j) {
139+
const i = astr.charCodeAt(j) - 'a'.charCodeAt(0);
140+
if ((mask >> i) & 1) {
141+
return false;
142+
}
143+
mask |= 1 << i;
144+
}
145+
return true;
146+
}
147+
```
148+
129149
### **...**
130150

131151
```

lcci/01.01.Is Unique/README_EN.md

+56-40
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
```python
4242
class Solution:
4343
def isUnique(self, astr: str) -> bool:
44-
bitmap = 0
44+
mask = 0
4545
for c in astr:
46-
pos = ord(c) - ord('a')
47-
if (bitmap & (1 << pos)) != 0:
46+
i = ord(c) - ord('a')
47+
if (mask >> i) & 1:
4848
return False
49-
bitmap |= 1 << pos
49+
mask |= 1 << i
5050
return True
5151
```
5252

@@ -55,74 +55,90 @@ class Solution:
5555
```java
5656
class Solution {
5757
public boolean isUnique(String astr) {
58-
int bitmap = 0;
58+
int mask = 0;
5959
for (char c : astr.toCharArray()) {
60-
int pos = c - 'a';
61-
if ((bitmap & (1 << pos)) != 0) {
60+
int i = c - 'a';
61+
if (((mask >> i) & 1) == 1) {
6262
return false;
6363
}
64-
bitmap |= (1 << pos);
64+
mask |= 1 << i;
6565
}
6666
return true;
6767
}
6868
}
6969
```
7070

71-
### **JavaScript**
71+
### **C++**
7272

73-
```js
74-
/**
75-
* @param {string} astr
76-
* @return {boolean}
77-
*/
78-
var isUnique = function (astr) {
79-
let bitmap = 0;
80-
for (let i = 0; i < astr.length; ++i) {
81-
const pos = astr[i].charCodeAt() - 'a'.charCodeAt();
82-
if ((bitmap & (1 << pos)) != 0) {
83-
return false;
73+
```cpp
74+
class Solution {
75+
public:
76+
bool isUnique(string astr) {
77+
int mask = 0;
78+
for (char c : astr) {
79+
int i = c - 'a';
80+
if (mask >> i & 1) {
81+
return false;
82+
}
83+
mask |= 1 << i;
8484
}
85-
bitmap |= 1 << pos;
85+
return true;
8686
}
87-
return true;
8887
};
8988
```
9089
9190
### **Go**
9291
9392
```go
9493
func isUnique(astr string) bool {
95-
bitmap := 0
96-
for _, r := range astr {
97-
pos := r - 'a'
98-
if (bitmap & (1 << pos)) != 0 {
94+
mask := 0
95+
for _, c := range astr {
96+
i := c - 'a'
97+
if mask>>i&1 == 1 {
9998
return false
10099
}
101-
bitmap |= (1 << pos)
100+
mask |= 1 << i
102101
}
103102
return true
104103
}
105104
```
106105

107-
### **C++**
106+
### **JavaScript**
108107

109-
```cpp
110-
class Solution {
111-
public:
112-
bool isUnique(string astr) {
113-
int bitmap = 0;
114-
for (char c : astr) {
115-
int pos = c - 'a';
116-
if ((bitmap & (1 << pos)) != 0) {
117-
return false;
118-
}
119-
bitmap |= (1 << pos);
108+
```js
109+
/**
110+
* @param {string} astr
111+
* @return {boolean}
112+
*/
113+
var isUnique = function (astr) {
114+
let mask = 0;
115+
for (const c of astr) {
116+
const i = c.charCodeAt() - 'a'.charCodeAt();
117+
if ((mask >> i) & 1) {
118+
return false;
120119
}
121-
return true;
120+
mask |= 1 << i;
122121
}
122+
return true;
123123
};
124124
```
125125

126+
### **TypeScript**
127+
128+
```ts
129+
function isUnique(astr: string): boolean {
130+
let mask = 0;
131+
for (let j = 0; j < astr.length; ++j) {
132+
const i = astr.charCodeAt(j) - 'a'.charCodeAt(0);
133+
if ((mask >> i) & 1) {
134+
return false;
135+
}
136+
mask |= 1 << i;
137+
}
138+
return true;
139+
}
140+
```
141+
126142
### **...**
127143

128144
```

lcci/01.01.Is Unique/Solution.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public:
33
bool isUnique(string astr) {
4-
int bitmap = 0;
4+
int mask = 0;
55
for (char c : astr) {
6-
int pos = c - 'a';
7-
if ((bitmap & (1 << pos)) != 0) {
6+
int i = c - 'a';
7+
if (mask >> i & 1) {
88
return false;
99
}
10-
bitmap |= (1 << pos);
10+
mask |= 1 << i;
1111
}
1212
return true;
1313
}

lcci/01.01.Is Unique/Solution.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
func isUnique(astr string) bool {
2-
bitmap := 0
3-
for _, r := range astr {
4-
pos := r - 'a'
5-
if (bitmap & (1 << pos)) != 0 {
2+
mask := 0
3+
for _, c := range astr {
4+
i := c - 'a'
5+
if mask>>i&1 == 1 {
66
return false
77
}
8-
bitmap |= (1 << pos)
8+
mask |= 1 << i
99
}
1010
return true
11-
}
11+
}

lcci/01.01.Is Unique/Solution.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public boolean isUnique(String astr) {
3-
int bitmap = 0;
3+
int mask = 0;
44
for (char c : astr.toCharArray()) {
5-
int pos = c - 'a';
6-
if ((bitmap & (1 << pos)) != 0) {
5+
int i = c - 'a';
6+
if (((mask >> i) & 1) == 1) {
77
return false;
88
}
9-
bitmap |= (1 << pos);
9+
mask |= 1 << i;
1010
}
1111
return true;
1212
}

lcci/01.01.Is Unique/Solution.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* @return {boolean}
44
*/
55
var isUnique = function (astr) {
6-
let bitmap = 0;
7-
for (let i = 0; i < astr.length; ++i) {
8-
const pos = astr[i].charCodeAt() - 'a'.charCodeAt();
9-
if ((bitmap & (1 << pos)) != 0) {
6+
let mask = 0;
7+
for (const c of astr) {
8+
const i = c.charCodeAt() - 'a'.charCodeAt();
9+
if ((mask >> i) & 1) {
1010
return false;
1111
}
12-
bitmap |= 1 << pos;
12+
mask |= 1 << i;
1313
}
1414
return true;
1515
};

lcci/01.01.Is Unique/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution:
22
def isUnique(self, astr: str) -> bool:
3-
bitmap = 0
3+
mask = 0
44
for c in astr:
5-
pos = ord(c) - ord('a')
6-
if (bitmap & (1 << pos)) != 0:
5+
i = ord(c) - ord('a')
6+
if (mask >> i) & 1:
77
return False
8-
bitmap |= 1 << pos
8+
mask |= 1 << i
99
return True

0 commit comments

Comments
 (0)