Java 1ms solution use the amount of number "9"


  • 0
    C

    public int[] plusOne (int[] digits)
    {

    int length = digits.length;
      int amountOf9 = 0;
    
      for(int i = length - 1 ; i >= 0 ; i--){
          if(digits[i] != 9){
              break;
          }else{
              amountOf9++;
          }
      }
    
      if(amountOf9 == length){
          int[] result = new int[length+1];
          result[0] = 1;
          for(int i = 1;i<length;i++)
          {
              result[i] = 0;
          }
          return result;
      }
      
      digits[length - 1 - amountOf9] += 1;
      
      for(int i = length-amountOf9; i < length;i++){
          digits[i] = 0;
      }
      
      return digits;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.