File tree Expand file tree Collapse file tree 8 files changed +94
-31
lines changed
0500-0599/0535.Encode and Decode TinyURL
0700-0799/0771.Jewels and Stones Expand file tree Collapse file tree 8 files changed +94
-31
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 2929 <li> <code>J</code> 中的字符不重复。</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### **...**
Original file line number Diff line number Diff line change 2525 <li>All the characters of <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### **...**
Original file line number Diff line number Diff line change 11class Solution {
22public:
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+ };
Original file line number Diff line number Diff line change 11class 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 }
Original file line number Diff line number Diff line change 11class 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 ])
You can’t perform that action at this time.
0 commit comments