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.

// https://oj.leetcode.com/problems/plus-one/
// Author : Chao Zeng
// Date   : 2015-1-29
class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        vector <int> numbers;
        reverse(digits.begin(),digits.end());
        int length = digits.size();
        digits[0]++;
        int temp;
        for (int i = 0; i < length; i++){
            //注意语句的顺序
            temp = digits[i] / 10;
            digits[i] = digits[i] % 10;
            numbers.push_back(digits[i]);
            //进位的处理
            if (i == length - 1 && temp > 0){
                numbers.push_back(temp);
            }
            else{
                digits[i+1] = digits[i+1] + temp;
            }
        }
        reverse(numbers.begin(),numbers.end());
        return numbers;
    }
};
时间: 2024-10-13 00:28:59

LeetCode:Plus One的相关文章

LeetCode:Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 动态规划解法 T(n) = O(n^2)  ,S(n) = O(n^2); Solutio

leetcode:Pascal&amp;#39;s Triangle

一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数的和值 须要注意的是,当行数为0时输出[[1]] 结果为一个二维数组,所以不难想到解决方式. 每层保存前一行的指针,然后当前行数据依据上一行来得到,每一个元素就是上一行两个相邻元素相加(第一个和最后一个元素是1). 算法时间复杂度应该是O(1+2+3+...+n)=O(n^2),空间上仅仅须要二维数

LeetCode: Triangle 题解

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

LeetCode: Permutations II 题解

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

LeetCode:(Array-189) Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

LeetCode:Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. Solution: class Solution { public: int removeElement(int

LeetCode:Combinations 题解

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]DFS,递归 1 class Solution { 2 public: 3 vector<vector<int> > an

LeetCode: Valid Parentheses 题解

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

leetcode:Pascal&#39;s Triangle

一.     题目 经典题目,杨辉三角,输入行数,生成杨辉三角的数组. 二.     分析 首先,我们知道有如下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数的和值 需要注意的是,当行数为0时输出[[1]] 结果为一个二维数组,所以不难想到解决方案. 每层保存前一行的指针,然后当前行数据根据上一行来得到,每个元素就是上一行两个相邻元素相加(第一个和最后一个元素是1).算法时间复杂度应该是O(1+2+3+...+n)=O(n^2),空间上只需要二维数组来存储结

LeetCode:位运算实现加法

LeetCode:位运算实现加法 写在前面 位运算符 实现加法的思路 两个加数,比如5(101)和6(110),如何不用加法就能得出两者之和呢? 我们知道二进制计算中,如果使用异或将会产生无进位的两者之和,而两数相与将会产生进位值!!! 可这样又如何呢? 1 sum = 011 2 carry =1000 两者继续异或将会产生 结果就出现了,此时无进位,所以进位为0时,sum将会为最终结果!因为此时不需要进位,异或运算就是最终结果! 优质代码 1 public int getSum(int a,