[Leetcode#2] ADD Numbers

Source: Internet
Author: User
Tags add numbers

The question:

Given an array of integers, find the numbers such that they add up to a specific target number.

The function twosum should return indices of the numbers such that they add up to the target, where index1 must is Les S than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.

You may assume this each input would has exactly one solution.

Input:numbers={2, 7, one, A, target=9
Output:index1=1, index2=2

Analysis:

This question was simple,

Digit = (L1_current.value + l2_current.value + carry)% 10;

Carry = (L1_current.value + l2_current.value + carry)% 10;

Corner Case:

Apparently, we would do the above computation in the while loop:while (l1_current! = NULL | | l2_current).

When we exit from the loop, there could are a situation that carry are 1, while all nodes in List 1 and List 2 have already b Een scanned.

We need to tackle this situation by adding following lines:

if // Take care , there might is a additional carry in the last digit            New ListNode (1);             = NewNode;             = Temp.next;}

My Solution:

/*** Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int X) {* val = x; * next = NULL; *} *}*/ Public classSolution { PublicListNode addtwonumbers (listnode L1, ListNode L2) {listnode LR=NewListNode (0); ListNode Temp= LR;//used to remember the current scan position, Note:this are dummpy head for keeping loop invariant!ListNode l1_current = L1;//Not dumy headListNode l2_current =L2; ListNode NewNode=NULL; intcarry = 0;//set the initial carry into zero         while(L1_current! =NULL|| L2_current! =NULL) {                       if(L1_current = =NULL&& l2_current! =NULL) {NewNode=NewListNode ((l2_current.val + carry)% 10); Carry= (L2_current.val + carry)/10;//there is no carry for next digit (next upper digit)L2_current =L2_current.next; //continue;           }                      if(L2_current = =NULL&& l1_current! =NULL) {NewNode=NewListNode ((l1_current.val + carry)% 10); Carry= (L1_current.val + carry)/10; L1_current=L1_current.next; //continue;            }                      if(L1_current! =NULL&& l2_current! =NULL) {NewNode=NewListNode ((L1_current.val + l2_current.val + carry)% 10); Carry= (L1_current.val + l2_current.val + carry)/10; L1_current=L1_current.next; L2_current=L2_current.next; //continue the continue would jump over the temp~~~~ careful!} temp.next= NewNode;//The head of the result list is recored in LRtemp = Temp.next;//move the scanner to the next position        }                if(Carry = = 1) {//Take care , there might is a additional carry in the last digitNewNode =NewListNode (1); Temp.next=NewNode; Temp=Temp.next; }                returnLr.next; }}

[Leetcode#2] ADD Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.