Skip to content

Commit 3da5184

Browse files
committedAug 7, 2021
feat: update solutions to lc problem: No.0771.Jewels and Stones
1 parent 8adb831 commit 3da5184

File tree

6 files changed

+85
-36
lines changed

6 files changed

+85
-36
lines changed
 

Diff for: ‎solution/0700-0799/0771.Jewels and Stones/README.md

+30-12
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
```python
4545
class Solution:
4646
def numJewelsInStones(self, jewels: str, stones: str) -> int:
47-
jewel_set = {c for c in jewels}
48-
return sum([1 for c in stones if c in jewel_set])
47+
s = set(jewels)
48+
return sum([1 for c in stones if c in s])
4949
```
5050

5151
### **Java**
@@ -55,13 +55,13 @@ class Solution:
5555
```java
5656
class Solution {
5757
public int numJewelsInStones(String jewels, String stones) {
58-
Set<Character> jewelSet = new HashSet<>();
59-
for (char ch : jewels.toCharArray()) {
60-
jewelSet.add(ch);
58+
Set<Character> s = new HashSet<>();
59+
for (char c : jewels.toCharArray()) {
60+
s.add(c);
6161
}
6262
int res = 0;
63-
for (char ch : stones.toCharArray()) {
64-
res += (jewelSet.contains(ch) ? 1 : 0);
63+
for (char c : stones.toCharArray()) {
64+
res += (s.contains(c) ? 1 : 0);
6565
}
6666
return res;
6767
}
@@ -74,19 +74,37 @@ class Solution {
7474
class Solution {
7575
public:
7676
int numJewelsInStones(string jewels, string stones) {
77-
unordered_set<char> jewelsSet;
78-
for (int i = 0; i < jewels.length(); ++i) {
79-
jewelsSet.insert(jewels[i]);
77+
unordered_set<char> s;
78+
for (char c : jewels) {
79+
s.insert(c);
8080
}
8181
int res = 0;
82-
for (int i = 0; i < stones.length(); ++i) {
83-
res += jewelsSet.count(stones[i]);
82+
for (char c : stones) {
83+
res += s.count(c);
8484
}
8585
return res;
8686
}
8787
};
8888
```
8989
90+
### **Go**
91+
92+
```go
93+
func numJewelsInStones(jewels string, stones string) int {
94+
s := make(map[rune]bool)
95+
for _, c := range jewels {
96+
s[c] = true
97+
}
98+
res := 0
99+
for _, c := range stones {
100+
if s[c] {
101+
res++
102+
}
103+
}
104+
return res
105+
}
106+
```
107+
90108
### **...**
91109

92110
```

Diff for: ‎solution/0700-0799/0771.Jewels and Stones/README_EN.md

+30-12
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@
3434
```python
3535
class Solution:
3636
def numJewelsInStones(self, jewels: str, stones: str) -> int:
37-
jewel_set = {c for c in jewels}
38-
return sum([1 for c in stones if c in jewel_set])
37+
s = set(jewels)
38+
return sum([1 for c in stones if c in s])
3939
```
4040

4141
### **Java**
4242

4343
```java
4444
class Solution {
4545
public int numJewelsInStones(String jewels, String stones) {
46-
Set<Character> jewelSet = new HashSet<>();
47-
for (char ch : jewels.toCharArray()) {
48-
jewelSet.add(ch);
46+
Set<Character> s = new HashSet<>();
47+
for (char c : jewels.toCharArray()) {
48+
s.add(c);
4949
}
5050
int res = 0;
51-
for (char ch : stones.toCharArray()) {
52-
res += (jewelSet.contains(ch) ? 1 : 0);
51+
for (char c : stones.toCharArray()) {
52+
res += (s.contains(c) ? 1 : 0);
5353
}
5454
return res;
5555
}
@@ -62,19 +62,37 @@ class Solution {
6262
class Solution {
6363
public:
6464
int numJewelsInStones(string jewels, string stones) {
65-
unordered_set<char> jewelsSet;
66-
for (int i = 0; i < jewels.length(); ++i) {
67-
jewelsSet.insert(jewels[i]);
65+
unordered_set<char> s;
66+
for (char c : jewels) {
67+
s.insert(c);
6868
}
6969
int res = 0;
70-
for (int i = 0; i < stones.length(); ++i) {
71-
res += jewelsSet.count(stones[i]);
70+
for (char c : stones) {
71+
res += s.count(c);
7272
}
7373
return res;
7474
}
7575
};
7676
```
7777
78+
### **Go**
79+
80+
```go
81+
func numJewelsInStones(jewels string, stones string) int {
82+
s := make(map[rune]bool)
83+
for _, c := range jewels {
84+
s[c] = true
85+
}
86+
res := 0
87+
for _, c := range stones {
88+
if s[c] {
89+
res++
90+
}
91+
}
92+
return res
93+
}
94+
```
95+
7896
### **...**
7997

8098
```

Diff for: ‎solution/0700-0799/0771.Jewels and Stones/Solution.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public:
33
int numJewelsInStones(string jewels, string stones) {
4-
unordered_set<char> jewelsSet;
5-
for (int i = 0; i < jewels.length(); ++i) {
6-
jewelsSet.insert(jewels[i]);
4+
unordered_set<char> s;
5+
for (char c : jewels) {
6+
s.insert(c);
77
}
88
int res = 0;
9-
for (int i = 0; i < stones.length(); ++i) {
10-
res += jewelsSet.count(stones[i]);
9+
for (char c : stones) {
10+
res += s.count(c);
1111
}
1212
return res;
1313
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func numJewelsInStones(jewels string, stones string) int {
2+
s := make(map[rune]bool)
3+
for _, c := range jewels {
4+
s[c] = true
5+
}
6+
res := 0
7+
for _, c := range stones {
8+
if s[c] {
9+
res++
10+
}
11+
}
12+
return res
13+
}

Diff for: ‎solution/0700-0799/0771.Jewels and Stones/Solution.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public int numJewelsInStones(String jewels, String stones) {
3-
Set<Character> jewelSet = new HashSet<>();
4-
for (char ch : jewels.toCharArray()) {
5-
jewelSet.add(ch);
3+
Set<Character> s = new HashSet<>();
4+
for (char c : jewels.toCharArray()) {
5+
s.add(c);
66
}
77
int res = 0;
8-
for (char ch : stones.toCharArray()) {
9-
res += (jewelSet.contains(ch) ? 1 : 0);
8+
for (char c : stones.toCharArray()) {
9+
res += (s.contains(c) ? 1 : 0);
1010
}
1111
return res;
1212
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Solution:
22
def numJewelsInStones(self, jewels: str, stones: str) -> int:
3-
jewel_set = {c for c in jewels}
4-
return sum([1 for c in stones if c in jewel_set])
3+
s = set(jewels)
4+
return sum([1 for c in stones if c in s])

0 commit comments

Comments
 (0)