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 c892bfc

Browse files
Create 2375. Construct Smallest Number From DI String
1 parent c589d97 commit c892bfc

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public String smallestNumber(String pattern) {
3+
int size = pattern.length();
4+
int[] ans = new int[size+1];
5+
for(int i=0;i<size+1;i++){
6+
ans[i]=i+1;
7+
}
8+
9+
for(int i=0;i<size;i++){
10+
int t=i;
11+
while(t<size && pattern.charAt(t)=='D'){
12+
t++;
13+
}
14+
15+
int left=i,right=t;
16+
while(left<right){
17+
int temp = ans[left];
18+
ans[left]=ans[right];
19+
ans[right]=temp;
20+
left++;
21+
right--;
22+
}
23+
24+
if(t!=i) i = t-1;
25+
26+
}
27+
28+
return Arrays.toString(ans).replaceAll("\\[|\\]|,|\\s", "");
29+
}
30+
}

0 commit comments

Comments
(0)

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