Given a non-negative number represented as an array of digits, plus N to the number.

The digits are stored such that the most significant digit is at the head of the list.

N is guaranteed to be non-negative.

Solution:

 public class Solution {
public int[] plusN(int[] digits, int n) {
int carry = n;
int index = digits.length-1;
while (carry>0 && index>=0){
int val = digits[index]+carry;
carry = val/10;
val = val%10;
digits[index]=val;
index--;
}
int[] res;
if (index<0 && carry>0){
String cStr = Integer.toString(carry);
res = new int[cStr.length()+digits.length];
for (int i=0;i<cStr.length();i++)
res[i]=cStr.charAt(i)-'0';
for (int i=0;i<digits.length;i++)
res[i+cStr.length()]=digits[i];
} else res = digits;
return res;
}
}

Leetcode Variant-Plus N的更多相关文章

  1. [LeetCode] 324. Wiggle Sort II 摆动排序 II

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  2. 决战Leetcode: easy part(51-96)

    本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...

  3. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  4. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  5. [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 ...

  6. 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 ...

  7. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  8. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  9. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  10. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

随机推荐

  1. 彻底解决_OBJC_CLASS_$_某文件名", referenced from:问题转

    最近在使用静态库时,总是出现这个问题.下面总结一下我得解决方法: 1. .m文件没有导入   在Build Phases里的Compile Sources 中添加报错的文件 2. .framework ...

  2. CentOS 6.4安装lnmp环境

    1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport ...

  3. CentOS 5.x版本升级Mysql

    #-----------------------------CentOS 5.x版本升级Mysql ------------------#! /bin/sh #1.关闭selinuxcp -rp /e ...

  4. Application Designer Security

    This wiki page covers how to manage and restrict Application Designer security through permission li ...

  5. [leetcode]_Path Sum I && II

    都是考查DFS.经典回溯算法,问题在于我对该类型的代码不熟悉,目前以参考别人的代码,然后加上自己的实现为主,通过类似的题目加强理解. 一.给定一棵二叉树,判断是否存在从root到leaf的路径和等于给 ...

  6. [leetcode]_Pascal's Triangle II

    题目:Pascal三角的变形,要求只用O(K)的额外空间. 思路:由于Pascal三角中,tri[n][i] = tri[n - 1][i] + tri[n-1][i-1],(通常情况下) 如果已经获 ...

  7. zabbix2.4 安装配置

    首先从www.zabbix.com下载rpm包: 接下来我要配置一台zabbix server,自己监控自己即使服务端又是客户端,zabbix web gui和zabbix数据库都放在同一台主机上,除 ...

  8. Oracle获取表结构信息:表名、是否视图、字段名、类型、长度、非空、主键

    select a.TABLE_NAME as "TableName", then 'V' else 'U'end as "TableType", a.COLUM ...

  9. sql server 跨库操作

    SELECT *FROM OPENDATASOURCE('SQLOLEDB','Data Source=sql服务器名;User ID=用户名;Password=密码;').PersonDb.dbo. ...

  10. .Net码农学Android---小点整理

    小点整理 虽然两大语言的编程思想相同,语法也相似,但具体到使用时,还是有些别扭,可能还是不太熟悉,现就自己遇到的一些微小问题整理如下: String/Int转换 C#:String--->Int ...