Plus One

本题收获

1.vector<int> 和vector<char>的区别,及与int转换

  从vector<int> nums 转换为int res型,直接for循环 res += nums[i];(nums中的数本就是int型,不需要再-‘0’,char型则需要用nums[i] 的ASIIC码减去 0 的ASIIC码)

  从vector<char> nums 转换为int res ,则是for循环 res += nums[i] - '0';

2.不知道数组具体大小时,用size = nums.size() 表示。

  且在for循环中最好不要用for(int i = 0; i < nums.size(); i++);

  而用 size = nums.size() ; for(int i = 0; i < size; i++ );

  第一种每次进入for循环都需要计算一遍nums的长度,而第二种只需要计算一次即可。

3.4.在程序中具体说:有关vector中 nums.push_back(res % dd)

  题目:

  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,最重要的数在列表的最前面

  题意是:给定一个用数组表示的非负数,加1后返回(还是一个数组形式),位数最高的在数组的最前面。

  思路:

    我的思路:将数组转化为int整数,int整数加1后,转化为数组返回。

          出现问题是开始用int型,但是发现数很大,改成long long,还是不行,测试的数更大,就意识到这道题不能用这用的思路来解题。

    leetcode:判断末尾是不是9,若果是变为0,不是则加1,若所有位都是9,则最后将0位置1,最后一位加个0,nums[0] = 1; nums.push_back(0);

        用数组存放数,那么数肯定很大,所以不要考虑用数组转数,数转为数组的方法,出这道题的本意不是用这种方法,而是另想办法。

  代码:  非常漂亮的代码,思路也很清晰!

 class MyClass
{
public:
vector<int> plusOne(vector<int> &digits)
{
int n = digits.size();
for (int i = n-; i >=; i--)
{
if (digits[i] == )
{
digits[i] = ;
}
else
{
digits[i]++;
return digits;
}
}
digits[] = ;
digits.push_back();
return digits;
}
};

  我的测试代码:

 // PlusOne.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
#include "vector"
using namespace std; class MyClass
{
public:
vector<int> plusOne(vector<int> &digits)
{
int n = digits.size();
for (int i = n-; i >=; i--)
{
if (digits[i] == )
{
digits[i] = ;
}
else
{
digits[i]++;
return digits;
}
}
digits[] = ;
digits.push_back();
return digits;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
vector<int> nums = {,,,,,,,,,,,,,,,,,};
vector<int> res;
MyClass solution;
res = solution.plusOne(nums);
int n = res.size();
for (int i = ; i < n; i++)
{
cout << res[i] << " ";
}
cout << endl;
system("pause");
return ;
}

  第一次出错的代码:

 // Plus One.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
#include "vector"
using namespace std; class Solution {
public:
vector<int> plusOne(vector<int>& digits)
{ int res = ;
vector<int> nums;
int d = ,dd = ;
int n = digits.size();
for (int i = ; i < n; i++)
{
res = res * ;
res += digits[i]; //1.强制转换的格式(int)(x) 2.digits本就是int型,不需要digits[i] - '0'
//cout << res << endl;
}
res = res + ; int res1 = res;
while (res1)
{
res1 = res1 / ;
d++;
dd = dd * ;
} dd = dd / ; for (int j = ; j < d; j++)
{
//cout << "aa" << endl;
nums.push_back(res /dd); //监视发现nums.size() 为0,因为在之前时,不知道nums的长度,所以没有设定大小。
res = res %dd; //这样的话用vector中的nums.push_back()。
dd = dd / ;
} return nums;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
vector<int> nums = { , , , };
vector<int> res;
Solution solution;
res = solution.plusOne(nums);
int size = res.size();
for (int i = ;i<size; i++)
{
cout << res[i];
}
cout << endl;
system("pause");
return ;
}

  简单的代码还是各种出错啊!

  

2016.6.20——Plus One的更多相关文章

  1. Stack Overflow 2016年度 20个最佳Python问题(一)

    Stack Overflow 2016年度 20个最佳Python问题(一) https://zhuanlan.zhihu.com/p/25020763

  2. 2016.9.20 java上课作业

    此程序从命令行接收多个数字,求和之后输出

  3. 2016 - 2 - 20 ARC知识总结(二 autorelease概念及实现)

    首先祝自己生日快乐~23咯~ 一  autorelease的概念 autorelease会像C语言的自动变量那样来对待对象实例.当超出作用域(相当于变量作用域)时,对象的实例release实力方法被调 ...

  4. project 2016 11 20 树的多项式

    #include <iostream>#include <stack>#include <cmath>#include <sstream> using ...

  5. 从零开始HTML(三 2016/9/20)

    1.HTML表单 HTML 表单用于搜集不同类型的用户输入.<form> 元素,HTML 表单用于收集用户输入.<form> 元素 ①<input> 元素,< ...

  6. 从零开始HTML(二 2016/9/20)

    1.布局 ① 使用 <div> 元素的 HTML 布局,<div> 元素常用作布局工具,因为能够轻松地通过 CSS 对其进行定位. <!DOCTYPE html> ...

  7. 2016.6.20 计算机网络复习要点第三章之CSMA/CD协议

    1.最早的以太网是将许多计算机都连接到一根总线上: (1)总线的特点是:当一台计算机发送数据时,总线上的所有计算机都检测到这个数据,这种就是广播通信方式: (2)为了实现在总线上的一对一通信,可以使每 ...

  8. 2016 - 1 - 20 runloop学习(2)

    一:CFRunLoopModeRef 1. CFRunLoopModeRef带表RunLoop的运行模式 2. 一个Runloop可以有若干个mode,每个mode又包含若干个sourse,timer ...

  9. 2016 - 1 - 20 runloop学习

    一:Runloop基本知识 1.本质就是运行循环 2.基本作用: 2.1保证程序持续运行 2.2处理APP中的各种事件:触摸,定时器,selector... 2.3节省CPU资源,系统程序性能:它会让 ...

  10. 详解YUV与RGB数据格式-2016.01.20

    参考文献 什么是I帧,P帧,B帧 图文详解YUV420数据格式 RGB565 与 RGB888的相互转换 最简单的基于FFmpeg的libswscale的示例(YUV转RGB)

随机推荐

  1. Hibernate 之主键生成策略小总结

    主键生成策略大致分两种: 手工控制策略 自动生成策略[框架自动生成和数据库自动生成] 手工控制策略: assigned:类型是任意的,需要在 save() 到数据库前,编码人员手工设置主键值,也就是调 ...

  2. mysql中字符串类型char(n)和varchar(n)的区别

    n的含义 根据网络上找到的结果(不能保证准确),在5.0.3以后版本中,n均代表字符数,而不是字节数:我用来测试的版本是5.7.20,该版本中,n表示字符数. 验证过程如下 建表 CREATE TAB ...

  3. 用python和unittest编写app自动化测试用例

    import unittest import webdriver import time class Test(unittest.TestCase): @classmethod def setUpCl ...

  4. 多进程编程之守护进程Daemonize

    1.守护进程 守护进程(daemon)是一类在后台运行的特殊进程,用于执行特定的系统任务.很多守护进程在系统引导的时候启动,并且一直运行直到系统关闭.另一些只在需要的时候才启动,完成任务后就自动结束. ...

  5. TeX-换行换页与段落命令

    换行换页与段落命令1 UTF8nsung Abstract 文档在排版时往往要求每一行具有相同的长度, LATEX 为了对整段的文挡进行优化,将插入必要的换行和空恪.如果必要的话对于一行中不好放的单词 ...

  6. Mac突然连不上WiFi

    标签(空格分隔): 杂七杂八的问题 从昨晚开始,Mac突然连不上WiFi了,人又在图书馆,上不了网好焦急.于是搜了很多方法,也不知是哪个起作用了,反正现在可以了. 步骤一 打开"Finder ...

  7. C++ STL 常用算术和生成算法

    C++ STL 常用算术和生成算法 accumulate() accumulate: 对指定范围内的元素求和,然后结果再加上一个由val指定的初始值. #include<numeric> ...

  8. Valid Parentheses - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Valid Parentheses - LeetCode 注意点 考虑输入为空的情况 解法 解法一:如果是'('.'{'.'['这三者就入栈,否则就判断栈 ...

  9. Monitor WMIExportsToC++Use DiskCleanup bypass UAC

    作者:嘶吼吼链接:https://zhuanlan.zhihu.com/p/23473665来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. About: Use odb ...

  10. Python 爬虫入门(一)

    毕设是做爬虫相关的,本来想的是用java写,也写了几个爬虫,其中一个是爬网易云音乐的用户信息,爬了大概100多万,效果不是太满意.之前听说Python这方面比较强,就想用Python试试,之前也没用过 ...