Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bb6483e

Browse files
committed
feat: update solutions to leetcode problem: No.0771. Jewels and Stones
1 parent 65b3267 commit bb6483e

File tree

8 files changed

+94
-31
lines changed

8 files changed

+94
-31
lines changed

‎solution/0500-0599/0535.Encode and Decode TinyURL/README.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public class Codec {
6060

6161
// Encodes a URL to a shortened URL.
6262
public String encode(String longUrl) {
63-
++count;
64-
String code = Integer.toHexString(count);
63+
String code = Integer.toHexString(++count);
6564
code2Url.put(code, longUrl);
6665
return prefixUrl + code;
6766
}

‎solution/0500-0599/0535.Encode and Decode TinyURL/README_EN.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public class Codec {
5252

5353
// Encodes a URL to a shortened URL.
5454
public String encode(String longUrl) {
55-
++count;
56-
String code = Integer.toHexString(count);
55+
String code = Integer.toHexString(++count);
5756
code2Url.put(code, longUrl);
5857
return prefixUrl + code;
5958
}

‎solution/0500-0599/0535.Encode and Decode TinyURL/Solution.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ public class Codec {
55

66
// Encodes a URL to a shortened URL.
77
public String encode(String longUrl) {
8-
++count;
9-
String code = Integer.toHexString(count);
8+
String code = Integer.toHexString(++count);
109
code2Url.put(code, longUrl);
1110
return prefixUrl + code;
1211
}

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,62 @@
2929
<li>&nbsp;<code>J</code>&nbsp;中的字符不重复。</li>
3030
</ul>
3131

32-
3332
## 解法
3433

3534
<!-- 这里可写通用的实现逻辑 -->
3635

36+
哈希表实现。
37+
3738
<!-- tabs:start -->
3839

3940
### **Python3**
4041

4142
<!-- 这里可写当前语言的特殊实现逻辑 -->
4243

4344
```python
44-
45+
class Solution:
46+
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])
4549
```
4650

4751
### **Java**
4852

4953
<!-- 这里可写当前语言的特殊实现逻辑 -->
5054

5155
```java
56+
class Solution {
57+
public int numJewelsInStones(String jewels, String stones) {
58+
Set<Character> jewelSet = new HashSet<>();
59+
for (char ch : jewels.toCharArray()) {
60+
jewelSet.add(ch);
61+
}
62+
int res = 0;
63+
for (char ch : stones.toCharArray()) {
64+
res += (jewelSet.contains(ch) ? 1 : 0);
65+
}
66+
return res;
67+
}
68+
}
69+
```
5270

71+
### **C++**
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
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]);
80+
}
81+
int res = 0;
82+
for (int i = 0; i < stones.length(); ++i) {
83+
res += jewelsSet.count(stones[i]);
84+
}
85+
return res;
86+
}
87+
};
5388
```
5489
5590
### **...**

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,54 @@
2525
<li>All the characters of&nbsp;<code>jewels</code> are <strong>unique</strong>.</li>
2626
</ul>
2727

28-
2928
## Solutions
3029

3130
<!-- tabs:start -->
3231

3332
### **Python3**
3433

3534
```python
36-
35+
class Solution:
36+
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])
3739
```
3840

3941
### **Java**
4042

4143
```java
44+
class Solution {
45+
public int numJewelsInStones(String jewels, String stones) {
46+
Set<Character> jewelSet = new HashSet<>();
47+
for (char ch : jewels.toCharArray()) {
48+
jewelSet.add(ch);
49+
}
50+
int res = 0;
51+
for (char ch : stones.toCharArray()) {
52+
res += (jewelSet.contains(ch) ? 1 : 0);
53+
}
54+
return res;
55+
}
56+
}
57+
```
4258

59+
### **C++**
60+
61+
```cpp
62+
class Solution {
63+
public:
64+
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]);
68+
}
69+
int res = 0;
70+
for (int i = 0; i < stones.length(); ++i) {
71+
res += jewelsSet.count(stones[i]);
72+
}
73+
return res;
74+
}
75+
};
4376
```
4477
4578
### **...**
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
class Solution {
22
public:
3-
int numJewelsInStones(string J, string S) {
4-
std::unordered_map<char,int> count;
5-
int number = 0;
6-
for(char c: J) count[c]++;
7-
for(char c: S){
8-
if(count.find(c)!=count.end())
9-
number++;
3+
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]);
107
}
11-
return number;
8+
int res = 0;
9+
for (int i = 0; i < stones.length(); ++i) {
10+
res += jewelsSet.count(stones[i]);
11+
}
12+
return res;
1213
}
13-
};
14+
};

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
2-
public int numJewelsInStones(String J, String S) {
3-
Set<Character> set = new HashSet<>();
4-
for (char ch : J.toCharArray()) {
5-
set.add(ch);
2+
public int numJewelsInStones(String jewels, String stones) {
3+
Set<Character> jewelSet = new HashSet<>();
4+
for (char ch : jewels.toCharArray()) {
5+
jewelSet.add(ch);
66
}
77
int res = 0;
8-
for (char ch : S.toCharArray()) {
9-
res += (set.contains(ch) ? 1 : 0);
8+
for (char ch : stones.toCharArray()) {
9+
res += (jewelSet.contains(ch) ? 1 : 0);
1010
}
1111
return res;
1212
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
class Solution:
2-
def numJewelsInStones(self, J: str, S: str) -> int:
3-
record = {ch for ch in J}
4-
sum = 0
5-
for ch in S:
6-
sum += 1 if ch in record else 0
7-
return sum
2+
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])

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /