[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 ...
随机推荐
- TensorFlow安装方法:附带坑解决办法
>>添加Anaconda 仓库的镜像 Anaconda 安装包可以到 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 下载. ...
- Pycharm永久激活方法
1.下载新版破解补丁 链接 https://pan.baidu.com/s/137-afPKYfkXbvroSv1hoYw 提取码: cm43 下载补丁文件jetbrains-agent.jar并将 ...
- 整理一下dedecms的相关知识
dedecms更改数据库连接 文件 data/common.inc.php ------------------------------------------------------------ ...
- 2017 Mid Central Regional F.Orderly Class(大水题)
这两天刷了两道过去的原题,看看思维还是8太行. 这道题问给出两个字符串,要求只翻转一次,问有几种不同的方法使得a串变成b串 我一开始没看到只翻转一次,还以为是个计数 + 字符串dp大难题,心想当年的学 ...
- 第8.27节 Python中__getattribute__与property的fget、@property装饰器getter关系深入解析
一. 引言 在<第7.23节 Python使用property函数定义属性简化属性访问的代码实现>和<第7.26节 Python中的@property装饰器定义属性访问方法gette ...
- 第10.4节 Python模块的弱封装机制
一. 引言 Python模块可以为调用者提供模块内成员的访问和调用,但某些情况下, 因为某些成员可能有特殊访问规则等原因,并不适合将模块内所有成员都提供给调用者访问,此时模块可以类似类的封装机制类似的 ...
- PyQt(Python+Qt)学习随笔:QListView的layoutMode属性和batchSize属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 batchSize属性 该属性是在layoutMode属性设置为Batched时,用于控制每个批量的 ...
- BUUOJ 杂项MISC(1)
爱因斯坦 下载之后解压打开是一张爱因斯坦的图片,看来是图片隐写题 使用binwalk -e misc2.jpg 获得一个有flag.txt的压缩包,但是需要密码才能打开,猜想密码在图片里面,把图片丢进 ...
- Hive基本原理及配置Mysql作为Hive的默认数据库
Hive是什么? Hive是基于Hadoop之上的数据仓库: Hive是一种可以存储.查询.分析存储在hadoop中的大规模数据 Hive定义了简单的类SQL查询语言,成为HQL,它允许熟悉SQL的用 ...
- 转载 HTTP协议
转载自:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, ...