-
Notifications
You must be signed in to change notification settings - Fork 2
/
1417.重新格式化字符串.js
48 lines (46 loc) · 1008 Bytes
/
1417.重新格式化字符串.js
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
47
/*
* @lc app=leetcode.cn id=1417 lang=javascript
*
* [1417] 重新格式化字符串
*/
// @lc code=start
/**
* @param {string} s
* @return {string}
*/
var reformat = function(s) {
var nums = [];
var alph = [];
var res = [];
for(var i=0;i<s.length;i++){
if(s[i]>='0' && s[i]<='9'){
nums.push(s[i]);
}else{
alph.push(s[i]);
}
}
if(Math.abs(nums.length-alph.length)>1){
return "";
}
if(nums.length>alph.length){
while(nums.length>1){
res.push(nums.shift());
res.push(alph.shift());
}
res.push(nums.shift());
}
else if(nums.length<alph.length){
while(alph.length>1){
res.push(alph.shift());
res.push(nums.shift());
}
res.push(alph.shift());
}else{
while(alph.length){
res.push(alph.shift());
res.push(nums.shift());
}
}
return res.join("");
};
// @lc code=end