-
Notifications
You must be signed in to change notification settings - Fork 0
/
nextBigger.js
59 lines (56 loc) · 1.2 KB
/
nextBigger.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
48
49
50
51
52
53
54
55
56
57
58
59
function nextBigger(n){
//your code here
var str = n+"";
var strArr = str.split(""),arr=copyArr(strArr),result,min,toCompareArr = [],isSucce;
for(var i = strArr.length-2;i>=0;i--){
for(var j = i+1;j<=strArr.length-1;j++){
swapIndex(arr,i,j);
result = parseInt(arr.join(""));
if(result>n){
isSucce = true;
var resultArr = arr.splice(i+1,arr.length-i-1);
resultArr.sort(function(x,y){
if(x==y){
return 0;
}
else{
return x>y;
}
});
arr = arr.concat(resultArr);
result = parseInt(arr.join(""));
toCompareArr.push(result);
arr = copyArr(strArr);
}
swapIndex(arr,i,j);
}
if(isSucce){
break;
}
}
if(toCompareArr.length===0){
return -1;
}
else{
var min = toCompareArr[0];
toCompareArr.forEach(function(item,index){
if(item<min){
min = item;
}
});
return min;
}
}
function copyArr(source){
var arr = []
source.forEach(function(item,index){
arr[index] = item;
});
return arr;
}
function swapIndex(arr,i,j){
var tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
nextBigger(1234567890);