[leetcode]66Plus One
/**
* Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any leading zero, except the number 0 itself. The digits are stored such that the most significant digit is at the head of the list.
*/
/*
* 从最后一位到第二位倒着遍历,重写每位的数据,如果遇到不进位的情况,直接返回数据
* 由于第一位关系到是否需要新建数组,所以单独判断,只要运行完循环,肯定是有进位的,因为如果没有的话就返回了
* 所以不需要设置进位标志*/
public class Q66PlusOne {
public int[] plusOne(int[] digits) {
int l = digits.length;
for (int i = l-1; i >=1 ; i--) {
int cur = digits[i] + 1;
digits[i] = cur % 10;
if (cur / 10 == 0)
return digits;
}
if (digits[0] < 9)
{
digits[0] += 1;
return digits;
}
else
{
int[] res = new int[l+1];
res[0] = 1;
for (int i = l;i > 0;i-- )
{
res[i] = 0;
}
return res;
}
}
}
[leetcode]66Plus One的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- sqlmap工具的简单使用
0x00 sqlmap简介:sqlmap是一款针对sql漏洞的自动化注入工具,有一个非常棒的特性,即对检测与利用的自动化处理(数据库指纹.访问底层文件系统.执行命令). 官方网站下载http://sq ...
- 推荐系统实践 0x0c FM系列
逻辑回归(LR) 在介绍FM系列之前,我想首先简单介绍一下逻辑回归.通常来说,逻辑回归模型能够综合利用更多的信息,如用户.物品.上下文等多种不同的特征,生成更为全面的结果.另外,逻辑回归将推荐问题看成 ...
- Monkey 部署环境
Monkey的介绍 它是Android系统自带一个命令行工具,可以运行在模拟器里或者真是设备中运行. Monkey是发送伪随机用户事件的工具. Monkey向系统发送伪随机的用户事件流,实现对正在开发 ...
- 效率神器-MouseInc推荐和使用
主要功能 鼠标手势 按住右键滑动即可开始使用. 配置细微,可自由修改手势宽度,颜色,识别灵敏度等. 支持黑名单,支持特定软件自定义手势,支持复合动作. 功能非常强大,比如下面的操作: 选中一个网址,画 ...
- Jmeter(三十二) - 从入门到精通 - Jmeter Http协议录制脚本工具-Badboy5(详解教程)
1.简介 这一篇文章,宏哥主要想讲解一下,录制完脚本不是就完事了,我们有时候还需要断言,看结果是否和我们预期的结果一致.这在测试中都是很重要的.用句老话说:只看结果不看过程. 2.录制脚本 想要断言, ...
- WC.exe(基于Java实现)
一.github地址 https://github.com/Mazin-hub/MyWC.exe.git 二.PSP表格 PSP2.1 Personal Software Process Stage ...
- KVM初体验之virt-manager unable to connect to libvirt的处理办法
解决方法 需要用root身份运行virt-manager
- webstorm实现手机预览页面
效果:在webstorm中开发页面,复制该页面在电脑中的浏览网址,发给手机,在手机上点击链接,可以直接访问本地开发的页面.并且,电脑上修改后保存,手机上刷新即可看到效果. 步骤: 1.webstorm ...
- nginx介绍1
1.1 nginx 是什么? 是一个高性能的web服务器和反向代理服务器 http://www.nginx.cn/ nginx中文手册 1.2 nginx的优点 1 支持高并发:能支持几万并发连接(特 ...
- Python 学习笔记 之 03 - 函数总结
函数总结 最基本的一种代码抽象的方式. 定义函数 使用def语句进行定义, return进行函数返回. 一旦执行导return,函数就执行完毕. 即使函数未指定retur ...