zoukankan      html  css  js  c++  java
  • LeetCode: Add two numbers

    这题没什么难度,直接看代码吧

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     ListNode* dfs(ListNode *l1, ListNode *l2, int carry) {
    12         if (!l1 && !l2) {
    13             if (!carry) return NULL;
    14             else {
    15                 ListNode* tmp = new ListNode(carry);
    16                 return tmp;
    17             }
    18         }
    19         if (!l1) {
    20             int sum = l2->val + carry;
    21             ListNode* tmp = new ListNode(sum%10);
    22             carry = sum/10;
    23             tmp->next = dfs(l1, l2->next, carry);
    24             return tmp;
    25         }
    26         if (!l2) {
    27             int sum = l1->val + carry;
    28             ListNode* tmp = new ListNode(sum%10);
    29             carry = sum/10;
    30             tmp->next = dfs(l1->next, l2, carry);
    31             return tmp;
    32         }
    33         if (l1 && l2) {
    34             int sum = l1->val + l2->val + carry;
    35             ListNode* tmp = new ListNode(sum%10);
    36             carry = sum/10;
    37             tmp->next = dfs(l1->next, l2->next, carry);
    38             return tmp;
    39         }
    40     }
    41     ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
    42         // Start typing your C/C++ solution below
    43         // DO NOT write int main() function
    44         int carry = 0;
    45         ListNode *ret = dfs(l1, l2, carry);
    46         return ret;
    47     }
    48 };

     Java:

     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 addTwoNumbers(ListNode l1, ListNode l2) {
    11         return addTwoNumbersWithCarry(l1, l2, 0);
    12     }
    13     private ListNode addTwoNumbersWithCarry(ListNode l1, ListNode l2, int carry)
    14     {
    15         if (l1 == null && l2 == null) 
    16         {
    17             if (carry > 0)
    18             {
    19                 return new ListNode(carry);
    20             }
    21             else
    22             {
    23                 return null;
    24             }
    25         }
    26         else if (l1 == null)
    27         {
    28             int num = l2.val + carry;
    29             ListNode newNode = new ListNode(num % 10);
    30             newNode.next = addTwoNumbersWithCarry(l1, l2.next, num / 10);
    31             return newNode;
    32         }
    33         else if (l2 == null)
    34         {
    35             int num = l1.val + carry;
    36             ListNode newNode = new ListNode(num % 10);
    37             newNode.next = addTwoNumbersWithCarry(l1.next, l2, num / 10);
    38             return newNode;
    39         }
    40         else
    41         {
    42             int num = l1.val + l2.val + carry;
    43             ListNode newNode = new ListNode(num % 10);
    44             newNode.next = addTwoNumbersWithCarry(l1.next, l2.next, num / 10);
    45             return newNode;
    46         }
    47     }
    48 }
  • 相关阅读:
    appium在Mac上环境搭建
    判断元素的16中方法expected_conditions
    python3条件表达式和字符串
    webdriver的API
    什么是web接口
    python2函数
    python1变量,表达式和语句
    XPath学习
    接口相关概念
    解决jdk1.7,1.8共存问题小思
  • 原文地址:https://www.cnblogs.com/yingzhongwen/p/2961351.html
Copyright © 2011-2022 走看看