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.

Example

Given [1,2,3] which represents 123, return [1,2,4].

Given [9,9,9] which represents 999, return [1,0,0,0].

分析:

这题分两种情况,一种情况是+1后array size 不变,另一种情况是array size 变大了。怎么知道array size会变大呢,就是当我们已经从尾部来到头部,但是carryover却还是1.

 public class Solution {
public int[] plusOne(int[] digits) {
if (digits == null || digits.length == ) return digits; int carryover = ;
int i = digits.length - ;
int digit = ;
do {
digit = carryover + digits[i];
carryover = digit / ;
digit = digit % ;
digits[i] = digit;
i--;
} while(carryover == && i >= );
// array contains only 9s
if (carryover == ) {
int[] newArray = new int[digits.length + ];
newArray[] = ;
return newArray;
}
return digits;
}
}

Plus One Linked List

Given a non-negative number represented as a singly linked list of digits, plus one to the number.

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

Example:

Input:
1->2->3 Output:
1->2->4 分析:
递归做法:
 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode plusOne(ListNode head) {
if (head == null) return null;
int carry = helper(head);
if (carry == ) {
ListNode newHead = new ListNode();
newHead.next = head;
return newHead;
} else {
return head;
}
} public int helper(ListNode head) {
if (head == null) return ; int carry = helper(head.next);
int value = head.val;
head.val = (value + carry) % ;
carry = (value + carry) / ;
return carry;
}
}

方法二:

先reverse, 加一,再reverse.

 class ListNode {
int val;
ListNode next; ListNode(int x) {
val = x;
}
} public class Solution { private ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode current = head;
ListNode next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
} public ListNode plusOne(ListNode head) {
if (head == null) return head;
head = reverse(head);
ListNode newHead = head;
int carry = ;
while (head != null) {
int value = head.val;
head.val = (value + carry) % ;
carry = (value + carry) / ;
head = head.next;
}
newHead = reverse(newHead);
if (carry == ) {
ListNode h = new ListNode();
h.next = newHead;
return h;
}
return newHead;
}
}

Plus One & Plus One Linked List的更多相关文章

  1. [LeetCode] Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  2. [LeetCode] Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  3. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  4. [LeetCode] Delete Node in a Linked List 删除链表的节点

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  5. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

  6. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  7. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  8. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  10. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

随机推荐

  1. javascript 数组对象及其方法

    数组声明:通过let arr = new Array(); 或者 let arr = []; 数组对象可调用的方法: 1)find方法,使用情况是对数组进行筛选遍历,find方法要求某个函数(A)作为 ...

  2. 第二次spring冲刺第1天

    今天,我们开会讨论了,觉得现阶段的四则运算还不够完善,功能也过于简单,没有太多能吸引人的地方,因次提出以下几点作为后续的修改: 1.计时器,如果具有计时功能,那么就可以增加趣味性. 2.页面跳转,现阶 ...

  3. js浮点数保留一位小数

    function changeTwoDecimal_f(x) { var f_x = parseFloat(x); if (isNaN(f_x)) { alert('function:changeTw ...

  4. everything 提供http和ftp的功能

    1. 早上起床看知乎,发现everything 有http和ftp的功能, 简单看了一下的确很强大.. 就是有点危险.. 功能位置. 2. 最下面有FTP和HTTP 可以进行启用 这是http的 建议 ...

  5. [转帖].NET Framework各版本操作系统支持

    .NET Framework .NET版本 1.0 1.1 2.0 3.0 3.5 4.0 4.5 完整版本 1.0.3705.0 1.1.4322.573 2.0.50727.42 3.0.4506 ...

  6. tensorflow的一些基础用法

    TensorFlow是一个采用数据流图,用于数值计算的开源软件库.自己接触tensorflow比较的早,可是并没有系统深入的学习过,现在TF在深度学习已经成了"标配",所以打算系统 ...

  7. Java之JDBC操作

    下载jar包: mysql-connector-java-5.1.44.jar 导入包: import java.sql.*; 源码如下: /** * 使用JDBC底层实现查询 */ public s ...

  8. 使用AutoMapper实现Dto和Model的自由转换(下)

    书接上文.在上一篇文章中我们讨论了使用AutoMapper实现类型间1-1映射的两种方式——Convention和Configuration,知道了如何进行简单的OO Mapping.在这个系列的最后 ...

  9. 使用VBA批量转换Excel格式,由.xls转换成.xlsx

    问题分析: Excel2007以前的格式是.xls,之后的格式是.xlsx.打开单独的一个Excel文档,使用“另存为”功能,可以很轻松的转换格式.但是面对几百个Excel表这样就太累了,搜索很久,也 ...

  10. 【刷题】BZOJ 2287 【POJ Challenge】消失之物

    Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. "要使用剩下的 N - 1 物品装满容积为 x ...