Skip to content

Commit 1aca20a

Browse files
committed
feat: add solutions to lc problem: No.0771
No.0771.Jewels and Stones
1 parent 602de0e commit 1aca20a

File tree

9 files changed

+96
-90
lines changed

9 files changed

+96
-90
lines changed

solution/0700-0799/0771.Jewels and Stones/README.md

+35-24
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@
4040

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

43-
哈希表实现。
43+
**方法一:哈希表或数组**
44+
45+
我们可以先用一个哈希表或数组 $s$ 记录所有宝石的类型。然后遍历所有石头,如果当前石头是宝石,就将答案加一。
46+
47+
时间复杂度 $O(m+n)$。其中 $m$ 和 $n$ 分别是字符串 $jewels$ 和 $stones$ 的长度。
4448

4549
<!-- tabs:start -->
4650

@@ -52,7 +56,7 @@
5256
class Solution:
5357
def numJewelsInStones(self, jewels: str, stones: str) -> int:
5458
s = set(jewels)
55-
return sum([1 for c in stones if c in s])
59+
return sum(c in s for c in stones)
5660
```
5761

5862
### **Java**
@@ -62,15 +66,15 @@ class Solution:
6266
```java
6367
class Solution {
6468
public int numJewelsInStones(String jewels, String stones) {
65-
Set<Character> s = new HashSet<>();
69+
int[] s = new int[128];
6670
for (char c : jewels.toCharArray()) {
67-
s.add(c);
71+
s[c] = 1;
6872
}
69-
int res = 0;
73+
int ans = 0;
7074
for (char c : stones.toCharArray()) {
71-
res += (s.contains(c) ? 1 : 0);
75+
ans += s[c];
7276
}
73-
return res;
77+
return ans;
7478
}
7579
}
7680
```
@@ -81,37 +85,44 @@ class Solution {
8185
class Solution {
8286
public:
8387
int numJewelsInStones(string jewels, string stones) {
84-
unordered_set<char> s;
85-
for (char c : jewels) {
86-
s.insert(c);
87-
}
88-
int res = 0;
89-
for (char c : stones) {
90-
res += s.count(c);
91-
}
92-
return res;
88+
int s[128] = {0};
89+
for (char c : jewels) s[c] = 1;
90+
int ans = 0;
91+
for (char c : stones) ans += s[c];
92+
return ans;
9393
}
9494
};
9595
```
9696
9797
### **Go**
9898
9999
```go
100-
func numJewelsInStones(jewels string, stones string) int {
101-
s := make(map[rune]bool)
100+
func numJewelsInStones(jewels string, stones string) (ans int) {
101+
s := make([]int, 128)
102102
for _, c := range jewels {
103-
s[c] = true
103+
s[c] = 1
104104
}
105-
res := 0
106105
for _, c := range stones {
107-
if s[c] {
108-
res++
109-
}
106+
ans += s[c]
110107
}
111-
return res
108+
return
112109
}
113110
```
114111

112+
### **JavaScript**
113+
114+
```js
115+
/**
116+
* @param {string} jewels
117+
* @param {string} stones
118+
* @return {number}
119+
*/
120+
var numJewelsInStones = function (jewels, stones) {
121+
const s = new Set(jewels.split(''));
122+
return stones.split('').reduce((prev, val) => prev + s.has(val), 0);
123+
};
124+
```
125+
115126
### **...**
116127

117128
```

solution/0700-0799/0771.Jewels and Stones/README_EN.md

+30-23
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@
3535
class Solution:
3636
def numJewelsInStones(self, jewels: str, stones: str) -> int:
3737
s = set(jewels)
38-
return sum([1 for c in stones if c in s])
38+
return sum(c in s for c in stones)
3939
```
4040

4141
### **Java**
4242

4343
```java
4444
class Solution {
4545
public int numJewelsInStones(String jewels, String stones) {
46-
Set<Character> s = new HashSet<>();
46+
int[] s = new int[128];
4747
for (char c : jewels.toCharArray()) {
48-
s.add(c);
48+
s[c] = 1;
4949
}
50-
int res = 0;
50+
int ans = 0;
5151
for (char c : stones.toCharArray()) {
52-
res += (s.contains(c) ? 1 : 0);
52+
ans += s[c];
5353
}
54-
return res;
54+
return ans;
5555
}
5656
}
5757
```
@@ -62,37 +62,44 @@ class Solution {
6262
class Solution {
6363
public:
6464
int numJewelsInStones(string jewels, string stones) {
65-
unordered_set<char> s;
66-
for (char c : jewels) {
67-
s.insert(c);
68-
}
69-
int res = 0;
70-
for (char c : stones) {
71-
res += s.count(c);
72-
}
73-
return res;
65+
int s[128] = {0};
66+
for (char c : jewels) s[c] = 1;
67+
int ans = 0;
68+
for (char c : stones) ans += s[c];
69+
return ans;
7470
}
7571
};
7672
```
7773
7874
### **Go**
7975
8076
```go
81-
func numJewelsInStones(jewels string, stones string) int {
82-
s := make(map[rune]bool)
77+
func numJewelsInStones(jewels string, stones string) (ans int) {
78+
s := make([]int, 128)
8379
for _, c := range jewels {
84-
s[c] = true
80+
s[c] = 1
8581
}
86-
res := 0
8782
for _, c := range stones {
88-
if s[c] {
89-
res++
90-
}
83+
ans += s[c]
9184
}
92-
return res
85+
return
9386
}
9487
```
9588

89+
### **JavaScript**
90+
91+
```js
92+
/**
93+
* @param {string} jewels
94+
* @param {string} stones
95+
* @return {number}
96+
*/
97+
var numJewelsInStones = function (jewels, stones) {
98+
const s = new Set(jewels.split(''));
99+
return stones.split('').reduce((prev, val) => prev + s.has(val), 0);
100+
};
101+
```
102+
96103
### **...**
97104

98105
```
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
class Solution {
22
public:
33
int numJewelsInStones(string jewels, string stones) {
4-
unordered_set<char> s;
5-
for (char c : jewels) {
6-
s.insert(c);
7-
}
8-
int res = 0;
9-
for (char c : stones) {
10-
res += s.count(c);
11-
}
12-
return res;
4+
int s[128] = {0};
5+
for (char c : jewels) s[c] = 1;
6+
int ans = 0;
7+
for (char c : stones) ans += s[c];
8+
return ans;
139
}
1410
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
func numJewelsInStones(jewels string, stones string) int {
2-
s := make(map[rune]bool)
1+
func numJewelsInStones(jewels string, stones string) (ans int) {
2+
s := make([]int, 128)
33
for _, c := range jewels {
4-
s[c] = true
4+
s[c] = 1
55
}
6-
res := 0
76
for _, c := range stones {
8-
if s[c] {
9-
res++
10-
}
7+
ans += s[c]
118
}
12-
return res
9+
return
1310
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public int numJewelsInStones(String jewels, String stones) {
3-
Set<Character> s = new HashSet<>();
3+
int[] s = new int[128];
44
for (char c : jewels.toCharArray()) {
5-
s.add(c);
5+
s[c] = 1;
66
}
7-
int res = 0;
7+
int ans = 0;
88
for (char c : stones.toCharArray()) {
9-
res += (s.contains(c) ? 1 : 0);
9+
ans += s[c];
1010
}
11-
return res;
11+
return ans;
1212
}
1313
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @param {string} jewels
3+
* @param {string} stones
4+
* @return {number}
5+
*/
6+
var numJewelsInStones = function (jewels, stones) {
7+
const s = new Set(jewels.split(''));
8+
return stones.split('').reduce((prev, val) => prev + s.has(val), 0);
9+
};
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Solution:
22
def numJewelsInStones(self, jewels: str, stones: str) -> int:
33
s = set(jewels)
4-
return sum([1 for c in stones if c in s])
4+
return sum(c in s for c in stones)

solution/0700-0799/0773.Sliding Puzzle/README.md

+3-10
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,9 @@ class Solution:
9999
for j in range(3):
100100
board[i][j] = int(s[i * 3 + j])
101101

102-
def next():
103-
def find0():
104-
for i in range(2):
105-
for j in range(3):
106-
if board[i][j] == 0:
107-
return (i, j)
108-
return (0, 0)
109-
102+
def f():
110103
res = []
111-
i, j = find0()
104+
i, j = next((i, j) for i in range(2) for j in range(3) if board[i][j] == 0)
112105
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
113106
x, y = i + a, j + b
114107
if 0 <= x < 2 and 0 <= y < 3:
@@ -129,7 +122,7 @@ class Solution:
129122
for _ in range(len(q)):
130123
x = q.popleft()
131124
setb(x)
132-
for y in next():
125+
for y in f():
133126
if y == end:
134127
return ans
135128
if y not in vis:

solution/0700-0799/0773.Sliding Puzzle/README_EN.md

+3-10
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,9 @@ class Solution:
7676
for j in range(3):
7777
board[i][j] = int(s[i * 3 + j])
7878

79-
def next():
80-
def find0():
81-
for i in range(2):
82-
for j in range(3):
83-
if board[i][j] == 0:
84-
return (i, j)
85-
return (0, 0)
86-
79+
def f():
8780
res = []
88-
i, j = find0()
81+
i, j = next((i, j) for i in range(2) for j in range(3) if board[i][j] == 0)
8982
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
9083
x, y = i + a, j + b
9184
if 0 <= x < 2 and 0 <= y < 3:
@@ -106,7 +99,7 @@ class Solution:
10699
for _ in range(len(q)):
107100
x = q.popleft()
108101
setb(x)
109-
for y in next():
102+
for y in f():
110103
if y == end:
111104
return ans
112105
if y not in vis:

0 commit comments

Comments
 (0)