2016.6.20--plus One

Source: Internet
Author: User

Plus One

The Harvest

The difference between 1.vector<int> and vector<char>, and the conversion with int

convert from vector<int> nums to int res, direct for loop res + = Nums[i];(nums number is int type, no more-' 0 ', char type requires nums[i] ASIIC code minus 0 of the ASIIC code)

Convert from vector<char> nums to int res, then the FOR loop res + = Nums[i]-' 0 ';

2. When the array size is not known, it is represented by size = Nums.size ().

And in the For loop it is best not to use for (int i = 0; i < nums.size (); i++);

And with size = Nums.size (); for (int i = 0; i < size; i++);

  The first time you enter a for loop, you need to calculate the length of the Nums, and the second one only needs to be computed once.

3.4. In the procedure specifically: Nums.push_back in the vector (res% dd)

Topic:

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

The digits is stored such, the most significant digit was at the head of the list.

Given a non-negative number represented by an array. Add this number to 1, the most important number at the top of the list

Test instructions is: Given an array of non-negative numbers, plus 1 returns (or an array form), the highest number of bits in the front of the array.

Ideas:

My idea: Convert an array to an int integer, an int integer plus 1, and the conversion is returned to the group.

The problem is to start with the int type, but found that the number is large, changed to a long long, or not, the number of tests is larger, realize that this problem can not use the idea to solve problems.

Leetcode: judge the end is not 9, if it becomes 0, not add 1, if all bits are 9, then the last 0 position 1, the last one plus a 0,nums[0] = 1; nums.push_back (0);

        Using the array to hold the number, then the number must be very large, so do not consider the array to the number of conversion, the number of methods to the array, out of this problem is not intended to use this method, but another way.

Code: Very beautiful code, the idea is very clear!

1 classMyClass2 {3  Public:4vector<int> PlusOne (vector<int> &digits)5     {6         intn =digits.size ();7          for(inti = n1; I >=0; i--)8         {9             if(Digits[i] = =9)Ten             { OneDigits[i] =0; A             } -             Else -             { thedigits[i]++; -                 returndigits; -             } -         } +digits[0] =1; -Digits.push_back (0); +         returndigits; A     } at};

My Test code:

1 //PlusOne.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"iostream"6#include"Vector"7 using namespacestd;8 9 classMyClassTen { One  Public: Avector<int> PlusOne (vector<int> &digits) -     { -         intn =digits.size (); the          for(inti = n1; I >=0; i--) -         { -             if(Digits[i] = =9) -             { +Digits[i] =0; -             } +             Else A             { atdigits[i]++; -                 returndigits; -             } -         } -digits[0] =1; -Digits.push_back (0); in         returndigits; -     } to }; +  -  the int_tmain (intARGC, _tchar*argv[]) * { $vector<int> nums = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9};Panax Notoginsengvector<int>Res; - MyClass solution; theres =Solution.plusone (nums); +     intn =res.size (); A      for(inti =0; I < n; i++) the     { +cout << Res[i] <<" "; -     } $cout <<Endl; $System"Pause"); -     return 0; -}

The first error code:

1 //Plus One.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"iostream"6#include"Vector"7 using namespacestd;8 9 classSolution {Ten  Public: Onevector<int> PlusOne (vector<int>&digits) A     { -          -         intres =0; thevector<int>nums; -         intD =0, DD =1; -         intn =digits.size ();  -          for(inti =0; I < n; i++) +         { -res = res *Ten; +Res + = Digits[i];//1. The format of the cast (int) (x) 2.digits is the int type, does not need digits[i]-' 0 ' A             //cout << res << Endl; at         } -res = res +1; -          -         intRes1 =Res; -          while(RES1) -         {         inRes1 = res1/Ten; -d++; toDD = DD *Ten; +         } -  theDD = DD/Ten; *  $          for(intj =0; J < D; J + +)Panax Notoginseng         { -             //cout << "AA" << Endl; theNums.push_back (RES/DD);//The Monitor found Nums.size () as 0, because at the previous time, the length of the nums was not known, so no size was set.  +res = res%dd;//In this case, use the Nums.push_back () in the vector.  ADD = DD/Ten; the         } +  -         returnnums; $     } $ }; -  -  the int_tmain (intARGC, _tchar*argv[]) - {Wuyivector<int> nums = {1,2,3,4 }; thevector<int>Res; - solution solution; Wures =Solution.plusone (nums); -     intSize =res.size (); About      for(inti =0;i<size; i++) $     { -cout <<Res[i]; -     } -cout <<Endl; ASystem"Pause"); +     return 0; the}

Simple code or a variety of mistakes Ah!

  

2016.6.20--plus One

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.