Skip to content

Commit f44deeb

Browse files
committed
chore: use computeIfAbsent api in java solutions
1 parent 59d9f7b commit f44deeb

File tree

32 files changed

+60
-178
lines changed

32 files changed

+60
-178
lines changed

solution/0100-0199/0126.Word Ladder II/Solution.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ private void bfs(Set<String> forward, Set<String> backward, Set<String> dict, bo
3838
if (!backward.contains(temp) && !dict.contains(temp)) continue;
3939
String key = !swap ? str : temp;
4040
String val = !swap ? temp : str;
41-
if (!hs.containsKey(key)) hs.put(key, new ArrayList<>());
4241
if (backward.contains(temp)) {
43-
hs.get(key).add(val);
42+
hs.computeIfAbsent(key, k -> new ArrayList<>()).add(val);
4443
isConnected = true;
4544
}
4645
if (!isConnected && dict.contains(temp)) {
47-
hs.get(key).add(val);
46+
hs.computeIfAbsent(key, k -> new ArrayList<>()).add(val);
4847
set3.add(temp);
4948
}
5049
}

solution/0200-0299/0216.Combination Sum III/Solution.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
class Solution {
22
public List<List<Integer>> combinationSum3(int k, int n) {
3-
List<List<Integer>> ans = new ArrayList<>();
4-
robot(1, k, n, ans, new ArrayList<Integer>());
3+
List<List<Integer>> ans = new ArrayList<>();
4+
robot(1, k, n, ans, new ArrayList<>());
55
return ans;
66
}
7-
7+
88
private void robot(int start, int k, int left, List<List<Integer>> ans, List<Integer> tmp) {
9-
if(k < 0 || left < 0) return;
10-
11-
if(k == 0 && left == 0) {
9+
if (k < 0 || left < 0) return;
10+
11+
if (k == 0 && left == 0) {
1212
ans.add(new ArrayList<>(tmp));
1313
return;
1414
}
15-
16-
for(int i = start; i <= 9; i++) {
17-
if(left >= i && k > 0) {
15+
16+
for (int i = start; i <= 9; i++) {
17+
if (left >= i && k > 0) {
1818
tmp.add(i);
1919
robot(i + 1, k - 1, left - i, ans, tmp);
2020
tmp.remove(tmp.size() - 1);

solution/0200-0299/0288.Unique Word Abbreviation/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,10 @@ class ValidWordAbbr {
115115
words = new HashMap<>();
116116
for (String word : dictionary) {
117117
String abbr = wordAbbr(word);
118-
Set<String> vals = words.getOrDefault(abbr, new HashSet<>());
119-
vals.add(word);
120-
words.put(abbr, vals);
118+
words.computeIfAbsent(abbr, k -> new HashSet<>()).add(word);
121119
}
122120
}
123-
121+
124122
public boolean isUnique(String word) {
125123
String abbr = wordAbbr(word);
126124
Set<String> vals = words.get(abbr);

solution/0200-0299/0288.Unique Word Abbreviation/README_EN.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,10 @@ class ValidWordAbbr {
103103
words = new HashMap<>();
104104
for (String word : dictionary) {
105105
String abbr = wordAbbr(word);
106-
Set<String> vals = words.getOrDefault(abbr, new HashSet<>());
107-
vals.add(word);
108-
words.put(abbr, vals);
106+
words.computeIfAbsent(abbr, k -> new HashSet<>()).add(word);
109107
}
110108
}
111-
109+
112110
public boolean isUnique(String word) {
113111
String abbr = wordAbbr(word);
114112
Set<String> vals = words.get(abbr);

solution/0200-0299/0288.Unique Word Abbreviation/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ public ValidWordAbbr(String[] dictionary) {
55
words = new HashMap<>();
66
for (String word : dictionary) {
77
String abbr = wordAbbr(word);
8-
Set<String> vals = words.getOrDefault(abbr, new HashSet<>());
9-
vals.add(word);
10-
words.put(abbr, vals);
8+
words.computeIfAbsent(abbr, k -> new HashSet<>()).add(word);
119
}
1210
}
1311

solution/0300-0399/0355.Design Twitter/README.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,12 @@ class Twitter {
159159

160160
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
161161
public void follow(int followerId, int followeeId) {
162-
Set<Integer> following = userFollowing.getOrDefault(followerId, new HashSet<>());
163-
following.add(followeeId);
164-
userFollowing.put(followerId, following);
162+
userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).add(followeeId);
165163
}
166164

167165
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
168166
public void unfollow(int followerId, int followeeId) {
169-
Set<Integer> following = userFollowing.getOrDefault(followerId, new HashSet<>());
170-
following.remove(followeeId);
171-
userFollowing.put(followerId, following);
167+
userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).remove(followeeId);
172168
}
173169
}
174170

solution/0300-0399/0355.Design Twitter/README_EN.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,12 @@ class Twitter {
154154

155155
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
156156
public void follow(int followerId, int followeeId) {
157-
Set<Integer> following = userFollowing.getOrDefault(followerId, new HashSet<>());
158-
following.add(followeeId);
159-
userFollowing.put(followerId, following);
157+
userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).add(followeeId);
160158
}
161159

162160
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
163161
public void unfollow(int followerId, int followeeId) {
164-
Set<Integer> following = userFollowing.getOrDefault(followerId, new HashSet<>());
165-
following.remove(followeeId);
166-
userFollowing.put(followerId, following);
162+
userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).remove(followeeId);
167163
}
168164
}
169165

solution/0300-0399/0355.Design Twitter/Solution.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,12 @@ public List<Integer> getNewsFeed(int userId) {
4141

4242
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
4343
public void follow(int followerId, int followeeId) {
44-
Set<Integer> following = userFollowing.getOrDefault(followerId, new HashSet<>());
45-
following.add(followeeId);
46-
userFollowing.put(followerId, following);
44+
userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).add(followeeId);
4745
}
4846

4947
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
5048
public void unfollow(int followerId, int followeeId) {
51-
Set<Integer> following = userFollowing.getOrDefault(followerId, new HashSet<>());
52-
following.remove(followeeId);
53-
userFollowing.put(followerId, following);
49+
userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).remove(followeeId);
5450
}
5551
}
5652

solution/0300-0399/0381.Insert Delete GetRandom O(1) - Duplicates allowed/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,9 @@ class RandomizedCollection {
132132

133133
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
134134
public boolean insert(int val) {
135-
Set<Integer> idxSet = m.getOrDefault(val, new HashSet<>());
136-
idxSet.add(l.size());
137-
m.put(val, idxSet);
135+
m.computeIfAbsent(val, k -> new HashSet<>()).add(l.size());
138136
l.add(val);
139-
return idxSet.size() == 1;
137+
return m.get(val).size() == 1;
140138
}
141139

142140
/** Removes a value from the collection. Returns true if the collection contained the specified element. */

solution/0300-0399/0381.Insert Delete GetRandom O(1) - Duplicates allowed/README_EN.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,9 @@ class RandomizedCollection {
125125

126126
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
127127
public boolean insert(int val) {
128-
Set<Integer> idxSet = m.getOrDefault(val, new HashSet<>());
129-
idxSet.add(l.size());
130-
m.put(val, idxSet);
128+
m.computeIfAbsent(val, k -> new HashSet<>()).add(l.size());
131129
l.add(val);
132-
return idxSet.size() == 1;
130+
return m.get(val).size() == 1;
133131
}
134132

135133
/** Removes a value from the collection. Returns true if the collection contained the specified element. */

solution/0300-0399/0381.Insert Delete GetRandom O(1) - Duplicates allowed/Solution.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ public RandomizedCollection() {
99
l = new ArrayList<>();
1010
rnd = new Random();
1111
}
12-
12+
1313
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
1414
public boolean insert(int val) {
15-
Set<Integer> idxSet = m.getOrDefault(val, new HashSet<>());
16-
idxSet.add(l.size());
17-
m.put(val, idxSet);
15+
m.computeIfAbsent(val, k -> new HashSet<>()).add(l.size());
1816
l.add(val);
19-
return idxSet.size() == 1;
17+
return m.get(val).size() == 1;
2018
}
21-
19+
2220
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
2321
public boolean remove(int val) {
2422
if (!m.containsKey(val)) {
@@ -41,7 +39,7 @@ public boolean remove(int val) {
4139
l.remove(lastIdx);
4240
return true;
4341
}
44-
42+
4543
/** Get a random element from the collection. */
4644
public int getRandom() {
4745
int size = l.size();

solution/0700-0799/0721.Accounts Merge/README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,7 @@ class Solution {
177177
List<String> account = accounts.get(i);
178178
for (int j = 1; j < account.size(); ++j) {
179179
String email = account.get(j);
180-
if (!mp.containsKey(pa)) {
181-
mp.put(pa, new HashSet<>());
182-
}
183-
mp.get(pa).add(email);
180+
mp.computeIfAbsent(pa, k -> new HashSet<>()).add(email);
184181
}
185182
}
186183
List<List<String>> res = new ArrayList<>();

solution/0700-0799/0721.Accounts Merge/README_EN.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ class Solution {
111111
List<String> account = accounts.get(i);
112112
for (int j = 1; j < account.size(); ++j) {
113113
String email = account.get(j);
114-
if (!mp.containsKey(pa)) {
115-
mp.put(pa, new HashSet<>());
116-
}
117-
mp.get(pa).add(email);
114+
mp.computeIfAbsent(pa, k -> new HashSet<>()).add(email);
118115
}
119116
}
120117
List<List<String>> res = new ArrayList<>();

solution/0700-0799/0721.Accounts Merge/Solutioin.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ public List<List<String>> accountsMerge(List<List<String>> accounts) {
2626
List<String> account = accounts.get(i);
2727
for (int j = 1; j < account.size(); ++j) {
2828
String email = account.get(j);
29-
if (!mp.containsKey(pa)) {
30-
mp.put(pa, new HashSet<>());
31-
}
32-
mp.get(pa).add(email);
29+
mp.computeIfAbsent(pa, k -> new HashSet<>()).add(email);
3330
}
3431
}
3532
List<List<String>> res = new ArrayList<>();

solution/0700-0799/0760.Find Anagram Mappings/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ class Solution {
6767
public int[] anagramMappings(int[] nums1, int[] nums2) {
6868
Map<Integer, Set<Integer>> map = new HashMap<>();
6969
for (int i = 0; i < nums2.length; ++i) {
70-
Set<Integer> s = map.getOrDefault(nums2[i], new HashSet<>());
71-
s.add(i);
72-
map.put(nums2[i], s);
70+
map.computeIfAbsent(nums2[i], k -> new HashSet<>()).add(i);
7371
}
7472
int[] res = new int[nums1.length];
7573
for (int i = 0; i < nums1.length; ++i) {

solution/0700-0799/0760.Find Anagram Mappings/README_EN.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ We want to find an <i>index mapping</i> <code>P</code>, from <code>A</code> to <
1414

1515
</p><p>
1616

17-
These lists <code>A</code> and <code>B</code> may contain duplicates. If there are multiple answers, output any of them.
17+
These lists <code>A</code> and <code>B</code> may contain duplicates. If there are multiple answers, output any of them.
1818

1919
</p>
2020

21-
22-
2321
<p>
2422

2523
For example, given
@@ -50,8 +48,6 @@ and so on.
5048

5149
</p>
5250

53-
54-
5551
<p><b>Note:</b><ol>
5652

5753
<li><code>A, B</code> have equal lengths in range <code>[1, 100]</code>.</li>
@@ -82,9 +78,7 @@ class Solution {
8278
public int[] anagramMappings(int[] nums1, int[] nums2) {
8379
Map<Integer, Set<Integer>> map = new HashMap<>();
8480
for (int i = 0; i < nums2.length; ++i) {
85-
Set<Integer> s = map.getOrDefault(nums2[i], new HashSet<>());
86-
s.add(i);
87-
map.put(nums2[i], s);
81+
map.computeIfAbsent(nums2[i], k -> new HashSet<>()).add(i);
8882
}
8983
int[] res = new int[nums1.length];
9084
for (int i = 0; i < nums1.length; ++i) {

solution/0700-0799/0760.Find Anagram Mappings/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ class Solution {
22
public int[] anagramMappings(int[] nums1, int[] nums2) {
33
Map<Integer, Set<Integer>> map = new HashMap<>();
44
for (int i = 0; i < nums2.length; ++i) {
5-
Set<Integer> s = map.getOrDefault(nums2[i], new HashSet<>());
6-
s.add(i);
7-
map.put(nums2[i], s);
5+
map.computeIfAbsent(nums2[i], k -> new HashSet<>()).add(i);
86
}
97
int[] res = new int[nums1.length];
108
for (int i = 0; i < nums1.length; ++i) {

solution/0900-0999/0981.Time Based Key-Value Store/README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,11 @@ class TimeMap {
109109
public TimeMap() {
110110
ktv = new HashMap<>();
111111
}
112-
112+
113113
public void set(String key, String value, int timestamp) {
114-
TreeMap<Integer, String> tv = ktv.getOrDefault(key, new TreeMap<>());
115-
tv.put(timestamp, value);
116-
ktv.put(key, tv);
114+
ktv.computeIfAbsent(key, k -> new TreeMap<>()).put(timestamp, value);
117115
}
118-
116+
119117
public String get(String key, int timestamp) {
120118
if (!ktv.containsKey(key)) {
121119
return "";

solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,11 @@ class TimeMap {
123123
public TimeMap() {
124124
ktv = new HashMap<>();
125125
}
126-
126+
127127
public void set(String key, String value, int timestamp) {
128-
TreeMap<Integer, String> tv = ktv.getOrDefault(key, new TreeMap<>());
129-
tv.put(timestamp, value);
130-
ktv.put(key, tv);
128+
ktv.computeIfAbsent(key, k -> new TreeMap<>()).put(timestamp, value);
131129
}
132-
130+
133131
public String get(String key, int timestamp) {
134132
if (!ktv.containsKey(key)) {
135133
return "";

solution/0900-0999/0981.Time Based Key-Value Store/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ public TimeMap() {
77
}
88

99
public void set(String key, String value, int timestamp) {
10-
TreeMap<Integer, String> tv = ktv.getOrDefault(key, new TreeMap<>());
11-
tv.put(timestamp, value);
12-
ktv.put(key, tv);
10+
ktv.computeIfAbsent(key, k -> new TreeMap<>()).put(timestamp, value);
1311
}
1412

1513
public String get(String key, int timestamp) {

solution/1100-1199/1181.Before and After Puzzle/README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ class Solution {
9999
for (int i = 0; i < phrases.length; ++i) {
100100
String phrase = phrases[i];
101101
String word = phrase.split(" ")[0];
102-
if (!sameFirstWord.containsKey(word)) {
103-
sameFirstWord.put(word, new HashSet<>());
104-
}
105-
sameFirstWord.get(word).add(i);
102+
sameFirstWord.computeIfAbsent(word, k -> new HashSet<>()).add(i);
106103
}
107104
Set<String> res = new HashSet<>();
108105
for (int i = 0; i < phrases.length; ++i) {

solution/1100-1199/1181.Before and After Puzzle/README_EN.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ class Solution {
8888
for (int i = 0; i < phrases.length; ++i) {
8989
String phrase = phrases[i];
9090
String word = phrase.split(" ")[0];
91-
if (!sameFirstWord.containsKey(word)) {
92-
sameFirstWord.put(word, new HashSet<>());
93-
}
94-
sameFirstWord.get(word).add(i);
91+
sameFirstWord.computeIfAbsent(word, k -> new HashSet<>()).add(i);
9592
}
9693
Set<String> res = new HashSet<>();
9794
for (int i = 0; i < phrases.length; ++i) {

solution/1100-1199/1181.Before and After Puzzle/Solution.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ public List<String> beforeAndAfterPuzzles(String[] phrases) {
44
for (int i = 0; i < phrases.length; ++i) {
55
String phrase = phrases[i];
66
String word = phrase.split(" ")[0];
7-
if (!sameFirstWord.containsKey(word)) {
8-
sameFirstWord.put(word, new HashSet<>());
9-
}
10-
sameFirstWord.get(word).add(i);
7+
sameFirstWord.computeIfAbsent(word, k -> new HashSet<>()).add(i);
118
}
129
Set<String> res = new HashSet<>();
1310
for (int i = 0; i < phrases.length; ++i) {

solution/1200-1299/1258.Synonymous Sentences/README.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,8 @@ class Solution {
113113
Map<String, Set<String>> s = new HashMap<>();
114114
for (List<String> item : synonyms) {
115115
String a = find(item.get(0)), b = find(item.get(1));
116-
if (!s.containsKey(a)) {
117-
s.put(a, new HashSet<>());
118-
}
119-
if (!s.containsKey(b)) {
120-
s.put(b, new HashSet<>());
121-
}
122-
s.get(a).add(item.get(0));
123-
s.get(b).add(item.get(1));
116+
s.computeIfAbsent(a, k -> new HashSet<>()).add(item.get(0));
117+
s.computeIfAbsent(b, k -> new HashSet<>()).add(item.get(1));
124118
}
125119
List<List<String>> all = new ArrayList<>();
126120
for (String word : text.split(" ")) {

0 commit comments

Comments
 (0)