博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode — plus-one
阅读量:7067 次
发布时间:2019-06-28

本文共 1597 字,大约阅读时间需要 5 分钟。

import java.util.ArrayList;import java.util.Arrays;import java.util.List;/** * Source : https://oj.leetcode.com/problems/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. * */public class PlusOne {    /**     * 加法运算,注意进位     *     * @param digit     * @return     */    public Integer[] plusOne (int[] digit) {        int carry = 1;        List
result = new ArrayList
(); for (int i = digit.length - 1; i > -1; i--) { carry += digit[i]; result.add(0, carry % 10); carry = carry / 10; } if (carry > 0) { result.add(0, carry); } return result.toArray(new Integer[result.size()]); } public static void main(String[] args) { PlusOne plusOne = new PlusOne(); int[] arr = new int[]{1,2,3}; int[] arr1 = new int[]{1,9,9}; int[] arr2 = new int[]{9,9,9}; int[] arr3 = new int[]{1}; int[] arr4 = new int[]{9}; int[] arr5 = new int[]{}; System.out.println(Arrays.toString(plusOne.plusOne(arr))); System.out.println(Arrays.toString(plusOne.plusOne(arr1))); System.out.println(Arrays.toString(plusOne.plusOne(arr2))); System.out.println(Arrays.toString(plusOne.plusOne(arr3))); System.out.println(Arrays.toString(plusOne.plusOne(arr4))); System.out.println(Arrays.toString(plusOne.plusOne(arr5))); }}

转载于:https://www.cnblogs.com/sunshine-2015/p/7679446.html

你可能感兴趣的文章
tomcat中同时启动两个项目出现内存不足的错误提示解决办法
查看>>
ssm框架开发过程中遇到的一错误以及解决问题提示
查看>>
树的遍历
查看>>
Akka2使用探索6(Futures)——实现并发和异步
查看>>
【持续更新】jQuery 实用技巧
查看>>
大象也能起舞,Citrix X1计划让你对笔记本电脑say good bye
查看>>
Nginx 之常见报错问题解决
查看>>
linux 防爆破方法
查看>>
2、通过ipmitool工具修改IPMI的WEB密码
查看>>
云盘关闭,教你用蒲公英搭建私有云
查看>>
Spring Cloud 入门教程5、服务容错监控:Hystrix Dashboard
查看>>
很好的学习平台
查看>>
hibernate学习笔记3
查看>>
SQL Server 2005 日常运维检查操作手册
查看>>
利用jquery和jsonp来获取跨站数据,并实现cookie共享
查看>>
我的友情链接
查看>>
写sql语句时将时间格式“20110725”转化为格式2012年07月25日
查看>>
[Hadoop in China 2011] 蒋建平:探秘基于Hadoop的华为共有云
查看>>
heartbeat高可用+lvsDR
查看>>
方丈被害子女有没有权利继承遗产?
查看>>