Leetcode 66 Plus One STL

题意让大数加1

我的做法是先让个位+1,再倒置digits,然后进位,最后倒置digits,得到答案。

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int> &digits) {
 4         digits[digits.size() -1]++; //个位+1
 5         reverse(digits.begin(),digits.end());//倒置digits
 6         for(vector<int>::size_type i = 0; i < digits.size() - 1; ++i){//除了最高位,进位
 7             if(digits[i] >= 10){
 8                 digits[i] -= 10;
 9                 digits[i+1] ++;
10             }
11         }
12         if(digits[digits.size() - 1] >= 10){//最高位进位
13             digits[digits.size() - 1] -= 10;
14             digits.push_back(1);
15         }
16         reverse(digits.begin(),digits.end());//倒置digits
17         return digits;
18     }
19 };
时间: 2024-08-13 00:14:33

Leetcode 66 Plus One STL的相关文章

LeetCode 66. 加1

题目: 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123. 示例2: 输入: [4,3,2,1] 输出: [4,3,2,2] 解释: 输入数组表示数字 4321. 思路与解答: 题解一: 这道题最直观的方法就是首先判断末位是否为9,不为9,直接加1返回,具体为: 从末位开

leetCode 66. Plus One 数组

66. 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. 题目大意:将一个数字的各位都放在一个数组中,给这个数字加1,求得到的新数组. 高位在前. class Solution { p

LeetCode(66)题解: Plus One

https://leetcode.com/problems/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. 思路: 设置进1位,逢9进1.注意999这种情况要首位inser

LeetCode 66. Plus One(加1)

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any leading zero, except the number 0 itself. The digits are stored such that the most significant digit is at

leetcode—66 Plus One(数组中的值+1进位操作,数组扩充)

Plus One 66  Total Accepted: 48227 Total Submissions: 157869 My Submissions Question Solution 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

[leetcode] 66. 加一

66. 加一 模拟加法运算,很简单 注意进位即可 class Solution { public int[] plusOne(int[] digits) { int k = digits.length - 1; digits[k] += 1; while (k > 0) { if (digits[k] < 10) break; digits[k] -= 10; digits[k - 1] += 1; k--; } if (digits[0] >= 10) { digits[0] -= 1

[leetcode] 66. Plus One

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any leading zero, except the number 0 itself. The digits are stored such that the most significant digit is at

[C++]LeetCode: 66 Single Number

题目: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 思路: 要求线性时间完成,并且不使用额外的存储空间. **非常之巧妙** 使用

leetcode 66. 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. 题解:简单的高精度加法,要注意[9]这种输入,要多一位. class Solution { public: vector<int> plusOne(v