LeetCode - PlusOne

题意:给一个数按位存放在一个int数组中,要求返回这个数加一后的数组。

懒人解法:

public class Solution {
    public int[] plusOne(int[] digits) {
	    java.math.BigInteger Bigdigits = new java.math.BigInteger(toString(digits));
		String s = Bigdigits.add(new java.math.BigInteger("1")).toString();
		return toArray(s);
	}
	public static String toString(int[] d) {
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<d.length; i++) {
			sb.append(d[i]);
		}
		return sb.toString();
	}
	public static int[] toArray(String s) {
		int[] ans = new int[s.length()];
		for(int i=0; i<s.length(); i++) {
			ans[i] = s.charAt(i) - ‘0‘;
		}
		return ans;
	}
}
时间: 2024-08-28 10:24:47

LeetCode - PlusOne的相关文章

LeetCode: plusOne 题解

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. 题目大意: 给定一个由一组数字构成的数组表示的非负整数, 对这个数进行加一操作,并将结果数组返回. 数组的首元素存储的是该非负整数的最高位. 本题比较简

LeetCode算法练习PlusOne

今天看到酷壳推荐的国外编程LeetCode算法编程网站,上面目前有154道算法题,感觉很有意思,平常工作也比较忙,现在很少有时间来锻炼算法相关的东西,有空的时候静下心来,温习下基础,活跃下自已的思路,也是有必要的.下午先做了个简单的题,后面会陆续补充其它的题目. 1.题目 Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such

Leetcode解题笔记-Plusone

题目内容: 给定一个int数组,代表一个非负数不同的位, 在这个数字中加上1之后将结果以int数组的形式返回. 个人解法1: 1.现将数组转化为int之后加上1之后再变成数组,这样就可能忽略溢出所以无法通过 个人解法2: 这个解法超级麻烦自己也做不下去了,感觉对于数组操作的部分还不是很熟悉: 1.先定义一个ArraList作为结果集(因为它不定长,不用考虑边界问题),之后对digits上面的每一位进行判断,如果加上1等于10的话digits[i]=0 and carry =1 2.将计算后的结果

leetcode问题:PlusOne

给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123. 示例 2: 输入: [4,3,2,1] 输出: [4,3,2,2] 解释: 输入数组表示数字 4321. 示例3:输入:[9]输出:[1,0]解法1:pubiic int[] plusOne(int[] digits){

LeetCode:Plus One - 数字加一

1.题目名称 Plus One(数字加一) 2.题目地址 https://leetcode.com/problems/plus-one 3.题目内容 英文: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

LeetCode Plus One Java版解题报告

https://oj.leetcode.com/problems/plus-one/ 题意:一个整数按位存储于一个int数组中,排列顺序为:最高位在array[0] ,最低位在[n-1],例如:98,存储为:array[0]=9; array[1]=8; 解题思路,从数组的最后一位开始加1,需要考虑进位,如果到[0]位之后仍然有进位存在,需要新开一个长度为(n.length + 1)的数组,拷贝原来的数组. public class Solution { public int[] plusOne

LeetCode: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. // https://oj.leetcode.com/problems/plus-one/ // Author : Chao Zeng // Date

LeetCode算法编程(两题)

今天看到酷壳推荐的国外编程LeetCode算法编程网站,上面目前有154道算法题,感觉很有意思,平常工作也比较忙,现在很少有时间来锻炼算法相关的东西,有空的时候静下心来,温习下基础,活跃下自已的思路,也是有必要的.先做了几道,后面会陆续补充其它的题目. 1.题目-PlusOne Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored s

LeetCode &quot;Plus One Linked List&quot;

I use a stack. Of course you can simply trace nodes with 9s - https://leetcode.com/discuss/111127/iterative-two-pointers-with-dummy-node-java-o-n-time-o-1-space /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;