Plus One & Plus One Linked List

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.

 1 public class Solution {
 2     public int[] plusOne(int[] digits) {
 3         if (digits == null || digits.length == 0) return digits;
 4 
 5         int carryover = 1;
 6         int i = digits.length - 1;
 7         int digit = 0;
 8         do {
 9             digit = carryover + digits[i];
10             carryover = digit / 10;
11             digit = digit % 10;
12             digits[i] = digit;
13             i--;
14         } while(carryover == 1 && i >= 0); 
15         // array contains only 9s
16         if (carryover == 1) {
17             int[] newArray = new int[digits.length + 1];
18             newArray[0] = 1;
19             return newArray;
20         }
21         return digits;
22     }
23 }

 

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

分析:
递归做法:
 1 /** 
 2  * Definition for singly-linked list. 
 3  * public class ListNode { 
 4  *     int val; 
 5  *     ListNode next; 
 6  *     ListNode(int x) { val = x; } 
 7  * } 
 8  */  
 9 public class Solution { 
10        public ListNode plusOne(ListNode head) {
11         if (head == null) return null;
13 int carry = helper(head); 14 if (carry == 1) { 15 ListNode newHead = new ListNode(1); 16 newHead.next = head; 17 return newHead; 18 } else { 19 return head; 20 } 21 } 22 23 public int helper(ListNode head) { 24 if (head == null) return 1; 26 27 int carry = helper(head.next); 28 int value = head.val; 29 head.val = (value + carry) % 10; 30 carry = (value + carry) / 10; 31 return carry; 32 } 33 }

方法二:

先reverse, 加一,再reverse.

 1 class ListNode {
 2     int val;
 3     ListNode next;
 4 
 5     ListNode(int x) {
 6         val = x;
 7     }
 8 }
 9 
10 public class Solution {
11 
12     private ListNode reverse(ListNode head) {
13         ListNode prev = null;
14         ListNode current = head;
15         ListNode next = null;
16         while (current != null) {
17             next = current.next;
18             current.next = prev;
19             prev = current;
20             current = next;
21         }
22         return prev;
23     }
24 
25     public ListNode plusOne(ListNode head) {
26         if (head == null) return head;
27         head = reverse(head);
28         ListNode newHead = head;
29         int carry = 1;
30         while (head != null) {
31             int value = head.val;
32             head.val = (value + carry) % 10;
33             carry = (value + carry) / 10;
34             head = head.next;
35         }
36         newHead = reverse(newHead);
37         if (carry == 1) {
38             ListNode h = new ListNode(1);
39             h.next = newHead;
40             return h;
41         }
42         return newHead;
43     }
44 }

 

posted @ 2016-07-10 23:04  北叶青藤  阅读(233)  评论(0编辑  收藏  举报