题目

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.

分析

该题目要求:将一整数按位存储在vector中,对其实现+1操作,返回结果。

是一道简单题目,对存储序列vector中元素,倒序遍历,末位+1,若<10可直接返回,否则,保存进位加之到下一位,循环至最高位。

若此时,进位依然为1,则新建长度增一的vector首位为1,其余为0,返回即可。

AC代码


class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int len = digits.size(); if (len == 0)
return digits; int carry = 1;
for (int i = len - 1; i >= 0; --i)
{
digits[i] += carry;
if (digits[i] >= 10)
{
digits[i] -= 10;
carry = 1;
continue;
}
else{
carry = 0;
break;
}
} if (carry == 0)
return digits;
else
{
vector<int> v;
v.push_back(1);
for (int i = 0; i < len; i++)
v.push_back(0);
return v;
}
}
};

GitHub测试程序源码

LeetCode(66)Plus One的更多相关文章

  1. LeetCode(66): 加一

    Easy! 题目描述: 给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会 ...

  2. Qt 学习之路 2(66):访问网络(2)

    Home / Qt 学习之路 2 / Qt 学习之路 2(66):访问网络(2) Qt 学习之路 2(66):访问网络(2)  豆子  2013年10月31日  Qt 学习之路 2  27条评论 上一 ...

  3. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  4. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  5. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  6. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  7. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  8. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  9. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

随机推荐

  1. C#XmlDocument输出字符串

    XmlDocument xmlDoc = new XmlDocument(); //假定该xmlDoc已经有内容; MemoryStream streamXml = new MemoryStream( ...

  2. GCD = XOR(GCD XOR )

    首先没看懂XOR(于是百度了一下):异或,英文为exclusive OR,或缩写成xor.同时还有一个OR,于是一起看了一眼: 大意: 输入一个整数n,在1~n内,有多少对整数(a,b)满足GCD(a ...

  3. Hdu 5459 Jesus Is Here (2015 ACM/ICPC Asia Regional Shenyang Online) 递推

    题目链接: Hdu 5459 Jesus Is Here 题目描述: s1 = 'c', s2 = 'ff', s3 = s1 + s2; 问sn里面所有的字符c的距离是多少? 解题思路: 直觉告诉我 ...

  4. Educational Codeforces Round 19 B

    Description You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to f ...

  5. 微信小程序生成分享图片,保存到本地

    1.页面 <canvas canvas-id="shareCanvas" style="width:600px;height:900px">< ...

  6. LN : leetcode 516 Longest Palindromic Subsequence

    lc 516 Longest Palindromic Subsequence 516 Longest Palindromic Subsequence Given a string s, find th ...

  7. 洛谷P4013 数字梯形问题(费用流)

    题意 $N$行的矩阵,第一行有$M$个元素,第$i$行有$M + i - 1$个元素 问在三个规则下怎么取使得权值最大 Sol 我只会第一问qwq.. 因为有数量的限制,考虑拆点建图,把每个点拆为$a ...

  8. android 防止bitmap 内存溢出

    在android开发过程中经常会处理网络图片发送内存溢出,那么怎么解决这种问题? 思路: 下载到本地 通过网络获取和文件下载存放到手机中目录 代码: // 获取网络 public InputStrea ...

  9. webapi之fiddler头设置

    Host: localhost:16648Connection: keep-aliveContent-Length: 36Accept: application/json, text/javascri ...

  10. JS通过使用PDFJS实现基于文件流的预览功能

    需求: 使用JS实现PDF文件预览功能 备选方案: 使用ViewerJS,官网  http://viewerjs.org/ 使用PDFJS,官网  https://mozilla.github.io/ ...