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

Commit1f63332

Browse files
authored
Merge pull requestneetcode-gh#1923 from AkifhanIlgaz/0402
Create: 0402-remove-k-digits.rs / .ts /.js
2 parents736249f +d5db5dd commit1f63332

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

‎javascript/0402-remove-k-digits.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
*@param {string} num
3+
*@param {number} k
4+
*@return {string}
5+
*/
6+
varremoveKdigits=function(num,k){
7+
letstack=[];
8+
for(chofnum){
9+
while(k>0&&stack.length>0&&stack.at(-1)>ch){
10+
k--;
11+
stack.pop();
12+
}
13+
stack.push(ch);
14+
}
15+
16+
letx=0;
17+
while(true){
18+
if(stack[x]!=='0'){
19+
break;
20+
}
21+
x++;
22+
}
23+
stack=stack.slice(x,stack.length-k);
24+
letres=stack.join('');
25+
returnres ?res :'0';
26+
};

‎rust/0402-remove-k-digits.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
implSolution{
2+
pubfnremove_kdigits(num:String,k:i32) ->String{
3+
letmut stack =vec![];
4+
letmut k = kasusize;
5+
6+
for chin num.chars(){
7+
whileletSome(&top) = stack.last(){
8+
if k >0 && top > ch{
9+
k -=1;
10+
stack.pop();
11+
}else{
12+
break;
13+
}
14+
}
15+
16+
stack.push(ch);
17+
}
18+
19+
// stack = stack[..stack.len() - k].to_vec();
20+
while k !=0{
21+
stack.pop();
22+
k -=1;
23+
}
24+
25+
let res:String = stack.into_iter().skip_while(|&ch| ch =='0').collect();
26+
if res.is_empty(){
27+
"0".to_string()
28+
}else{
29+
res
30+
}
31+
}
32+
}

‎typescript/0402-remove-k-digits.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
functionremoveKdigits(num:string,k:number):string{
2+
letstack:string[]=[];
3+
for(letchofnum){
4+
while(k>0&&stack.length>0&&stack[stack.length-1]>ch){
5+
k--;
6+
stack.pop();
7+
}
8+
stack.push(ch);
9+
}
10+
11+
letx=0;
12+
while(true){
13+
if(stack[x]!=='0'){
14+
break;
15+
}
16+
x++;
17+
}
18+
stack=stack.slice(x,stack.length-k);
19+
letres=stack.join('');
20+
returnres ?res :'0';
21+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp