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 };
  • 相关阅读:
    《构建之法》阅读报告
    教务管理系统类图及数据库E/R图
    设计模式:抽象工厂
    结对项目:四则运算程序测试
    Leetcode笔记之57和为s的连续正数序列
    Leetcode笔记之1103分糖果 II
    Leetcode笔记之199二叉树的右视图
    每日Scrum(9)
    每日Scrum(7)
    每日Scrum(6)
  • 原文地址:https://www.cnblogs.com/zle1992/p/10161877.html
Copyright © 2011-2022 走看看