zoukankan      html  css  js  c++  java
  • 66. Plus One

     
    Easy

    Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

    The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

    You may assume the integer does not contain any leading zero, except the number 0 itself.

    Example 1:

    Input: [1,2,3]
    Output: [1,2,4]
    Explanation: The array represents the integer 123.
    

    Example 2:

    Input: [4,3,2,1]
    Output: [4,3,2,2]
    Explanation: The array represents the integer 4321.
     1 class Solution:
     2     def plusOne(self, digits):
     3         """
     4         :type digits: List[int]
     5         :rtype: List[int]
     6         """
     7         if len(digits) == 0:
     8             return []
     9         digits = list(map(str,digits))
    10 
    11         n = ''.join(digits)
    12         res = int(n)+1
    13         res = list(str(res))
    14         res =  list(map(int,res))
    15         return list(res)
     1 class Solution {
     2 public:
     3     vector<int> plusOne(vector<int>& digits) {
     4         int n = digits.size();
     5 
     6         for(int i=n-1;i>=0;i--){
     7             if(digits[i]==9){
     8                 digits[i] = 0;
     9       
    10             }
    11             else{
    12                 digits[i]+=1; //加一位马上跑,如果没跑成功,说明最终有进位999+1 =1000
    13                 return digits;
    14                 
    15             
    16             }
    17         }
    18       
    19             digits[0] =1;
    20             digits.push_back(0);
    21         
    22          return digits;
    23     }
    24        
    25 };
  • 相关阅读:
    BZOJ 1218: [HNOI2003]激光炸弹( 前缀和 + 枚举 )
    BZOJ 1878: [SDOI2009]HH的项链( BIT )
    BZOJ 1054: [HAOI2008]移动玩具( BFS )
    js-提取行间元素
    vim的三种模式的基本操作
    Linux的高级命令
    Linux的进阶命令
    Linux的基本命令
    Linux常见的文件目录结构
    js-操作属性
  • 原文地址:https://www.cnblogs.com/zle1992/p/10161877.html
Copyright © 2011-2022 走看看