Movatterモバイル変換


[0]ホーム

URL:


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

Commit3499e48

Browse files
add 2182
1 parented9005e commit3499e48

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|------|----------------|---------------|--------|-------------|-------------
11+
| 2182|[Construct String With Repeat Limit](https://leetcode.com/problems/construct-string-with-repeat-limit/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2182.java)||Medium||
1112
| 2181|[Merge Nodes in Between Zeros](https://leetcode.com/problems/merge-nodes-in-between-zeros/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2181.java)||Medium||
1213
| 2180|[Count Integers With Even Digit Sum](https://leetcode.com/problems/count-integers-with-even-digit-sum/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2180.java)||Easy||
1314
| 2177|[Find Three Consecutive Integers That Sum to a Given Number](https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2177.java)||Medium||
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
importjava.util.PriorityQueue;
6+
7+
publicclass_2182 {
8+
publicstaticclassSolution1 {
9+
publicStringrepeatLimitedString(Strings,intrepeatLimit) {
10+
Map<Character,Integer>map =newHashMap<>();
11+
for (charc :s.toCharArray()) {
12+
map.put(c,map.getOrDefault(c,0) +1);
13+
}
14+
PriorityQueue<Count>heap =newPriorityQueue<Count>((a,b) ->b.c -a.c);
15+
for (charc :map.keySet()) {
16+
heap.offer(newCount(c,map.get(c)));
17+
}
18+
StringBuildersb =newStringBuilder();
19+
while (!heap.isEmpty()) {
20+
Countcurr =heap.poll();
21+
intnum =Math.min(curr.num,repeatLimit);
22+
while (num >0) {
23+
sb.append(curr.c);
24+
num--;
25+
}
26+
if (curr.num >repeatLimit) {
27+
CountnewCount =newCount(curr.c,curr.num -repeatLimit);
28+
if (!heap.isEmpty()) {
29+
Countnext =heap.poll();
30+
sb.append(next.c);
31+
if (next.num >1) {
32+
heap.offer(newCount(next.c,next.num -1));
33+
}
34+
}else {
35+
break;
36+
}
37+
heap.offer(newCount);
38+
}
39+
}
40+
returnsb.toString();
41+
}
42+
43+
classCount {
44+
charc;
45+
intnum;
46+
47+
publicCount(charc,Integerinteger) {
48+
this.c =c;
49+
this.num =integer;
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp