美文网首页
两个数组相加

两个数组相加

作者: 水瓶鱼 | 来源:发表于2017-03-14 22:20 被阅读45次

题目(leetcode)

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

python

  # Definition for singly-linked list.
  # class ListNode(object):
  #     def __init__(self, x):
  #         self.val = x
  #         self.next = None
  class Solution(object):
      def addTwoNumbers(self, l1, l2):
          """
          : type l1: ListNode
          :type l2: ListNode
          :rtype: ListNode
          """
          node1 = l1.next
          node2 = l2.next
          increase = l1.val + l2.val
          result = ListNode(increase % 10)
          increase //= 10
          tem = result;
          while node1 is not None or node2 is not None:
              add=0
              if node1 is not None and node2 is not None:
                  add = node1.val + node2.val + increase
              elif node1 is not None:
                  add = node1.val + increase
              else:
                  add = node2.val + increase
              result.next = ListNode(add % 10)
              increase = add // 10
              if node1:
                  node1 = node1.next
              if node2:
                  node2 = node2.next
              result = result.next
          if increase>0:
              result.next=ListNode(increase)
    
          return tem

c++

  class Solution {
  public:
      ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
          int add=l1->val+l2->val;
          ListNode *head=new ListNode(add%10);
          ListNode *tem=head;
          l1=l1->next;
          l2=l2->next;
          add=add/10;
          while(l1!=NULL&&l2!=NULL){
              add=l1->val+l2->val+add;
              ListNode *tem1=new ListNode(add%10);
              add=add/10;
              tem->next=tem1;
              tem=tem1;
              l1=l1->next;
              l2=l2->next;
          }
          if(l2!=NULL){
              l1=l2;
          }
          while(l1!=NULL){
              int n=l1->val+add;
              add=n/10;
              ListNode * tem1=new ListNode(n%10);
              tem->next=tem1;
              tem=tem1;
              l1=l1->next;
          }
          if(add!=0){
              tem->next=new ListNode(add);
          }
          return head;
      }
  };

相关文章

  • 两个数组相加

    题目(leetcode) You are given two non-empty linked lists rep...

  • Swift-数组“运算”汇总

    两个数组“相加”——合并数组 通过运算符+,将两个数组合并到新数组。 追加元素 通过运算符+=,将右边数组追加到左...

  • 大数加法(A + B Problem Plus)问题

    1.解题思路 两个⼤数可以⽤数组来逐位保存,然后在数组中逐位进⾏相加,再判断该位相加后是否需要进位。为了⽅便计算,...

  • 前端数组算法操作(部分更新中)

    合并两个有序数组: 在数组中找到两个值相加,与目标值相同,返回这两个值的索引,且索引一少于索引二: 数组反转: 数...

  • 数组相加

    var array=[1,2,3,4,5,15];console.log(array.reduce((count,...

  • 1.two sum

    题目大意:给出一个int数组,和一个目标数字。求出该数组中哪两个的值相加等于目标数字,返回两个值得数组下标。

  • 08-Swift数组

    1.创建空数组 2.创建带有默认值的数组 3. 通过两个数组相加创建一个数组 4.用数组字面量构造数组 5.数组遍...

  • 167. Two Sum II - Input array is

    给定一个已按照升序排列的整数数组 nums,请你从数组中找出两个数满足相加之和等于目标数组 target。假设每个...

  • leetcode two-sum

    题目 给一个数组和一个和,查找出数组中两个数相加为给定的和 Given an array of integers,...

  • 02.11 数组

    数组 增删改查 加法运算: 两个数组相加实质是转换乘字符串然后拼接console.log([12,3,'abc']...

网友评论

      本文标题:两个数组相加

      本文链接:https://www.haomeiwen.com/subject/bsrknttx.html