find next greater number with same set of digits javascript
def findnext(ii):
iis=list(map(int,str(ii)))
for i in reversed(range(len(iis))):
if i == 0: return ii
if iis[i] > iis[i-1] :
break
left,right=iis[:i],iis[i:]
for k in reversed(range(len(right))):
if right[k]>left[-1]:
right[k],left[-1]=left[-1],right[k]
break
return int("".join(map(str,(left+sorted(right)))))
123456784987654321
start with a number
123456784 987654321
^the first place from the right where the left-digit is less than the right
Digit "x" is 4
123456784 987654321
^find the smallest digit larger than 4 to the right
123456785 4 98764321
^place it to the left of 4
123456785 4 12346789
123456785123446789
^sort the digits to the right of 5. Since all of them except
the '4' were already in descending order, all we need to do is
reverse their order, and find the correct place for the '4'
Also in JavaScript:
- how to print in js
- cdnjs smeantic ui
- how to Check if an array contains a string
- document .ready
- create window electron
- react native object is empty
- reload a child component in angular
- angular maxlength directive input type number
- new features of angular 11
- node js list all installed modules
- react template
- Create a node in the Node Editor or Hypershade, press the ____ and type into search bar:
- nodejs map
- javascript string replace
- react generate component command
- jquery get current row value
- npm react router 6.0.0-alpha.2
- unable to communicate with the paypal gateway in magento 2
- javascript date minus seconds
- orm for postgres node js
- axios react post form data
- react axios download zip file
- javascript string proper case
- js queryselectorall