[php] PHP
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- <?php
- $var = 'function nextBigger(n) {
- // Separate the left and right parts by splitDigits
- const [left, right] = splitDigits(`${n}`.split(\'\'), 2)
- if (!left) return -1
- // Rearrange the right part and combine the return value with the left group
- return Number(left.concat(resort(right)).join(\'\'))
- }
- // Split the digits array according to rightSize, if not and the specification, recursively split by rightSize+1
- function splitDigits(digits, rightSize) {
- if (rightSize > digits.length) return []
- const right = digits.slice(-rightSize)
- // Determine if right meets the requirements
- if (right[0] < right[1]) return [digits.slice(0, -rightSize), right]
- return splitDigits(digits, rightSize + 1)
- }
- function resort(right) {
- const first = right[0]
- // here both sort and reverse
- const rest = right.slice(1).sort()
- // find the index of the next larger number
- const idx = rest.findIndex(n => n > first)
- const p = rest[idx]
- rest[idx] = first
- return [p].concat(rest)
- }';
Editor
You can edit this paste and save as new: