zoukankan      html  css  js  c++  java
  • [LeetCode#2]Add Two Numbers

    The  question: 

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

    The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

    You may assume that each input would have exactly one solution.

    Input: numbers={2, 7, 11, 15}, target=9
    Output: index1=1, index2=2

    Analysis:

    This question is 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 be a situation that carry is 1, while all nodes in list 1 and list 2 has already been scanned.

    we need to tackle this situation by adding following lines:

     if (carry == 1) { //take care, there might be a additional carry in the last digit
                newNode = new ListNode(1);
                temp.next = newNode;
                temp = temp.next;
     }

    My solution: 

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode lr = new ListNode(0);
            ListNode temp = lr; //used to remember the current scan position, note: this is dummpy head for keeping loop invariant!
            ListNode l1_current = l1; //not dumy head
            ListNode l2_current = l2; 
            ListNode newNode = null; 
            int carry = 0; //set the initial carry into zero
    
            while (l1_current != null || l2_current != null) {
                
               if (l1_current == null && l2_current != null) {
                   newNode = new ListNode((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 = new ListNode((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 = new ListNode((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 lr 
              temp = temp.next; //move the scanner to the next position 
            }
            
            if (carry == 1) { //take care, there might be a additional carry in the last digit
                newNode = new ListNode(1);
                temp.next = newNode;
                temp = temp.next;
            }
            
            return lr.next;
        }
    }
  • 相关阅读:
    jenkins免密添加SSH Servers
    Workman启动失败的解决方法 stream_socket_server() has been disabled for security reasons
    jenkins主从从服务器发布脚本执行成功但总提示失败 FATAL: Remote call on XXXX failed
    mac OS配置用户全局环境变量(设置字符集为UTF8)
    使用 Application Loader提交IPA文件到苹果市场
    IOS使用批处理打包
    Java进阶知识24 Spring对JDBC的支持
    Java进阶知识23 Spring execution 切入点表达式
    Java进阶知识22 Spring的AOP编程
    Java进阶知识21 Spring的代理模式
  • 原文地址:https://www.cnblogs.com/airwindow/p/4192762.html
Copyright © 2011-2022 走看看