[php] PHP

Viewer

  1. <?php 
  2. $var = 'function nextBigger(n) {
  3.   // Separate the left and right parts by splitDigits
  4.   const [left, right] = splitDigits(`${n}`.split(\'\'), 2)
  5.   if (!left) return -1
  6.      // Rearrange the right part and combine the return value with the left group
  7.   return Number(left.concat(resort(right)).join(\'\'))
  8. }
  9.  
  10.  // Split the digits array according to rightSize, if not and the specification, recursively split by rightSize+1
  11. function splitDigits(digits, rightSize) {
  12.   if (rightSize > digits.length) return []
  13.  
  14.   const right = digits.slice(-rightSize)
  15.      // Determine if right meets the requirements
  16.   if (right[0] < right[1]) return [digits.slice(0, -rightSize), right]
  17.  
  18.   return splitDigits(digits, rightSize + 1)
  19. }
  20.  
  21. function resort(right) {
  22.   const first = right[0]
  23.      // here both sort and reverse
  24.   const rest = right.slice(1).sort()
  25.  
  26.      // find the index of the next larger number
  27.   const idx = rest.findIndex(n => n > first)
  28.   const p = rest[idx]
  29.   rest[idx] = first
  30.   return [p].concat(rest)
  31. }';

Editor

You can edit this paste and save as new:


File Description
  • PHP
  • Paste Code
  • 24 Jun-2021
  • 1.02 Kb
You can Share it: