Skip to content

Commit 2257a93

Browse files
committed
feat: update solutions to lcci problem: No.01.01. Is Unique
1 parent 786ba6d commit 2257a93

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

lcci/01.01.Is Unique/README.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class Solution:
5959
class Solution {
6060
public boolean isUnique(String astr) {
6161
int bitmap = 0;
62-
for (int i = 0, n = astr.length(); i < n; ++i) {
63-
int pos = astr.charAt(i) - 'a';
62+
for (char c : astr.toCharArray()) {
63+
int pos = c - 'a';
6464
if ((bitmap & (1 << pos)) != 0) {
6565
return false;
6666
}
@@ -107,6 +107,25 @@ func isUnique(astr string) bool {
107107
}
108108
```
109109

110+
### **C++**
111+
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);
123+
}
124+
return true;
125+
}
126+
};
127+
```
128+
110129
### **...**
111130
112131
```

lcci/01.01.Is Unique/README_EN.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class Solution:
5656
class Solution {
5757
public boolean isUnique(String astr) {
5858
int bitmap = 0;
59-
for (int i = 0, n = astr.length(); i < n; ++i) {
60-
int pos = astr.charAt(i) - 'a';
59+
for (char c : astr.toCharArray()) {
60+
int pos = c - 'a';
6161
if ((bitmap & (1 << pos)) != 0) {
6262
return false;
6363
}
@@ -104,6 +104,25 @@ func isUnique(astr string) bool {
104104
}
105105
```
106106

107+
### **C++**
108+
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);
120+
}
121+
return true;
122+
}
123+
};
124+
```
125+
107126
### **...**
108127
109128
```

lcci/01.01.Is Unique/Solution.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
bool isUnique(string astr) {
4+
int bitmap = 0;
5+
for (char c : astr) {
6+
int pos = c - 'a';
7+
if ((bitmap & (1 << pos)) != 0) {
8+
return false;
9+
}
10+
bitmap |= (1 << pos);
11+
}
12+
return true;
13+
}
14+
};

lcci/01.01.Is Unique/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution {
22
public boolean isUnique(String astr) {
33
int bitmap = 0;
4-
for (int i = 0, n = astr.length(); i < n; ++i) {
5-
int pos = astr.charAt(i) - 'a';
4+
for (char c : astr.toCharArray()) {
5+
int pos = c - 'a';
66
if ((bitmap & (1 << pos)) != 0) {
77
return false;
88
}

0 commit comments

Comments
 (0)