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. 《Linux内核设计与实现》 第一、二章学习笔记

    第一章 Linux内核简介 一.Unix 1.Unix的特点 简洁 绝大部分东西都被当做文件对待.这种抽象使对数据和对设备的操作都是通过一套相同的系统调用借口来进行的:open(),read(),wr ...

  2. Iterative Quantization,ITQ

    Abstract 针对大规模的图像检索问题,论文提出了一个高效的ITQ算法.该算法先将中心化后的数据映射到超立方体的顶点上,再通过优化过程寻找一个旋转矩阵,使得数据点经过旋转后,与超立方体的顶点数据具 ...

  3. OpenFlow PacketOut消息机制

    OpenFlow PacketOut消息机制 前言 由于最近实验的进行,遇到一个比较棘手的问题,就是利用控制器主动发送packet消息的问题,期间遇到一些问题,后来在RYU群中得到群友左木的帮助成功解 ...

  4. PHP 执行命令时sudo权限的配置

    PHP 执行命令时sudo权限的配置 1.先写一个PHP文件 <?php system('whoami'); 先看自己的apache2的用户是谁,下面是笔者的截图,笔者使用apche2的用户是w ...

  5. 我与git“美妙”的一天

    今天是第一天使用git,苦不堪言,感觉服务器和自己都要爆炸了,弄了半天才马马虎虎会了一点,基本流程如下 1.在mukever.online注册用户 2.下载git for windows(一个客户端) ...

  6. 12th final 发布评价 I

    1.  约跑App——nice!:这次使用了摄像进行讲解,相比于上次能够更准确地向大家讲解,整体效果更好了,而且很好地针对同学提出的bug进行修改,能够在并不是很熟悉的领域做到这个程度已经很不容易了, ...

  7. Android 出现 maybe missing INTERNET permission 错误问题解决

    在AndroidManifest.xml中,需要进行如下配置:<manifest> //加入以下许可 <uses-permission android:name="andr ...

  8. C# 多线程初级汇总

    异步委托 创建线程的一种简单方式是定义一个委托,并异步调用它 委托是方法的类型安全的引用 Delegate类还支持异步地调用方法.在后台,Delegate类会创建一个执行任务的线程 投票,并检查委托是 ...

  9. JDK8新特性,方法的引用

    引用方法并运行 在Java中,方法和构造方法都看作是对象的一种,那么你要引用它(不是调用),则可以用::来引用.用来存储这个引用的类型用@FunctionlaInterface注解来标识. 示例: p ...

  10. BZOJ 4173: 数学

    4173: 数学 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 462  Solved: 227[Submit][Status][Discuss] D ...