29
29
30
30
<!-- 这里可写通用的实现逻辑 -->
31
31
32
+ ** 方法一:位运算**
33
+
32
34
根据示例,可以假定字符串中只包含小写字母(实际验证,也符合假设)。
33
35
34
- 用 bitmap 标记小写字母是否出现过。
36
+ 因此,我们可以使用一个 $32$ 位整数 ` mask ` 的每一位来表示字符串中的每一个字符是否出现过。
37
+
38
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串长度。
35
39
36
40
<!-- tabs:start -->
37
41
42
46
``` python
43
47
class Solution :
44
48
def isUnique (self , astr : str ) -> bool :
45
- bitmap = 0
49
+ mask = 0
46
50
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 :
49
53
return False
50
- bitmap |= 1 << pos
54
+ mask |= 1 << i
51
55
return True
52
56
```
53
57
@@ -58,74 +62,90 @@ class Solution:
58
62
``` java
59
63
class Solution {
60
64
public boolean isUnique (String astr ) {
61
- int bitmap = 0 ;
65
+ int mask = 0 ;
62
66
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 ) {
65
69
return false ;
66
70
}
67
- bitmap |= ( 1 << pos) ;
71
+ mask |= 1 << i ;
68
72
}
69
73
return true ;
70
74
}
71
75
}
72
76
```
73
77
74
- ### ** JavaScript **
78
+ ### ** C++ **
75
79
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 ;
87
91
}
88
- bitmap |= 1 << pos ;
92
+ return true ;
89
93
}
90
- return true ;
91
94
};
92
95
```
93
96
94
97
### **Go**
95
98
96
99
```go
97
100
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 {
102
105
return false
103
106
}
104
- bitmap |= ( 1 << pos)
107
+ mask |= 1 << i
105
108
}
106
109
return true
107
110
}
108
111
```
109
112
110
- ### ** C++ **
113
+ ### ** JavaScript **
111
114
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 ;
123
126
}
124
- return true ;
127
+ mask |= 1 << i ;
125
128
}
129
+ return true ;
126
130
};
127
131
```
128
132
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
+
129
149
### ** ...**
130
150
131
151
```
0 commit comments