FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OSSDP-Lab2-acceptPR/Solution2.java at main · MortusCc/OSSDP-Lab2-acceptPR · GitHub
MortusCc
/
OSSDP-Lab2-acceptPR
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OSSDP-Lab2-acceptPR
/
Solution2.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (45 loc) · 1.33 KB
Breadcrumbs
OSSDP-Lab2-acceptPR
/
Solution2.java
Copy path
File metadata and controls
47 lines (45 loc) · 1.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @description:
*
* 给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。
*
*
* 示例 1:
*
* 输入:s = "bcabc"
* 输出:"abc"
* 示例 2:
*
* 输入:s = "cbacdcbc"
* 输出:"acdb"
*
* 1 <= s.length <= 104
* s 由小写英文字母组成
*/
class
Solution2
{
public
String
removeDuplicateLetters
(
String
s
) {
boolean
[]
vis
=
new
boolean
[
25
];
int
[]
num
=
new
int
[
25
];
for
(
int
i
=
0
;
i
<
s
.
length
();
i
++) {
num
[
s
.
charAt
(
i
) -
' '
]++;
}
StringBuffer
sb
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
s
.
length
()+
1
;
i
++) {
char
ch
=
s
.
charAt
(
i
);
if
(!
vis
[
ch
-
' '
]) {
while
(
sb
.
length
() >
0
&&
sb
.
charAt
(
sb
.
length
() -
1
) >
ch
) {
if
(
num
[
sb
.
charAt
(
sb
.
length
() -
1
) -
'a'
] >
0
) {
vis
[
sb
.
charAt
(
sb
.
length
() -
1
) -
'a'
] =
false
;
sb
.
deleteCharAt
(
sb
.
length
() -
1
);
}
else
{
break
;
}
}
vis
[
ch
-
'a'
] =
true
;
sb
.
append
(
ch
);
}
num
[
ch
-
'a'
] +=
1
;
}
return
sb
.
toString
();
}
}
Back
|
FazBrowse Home
|
New Git URL