2016.6.20——Plus One

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);

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

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

 1 class MyClass
 2 {
 3 public:
 4     vector<int> plusOne(vector<int> &digits)
 5     {
 6         int n = digits.size();
 7         for (int i = n-1; i >=0; i--)
 8         {
 9             if (digits[i] == 9)
10             {
11                 digits[i] = 0;
12             }
13             else
14             {
15                 digits[i]++;
16                 return digits;
17             }
18         }
19         digits[0] = 1;
20         digits.push_back(0);
21         return digits;
22     }
23 };

  我的测试代码:

 1 // PlusOne.cpp : 定义控制台应用程序的入口点。
 2 //
 3
 4 #include "stdafx.h"
 5 #include "iostream"
 6 #include "vector"
 7 using namespace std;
 8
 9 class MyClass
10 {
11 public:
12     vector<int> plusOne(vector<int> &digits)
13     {
14         int n = digits.size();
15         for (int i = n-1; i >=0; i--)
16         {
17             if (digits[i] == 9)
18             {
19                 digits[i] = 0;
20             }
21             else
22             {
23                 digits[i]++;
24                 return digits;
25             }
26         }
27         digits[0] = 1;
28         digits.push_back(0);
29         return digits;
30     }
31 };
32
33
34 int _tmain(int argc, _TCHAR* argv[])
35 {
36     vector<int> nums = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9};
37     vector<int> res;
38     MyClass solution;
39     res = solution.plusOne(nums);
40     int n = res.size();
41     for (int i = 0; i < n; i++)
42     {
43         cout << res[i] << " ";
44     }
45     cout << endl;
46     system("pause");
47     return 0;
48 }

  第一次出错的代码:

 1 // Plus One.cpp : 定义控制台应用程序的入口点。
 2 //
 3
 4 #include "stdafx.h"
 5 #include "iostream"
 6 #include "vector"
 7 using namespace std;
 8
 9 class Solution {
10 public:
11     vector<int> plusOne(vector<int>& digits)
12     {
13
14         int res = 0;
15         vector<int> nums;
16         int  d = 0,dd = 1;
17         int n = digits.size();
18         for (int i = 0; i < n; i++)
19         {
20             res = res * 10;
21             res += digits[i];        //1.强制转换的格式(int)(x) 2.digits本就是int型,不需要digits[i] - ‘0‘
22             //cout << res << endl;
23         }
24         res = res + 1;
25
26         int res1 = res;
27         while (res1)
28         {
29             res1 = res1 / 10;
30             d++;
31             dd = dd * 10;
32         }
33
34         dd = dd / 10;
35
36         for (int j = 0; j < d; j++)
37         {
38             //cout << "aa" << endl;
39             nums.push_back(res /dd);            //监视发现nums.size() 为0,因为在之前时,不知道nums的长度,所以没有设定大小。
40             res = res %dd;                        //这样的话用vector中的nums.push_back()。
41             dd = dd / 10;
42         }
43
44         return nums;
45     }
46 };
47
48
49 int _tmain(int argc, _TCHAR* argv[])
50 {
51     vector<int> nums = { 1, 2, 3, 4 };
52     vector<int> res;
53     Solution solution;
54     res = solution.plusOne(nums);
55     int size = res.size();
56     for (int i = 0;i<size; i++)
57     {
58         cout << res[i];
59     }
60     cout << endl;
61     system("pause");
62     return 0;
63 }

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

  

时间: 2024-08-18 10:03:35

2016.6.20——Plus One的相关文章

2016/02/20 codes

<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>2016/02/20</title></head><body><div id="mainDiv"> <div id = "content"> <div id = &qu

2016/7/20 1:18:29 PyQT5 炫酷的左侧导航效果

2016/7/20 1:18:29  完整code from PyQt5.QtWidgets import (QApplication, QWidget, QHBoxLayout, QTreeWidget, QTreeWidgetItem, QGroupBox) from PyQt5.QtGui import QIcon, QPixmap from PyQt5.QtCore import QSize class Bar_Navigation(QWidget): def __init__(self

2016/09/20

1. Python序列化之pickle模块 - 用于[python特有的类型]和[python基本数据类型]间进行转换 - pickle模块提供了四个功能:dumps.dump.loads.load - json更加适合跨语言 基本数据类型的序列化  pickle仅适用于python 复杂类型的序列化 # import json # dic = {'k1': 'v1'} # print(dic, type(dic)) # # res = json.dumps(dic) # 将python的基本数

分布式技术一周技术动态 2016.03.20

分布式系统实践 1. 基于Mesos和Docker的分布式计算平台 https://mp.weixin.qq.com/s?__biz=MzAxMDgzOTA2Mw==&mid=402769128&idx=1&sn=cea3ad1357bd9312acf1768c0a493bfd&scene=1&srcid=0318BTuxT0fsFYwPjpeyuDOa&key=710a5d99946419d90fbc1e7600cce055b6e997d6afafc74c

“耐撕”团队2016.04.20站立会议

1. 时间 : 10:20--10:40  共计20分钟 2. 人员 : Z   郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客:http://www.cnblogs.com/charliePU/), Q 齐嘉亮(博客:http://www.cnblogs.com/dendroaspis-polylepis/), M 张敏(博客:http://www.cnblogs.com/zhangminss/) 3.功能点清单. 序号

2016.9.20小程序--1

在员工管理的GUI练习中加入数据验证.也就是在添加对象进数组之前,先作数据合法性的验证,数据合法再作添加. 姓名:2个以上的字母或汉字 性别:必须是男或女 年龄:必须为数字 电话:13.15.18开始的11位数字 或者  028-99823345 1.员工类 1 public class Staff { 2 private String name ; 3 private int age ; 4 private String sex ; 5 private String tel ; 6 7 8 9

2016.02.20 学习笔记 数据在Activity之间的传递的情况

情况一:一个Activity跳转到另一个Activity时,将第一个Activity的数据传递到第二个Activity里面. 分析:当一个界面跳转到另一个界面的同时还要讲数据传递过去,这种情况需要用Intent类putExtra()方法实现. 具体在Onclick方法中的样例代码如下: Intent i1=new Intent(this,SecondActivity.class); String Message=Edit1.getText().toString(); i1.putExtra("M

project 2016 11 20 树的多项式

#include <iostream>#include <stack>#include <cmath>#include <sstream> using namespace std; bool isoptr(char ch) { if(ch == '+' || ch == '-' || ch == '*' || ch ==  '/' || ch == '^' || ch == 's' || ch == 'c' || ch == 'l' || ch == 'n'

2016第20周六厦门旅游

今天去了闻名已久的厦门最主要的景点鼓浪屿,见面不如闻名,有些地方没去过在心里的感觉比去过之后的感觉高大上多了,虽然亲眼所见才为实,有时候虚一点或许更好,就让那些名不副实的景点在你心中留下个好印象,把时间用在发现真正的美上. 小收获,一般海里的陆地10平安公里以上的成为岛,10平房公里以下的成为屿,鼓浪屿面积1.88平房公里左右,长1.88公里,宽1公里,面积不大,但长住人口1.7w,日游览人数6.5w. 厝读cuo,闽南意思为住宅,比如曾厝安. 筼筜湖.