Plus One
来源:互联网 发布:小美工作室淘宝真假 编辑:程序博客网 时间:2024/06/14 21:57
题意:一个整数,例如998,百位、十位、个位上的9、9、8分别存储在数组的2、1、0位;对该数加一,求加一后的整数大小。
思路:从个位数字开始,设置一个temp值监控,当不需要进位时,直接在个位加一;当需要进位时,就进行加一操作, 返回该整数。
代码:package PlusOne;
public class PlusOne {
public int[] PlusOne(int []digits){
int temp = 1;
for(int i = digits.length - 1 ; i >=0 ; i–){
if(temp > 0){
int sum = digits[i] + temp;
digits[i] = sum%10;
temp = sum / 10;
}
}
if(temp == 0) return digits;
int []rst = new int[digits.length + 1]; rst[0] = 1; for(int i = 1 ; i < digits.length - 1 ; i++){ rst[i] = digits[i-1]; } return rst;}public static void main(String[] args) { PlusOne po = new PlusOne(); int []digits = {9,7,9}; int []temp = po.PlusOne(digits); for (int i : temp) { System.out.print(i+" "); }}
}
0 0
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- java--servlet相关类(1)
- 多进程和多线程之间的通信方式及通信实现步骤小结
- selinux 基础知识
- [LeetCode-389]Find the Difference
- React(7.2)--React ES6 处理 mixin
- Plus One
- android EventBus
- 网络编程中客户端和服务端通信过程、原理及代码示例
- 图像处理之积分图应用一(半径无关的快速模糊算法)
- RxJava详细解析
- Python 学习第三天
- JAVA基础再回首(二十七)——JAVA中的图形化界面、GUI概述、简单的窗体创建、事件监听机制、动作事件、GUI布局、鼠标事件
- Glide详细解析
- Java进阶之hibernate中cascade与inverse