LeetCode之“数学”:Plus One
题目要求:
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.
这道题不难,不过可能会误解是二进制数的相加,切记!程序如下:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
vector<int> ret;
int sz = digits.size();
if(sz == )
return ret;
int carry = ;
for(int i = sz - ; i > -; i--)
{
if(carry + digits[i] == )
ret.insert(ret.begin(), );
else
{
ret.insert(ret.begin(), carry + digits[i]);
carry = ;
}
}
if(carry == )
ret.insert(ret.begin(), );
return ret;
}
};
LeetCode之“数学”:Plus One的更多相关文章
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- LeetCode之“数学”:Happy Number
题目链接 题目要求: Write an algorithm to determine if a number is "happy". A happy number is a num ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- LeetCode之“数学”:Rectangle Area
题目链接 题目要求: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle i ...
- [LeetCode]实现数学计算
乘方 思路是:pow(x,n) = pow(x,n/2)*pow(x,n-n/2) 递归实现 public double myPow(double x, int n) { if (n==0) retu ...
- leetcode必刷200题
一.数据结构相关 链表 1. 相交链表 2. 反转链表 3. 合并两个有序链表 4. 删除排序链表中的重复元素 5. 删除链表的倒数第 n 个节点 6. 两两交换链表中的节点 7. 两数相加 II 8 ...
- LeetCode初级算法的Python实现--排序和搜索、设计问题、数学及其他
LeetCode初级算法的Python实现--排序和搜索.设计问题.数学及其他 1.排序和搜索 class Solution(object): # 合并两个有序数组 def merge(self, n ...
- leetcode 343. Integer Break(dp或数学推导)
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...
随机推荐
- EBS中使用JAVA方式发送HTML格式邮件
转自huan.gu专栏:http://blog.csdn.net/gh320/article/details/17174769 EBS中使用JAVA方式发送HTML格式邮件 一.开发工具:JDevel ...
- 微信小程序基础之创建使用教程
本文档将带你一步步创建完成一个微信小程序,并可以在手机上体验该小程序的实际效果.这个小程序的首页将会显示欢迎语以及当前用户的微信头像,点击头像,可以在新开的页面中查看当前小程序的启动日志. 1. 获取 ...
- Makefile自动生成
automake/autoconf入门作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下,大家都是手工写一个简单Makefile ...
- Storm并发机制详解
本文可作为 <<Storm-分布式实时计算模式>>一书1.4节的读书笔记 在Storm中,一个task就可以理解为在集群中某个节点上运行的一个spout或者bolt实例. 记住 ...
- Android View框架总结(八)ViewGroup事件分发机制
请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52298780 上篇分析了View的事件分发流程,留了一个问题:如果上 ...
- Java基础---Java---面试题---交通灯管理系统(面向对象、枚举)
交通灯管理系统的项目需求: 模拟实现十字路口的交通灯管理系统逻辑,具体需求如下: 1.异步随机生成按照各个路线行驶的车辆 例如: 由南向而来去往北向的车辆-----直行车辆 由西向而来去往南 ...
- 1、Android测试入门
编写和运行测试时Android APP开发周期中的重要的一环.好的测试可以让你非常容易的在开发过程中发现bug,提升你对自己代码的自信.使用Android Studio,你可以在物理设备或者虚拟机中运 ...
- 1085. Perfect Sequence (25) -二分查找
题目如下: Given a sequence of positive integers and another positive integer p. The sequence is said to ...
- 【转载】图灵AngularJS入门教程
摘自图灵的AngularJS入门教程:http://www.ituring.com.cn/article/13471 感觉非常不错,所以推荐到首页一下! (一)Hello World! 开始学习Ang ...
- Hessian源码分析--HessianSkeleton
HessianSkeleton是Hessian的服务端的核心,简单总结来说:HessianSkeleton根据客户端请求的链接,获取到需要执行的接口及实现类,对客户端发送过来的二进制数据进行反序列化, ...