zoukankan      html  css  js  c++  java
  • Leetcode Plus One

    //Given a non-negative number represented as an array of digits, plus one to the number.

    //The digits are stored such that the most significant digit is at the head of the list.

    //digits={9,9,9,9},那么经过函数运算变为{1,0,0,0},也就是把vector中各位存储的数字看成一个整数的各个位,digits[0]为最高位

    class Solution {
    public:
        vector<int> plusOne(vector<int> &digits) {
           
           if(digits.size() == 0){
               digits.push_back(1);
               return digits;
           } 
           
           int carry = 0;
           int i = 0;
           int current;
           int size = digits.size();
           for(i = size - 1; i >=0 ; i--){
               if(i == size -1){
                   current = (digits[i] + 1); 
               } else {
                   current = (digits[i] + carry);
               }
               
               carry = current / 10;
               digits[i] = current % 10;
           }
           if(carry != 0){
               digits.insert(digits.begin(), carry);
           }
           return digits;
           
        }
    };


  • 相关阅读:
    Qt之镜像旋转
    Qt之QCheckBox
    Qt之动画框架
    Qt之QFileSystemWatcher
    Qt之qSetMessagePattern
    Qt之qInstallMessageHandler(重定向至文件)
    Qt之qInstallMessageHandler(输出详细日志)
    Qt之窗体透明
    Qt之窗体拖拽、自适应分辨率、自适应大小
    Qt之设置应用程序图标
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4035250.html
Copyright © 2011-2022 走看看