leetcode[67] Plus One
题目:对一个用vector存的数字进行加1,然后返回加1后的值。
一次就在oj上通过了。
就是进位加上当前位如果大于9,那就当前位等于0;
随后进位还为1的话就是在数组前面插入一个1;
class Solution {
public:
vector<int> plusOne(vector<int> &digits)
{
int up = ;
int len = digits.size() - ;
while(len >= )
{
if (digits[len] + up > )
{
digits[len] = ;
}
else
{
digits[len] += up;
return digits;
}
len--;
}
digits.insert(digits.begin(), );
return digits;
}
};
2015/03/29:
python:
class Solution:
# @param digits, a list of integer digits
# @return a list of integer digits
def plusOne(self, digits):
up = 1
length = len(digits) - 1
for i in range(len(digits)):
digits[length-i] += up
up = digits[length-i]/10
digits[length-i] %= 10
if up == 1:
digits.insert(0, 1)
return digits
leetcode[67] Plus One的更多相关文章
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- leetcode 67
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- LeetCode(67)题解: Add Binary
https://leetcode.com/problems/add-binary/ 题目: Given two binary strings, return their sum (also a bin ...
- LeetCode 67. Add Binary (二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- Java实现 LeetCode 67 二进制求和
67. 二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1 ...
- Leetcode 67 Add Binary 大数加法+字符串处理
题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...
- LeetCode 67. Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- Java [Leetcode 67]Add Binary
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...
随机推荐
- ashx一般处理程序和HttpHandler
asp.net项目中,使用.ashx的文件(一般处理程序)可以用于处理客户端发送来的请求,并将服务器端的处理结果返回给客户端.它能返回的类型可以是文本.或者图片.有时候,我们可以在项目中使用.cs的文 ...
- 使用php+swoole对client数据实时更新(二) (转)
上一篇提到了swoole的基本使用,现在通过几行基本的语句来实现比较复杂的逻辑操作: 先说一下业务场景.我们目前的大多数应用都是以服务端+接口+客户端的方式去协调工作的,这样的好处在于不论是处在何种终 ...
- Mozilla 构建系统(转)
英文来源:Mozilla’s Build System 中文出处:开放博客,由灰狐翻译小组制作 Mozilla 构建系统是一个非常酷的分布式系统,运行在BuildBot上.系统能在每次修改后自动重新构 ...
- HDU 1074 Doing Homework(像缩进DP)
Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...
- Git Config(转)
一.Git已经在你的系统中了,你会做一些事情来客户化你的Git环境. 你只需要做这些设置一次:即使你升级了,他们也会绑定到你的环境中.你也可以在任何时刻通过运行命令来重新更改这些设置. ...
- HDU 4283 You are the one(间隔DP)
标题效果: The TV shows such as You Are the One has been very popular. In order to meet the need of boys ...
- 关于 pthread_cond_wait 和 pthread_cond_signal , signal 无效的问题
关于一个消费者模式,,,引起的问题.. 我在io线程里不断的把一个函数调用放到队列里 然后ruby线程就不断的从这个队列里取出函数之争并运行. 典型的 消费者模式. 我曾经以为是这样... 这是wor ...
- HDU 3366 Passage (概率DP)
Passage Problem Description Bill is a millionaire. But unfortunately he was trapped in a castle. The ...
- Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL
http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php- ...
- 怎样在多线程中使用JNI?
假设你想了解JNI在怎样在多线程下使用 假设你在子线程使用JNI时遇到findClass不能找到目标Class,而在主线程下却能找到该Class的问题.或是GetEnv返回NULL的问题 假设你想多学 ...