LeetCode !! 69 plusOne
参考资料:左程云算法课
66. Plus One
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.Increment the large integer by one and return the resulting array of digits.Example 1:Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].
思路:
倒序遍历,遇到第一个不为9的位加一并结束返回数组,否则(也就是当前位是9)直接化为0.
如果遍历结束,还没有结束返回值,那么说明原数组中都是9,于是 创建新数组,手动加一。
public static int[] plusOne(int[] digits){// 9,9,9int n = digits.length;// i=n-1 for(int i=n-1;i>=0;i++){if(digits[i]<9){digits[i]+=1;return digits;}digits[0]=0;}int[] ans = new int[n+1];ans[0]=1;return ans;}
LeetCode !! 69 plusOne相关推荐
- 【分治】LeetCode 69. Sqrt(x)
LeetCode 69. Sqrt(x) 参考网址:http://www.cnblogs.com/grandyang/p/4346413.html Solution1: class Solution ...
- LeetCode #69 x的平方根 二分查找
LeetCode #69 x的平方根 题目描述 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍 ...
- LeetCode 69. x 的平方根:二分查找法实现自定义的函数:x 的平方根
LeetCode 69. x 的平方根:二分查找法实现自定义的函数:x 的平方根 题目描述 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型 ...
- LeetCode - 69. x 的平方根
69. x 的平方根 class Solution {private static final Integer MAX_POW = 46340;/*** 牛顿迭代* f(x) = x^2 - n* 切 ...
- LeetCode 69. x 的平方根(二分查找)
文章目录 1. 题目 2.解题 2.1 二分查找 2.2 牛顿迭代 1. 题目 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果 ...
- 1.1.3 数组——x的平方(Leetcode 69)
x的平方 1. 题目 2. 思路 3. 代码实现 4. 总结 1. 题目 leetcode链接 给你一个非负整数 x ,计算并返回 x 的 算术平方根 . 由于返回类型是整数,结果只保留 整数部分 , ...
- 【手绘漫画】图解LeetCode之x 的平方根(LeetCode 69题)
文章目录 图解LeetCode刷题计划 1.写在前面 2.题目 3.正文 4.代码 图解LeetCode刷题计划 1.写在前面 手绘漫画系列正式上线!!!"图解LeetCode刷题计划&qu ...
- LeetCode——69 x的平方根
问题描述: 实现 int sqrt(int x) 函数.计算并返回 x 的平方根,其中 x 是非负整数.由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 ...
- LeetCode:69. Sqrt(x)
题目链接: 69. Sqrt(x) 题目描述: Implement int sqrt(int x) Compute and return the square root of x. 题目解释: 提干非 ...
最新文章
- MySQL的常见存储引擎介绍与参数设置调优
- JavaScript学习笔记——BOM_window对象
- ODataModel.submitBatch by default Asynchronous
- Redis非阻塞I/O多路复用机制
- python 生成器_提高你的Python: 解释‘yield’和‘Generators(生成器)’
- 设置html按钮点击事件无效果,css怎么设置按钮不能点击?
- python开发蠕虫病毒_清除服务器蠕虫病毒
- git(5)---Git 与Repo 管理Android代码
- 【C++】C++中substr的用法
- 快递100 快递公司编码-标准国际
- Oracle 11g SQL基础入门系列培训-小布作品
- MATLAB 常用函数学习笔记
- 成功三大定律 重在厚积薄发!
- Unity实现自适应屏幕大小——九宫格
- 【一点唐城】15个未来高科技产品会让你无法想象、这些开脑洞的设计太牛了
- 数据结构C语言 胡学钢 PDF,数据结构(C语言版) 胡学钢.ppt
- ENSP 单臂路由的配置(带配置命令)
- 人工智能最受欢迎的10大TED演讲
- feasycom蓝牙对接Android,2.4G低功耗蓝牙解决方案
- 菜鸟级MVC登录注册实现