[LeetCode]66.Plus One

【题目】

Given a number represented as an array of digits, plus one to the number.

【题意】

给你一个用数组表示的数,求加一之后的结果,结果还是用数组表示。

【分析】

从低位到高位,连续遇到9才能加一进位。

【代码1】

/*********************************
*   日期:2014-01-22
*   作者:SJF0115
*   题号: Plus One
*   来源:http://oj.leetcode.com/problems/plus-one/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        int i;
        for(i = digits.size() - 1;i >= 0;--i){
            if(digits[i] != 9){
                ++digits[i];
                return digits;
            }
            else {
                digits[i] = 0;
            }
        }
        //各位全是9
        if(i < 0) {
            digits.insert(digits.begin(),1);
        }
        return digits;
    }
};
int main() {
    Solution solution;
    vector<int> result;
    vector<int> array = {9,9,9};
    result = solution.plusOne(array);
    int n = result.size();
    for(int i = 0;i < n;i++){
        printf("%d",result[i]);
    }//for
    printf("\n");
    return 0;
}

【代码2】

class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        add(digits,1);
        return digits;
    }
private:
    //模版:数组表示的大数加一个整数(0-9)
    void add(vector<int> &digits,int value){
        int i;
        //进位
        int c = value;
        int n = digits.size();
        for(i = n - 1;i >= 0;i--){
            digits[i] += c;
            c = digits[i] / 10;
            digits[i] %= 10;
        }
        //还有进位
        if(c > 0){
            digits.insert(digits.begin(),c);
        }
    }
};
时间: 2024-08-29 01:28:33

[LeetCode]66.Plus One的相关文章

[LeetCode]135.Candy

[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

代码分析-leetcode:Scramble String问题

问题描述 leetcode:Scramble String问题 题目:Given a string s1 we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = ""great"": great / gr eat / / g r e at /

c++-leetcode Median of Two Sorted Arrays

问题描述 leetcode Median of Two Sorted Arrays class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { if(nums1.size()==0){ if(nums2.size()%2==0)return ((double)nums2[nums2.size()/2]+nums2[nums2.size()/2-1])/2.0; else ret

[LeetCode]61.Rotate List

[题目] Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. [题意] 给定一个链表,向右旋转k个位置,其中k是非负的. [分析] 先遍历一遍,得出链表长度 len,注意 k 可能大于

[LeetCode]91.Decode Ways

题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 - 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encode

Dreamweaver MX 2004视频宝典教程(66)

dreamweaver|教程 第 66 集:为什么应用CSS 课程目标:掌握应用CSS呈现的好处. 课程要点:应用CSS可以方便的控制和调整内容的呈现,并且可以大大减少网页的文件大小. [全屏观看] | [下载视频] 本教程尺寸为 800 * 600 建议全屏在线观看或下载观看,以达到最佳观看效果

LeetCode总结之二分查找篇

二分查找算法虽然简单,但面试中也比较常见,经常用来在有序的数列查找某个特定的位置.在LeetCode用到此算法的主要题目有: Search Insert Position Search for a Range Sqrt(x) Search a 2D Matrix Search in Rotated Sorted Array Search in Rotated Sorted Array II 这类题目基本可以分为如下四种题型: 1. Search Insert Position和Search fo