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 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‎

Lines changed: 2 additions & 3 deletions
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 -> newArrayList<>()).add(val);
4443
isConnected = true;
4544
}
4645
if (!isConnected && dict.contains(temp)) {
47-
hs.get(key).add(val);
46+
hs.computeIfAbsent(key, k -> newArrayList<>()).add(val);
4847
set3.add(temp);
4948
}
5049
}

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

Lines changed: 9 additions & 9 deletions
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‎

Lines changed: 2 additions & 4 deletions
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‎

Lines changed: 2 additions & 4 deletions
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‎

Lines changed: 1 addition & 3 deletions
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‎

Lines changed: 2 additions & 6 deletions
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‎

Lines changed: 2 additions & 6 deletions
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‎

Lines changed: 2 additions & 6 deletions
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‎

Lines changed: 2 additions & 4 deletions
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‎

Lines changed: 2 additions & 4 deletions
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. */

0 commit comments

Comments
(0)

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