亚洲精品久久久久久久久久久,亚洲国产精品一区二区制服,亚洲精品午夜精品,国产成人精品综合在线观看,最近2019中文字幕一页二页

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

Add Two Numbers

汽車電子技術 ? 來源:神經網絡與強化學習 ? 作者:Jemma Liu ? 2023-03-01 13:38 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

今天的題目是兩數相加。

2.Add Two Numbers

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.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

Explanation: 342 + 465 = 807.

  1. 兩數相加

給出兩個 非空 的鏈表用來表示兩個非負的整數。其中,它們各自的位數是按照 逆序 的方式存儲的,并且它們的每個節(jié)點只能存儲 一位 數字。

如果,我們將這兩個數相加起來,則會返回一個新的鏈表來表示它們的和。

您可以假設除了數字 0 之外,這兩個數都不會以 0 開頭。

示例:

輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4)

輸出:7 -> 0 -> 8

原因:342 + 465 = 807

My answer:

首先創(chuàng)建兩個指針指向結果鏈表的頭節(jié)點,一個指針dummy始終指在頭節(jié)點,一個指針now用來指向尾結點(新值插入的位置)。然后設置一個進位標志carry初始化為0。x來代表l1的數值,y代表l2數值,任意一個鏈表的結束時其對應數值設為0,直到兩個鏈表均結束循環(huán)停止。然后在循環(huán)內,獲得當前位的值sum = x+y+carry和進位carry = sum//10,并將新值sum%10接在now指針后面。最后循環(huán)結束時,判斷是否依然有進位,如果有進位則在結果鏈表后新增值為1的結點即可。最后返回dummy.next(注意返回時略過頭節(jié)點)鏈表。

Runtime: 40 ms, faster than 99.89% of Python online submissions for Add Two Numbers.

     Memory Usage: 11.9 MB, less than 31.51% of Python online submissions for Add Two Numbers.
# 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
        """
        now = dummy = ListNode(0) #new node 
        carry = 0
        while(l1 or l2):
            x = l1.val if l1 is not None else 0
            y = l2.val if l2 is not None else 0
            sum = x+y+carry
            carry = sum//10
            now.next = ListNode(sum%10)
            now = now.next
            if(l1): l1 = l1.next
            if(l2): l2 = l2.next
        if(carry):
            now.next = ListNode(1)
        return dummy.next
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 節(jié)點
    +關注

    關注

    0

    文章

    226

    瀏覽量

    25357
  • now
    now
    +關注

    關注

    0

    文章

    2

    瀏覽量

    6755
  • dummy
    +關注

    關注

    0

    文章

    6

    瀏覽量

    5859
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    【RA4L1-SENSOR】4、移植FreeRTOS-CLI

    (pcParameter2, NULL, 10); // add the two numbers int32_t xResultValue = xValue1 + xValue2; // convert
    發(fā)表于 06-26 22:30

    【RA-Eco-RA6M4開發(fā)板評測】2、移植FreeRTOS_CLI和添加自定義指令

    ); int32_t xValue2 = strtol(pcParameter2, NULL, 10); // add the two numbers int32_t xResultValue
    發(fā)表于 07-23 11:34

    誰有extract numbers 子vi

    誰有extract numbers 子vi
    發(fā)表于 10-23 11:15

    VRF如何添加十六進制數據?

    with the IO/"To String" object with transaction Write/Text/HexFormat.If you want to add two numbers
    發(fā)表于 09-25 15:46

    ADD8504/ADD8505/ADD8506,pdf da

    The ADD8504, ADD8505, and ADD8506 are 4-, 5-, and 6-channel LCD gamma reference buffers designed
    發(fā)表于 09-02 16:16 ?15次下載

    CD54AC283,CD74AC283,CD54ACT283

    technology. These devices add two 4-bit binary numbers and generate a carry-out bit if the sum exceeds 15. Becau
    發(fā)表于 08-09 18:14 ?10次下載

    CD54HC283,CD74HC283,CD54HCT283

    The ’HC283 and ’HCT283 binary full adders add two 4-bit binary numbers and generate a carry-out bit
    發(fā)表于 08-09 18:20 ?26次下載

    Add-On Circuit Preconditions B

    Add-On Cir
    發(fā)表于 04-30 10:55 ?1652次閱讀
    <b class='flag-5'>Add</b>-On Circuit Preconditions B

    Add Thermal Monitoring to Redu

    Add Thermal Monitoring to Reduce Data Center Energy Consumption Abstract: Precise and adaptable
    發(fā)表于 05-29 11:01 ?832次閱讀
    <b class='flag-5'>Add</b> Thermal Monitoring to Redu

    Add a Discrete Jack-Sensing Ci

    Add a Discrete Jack-Sensing Circuit to the MAX13330/MAX13331 Automotive Headphone Amplifier
    發(fā)表于 10-03 08:46 ?1915次閱讀
    <b class='flag-5'>Add</b> a Discrete Jack-Sensing Ci

    什么是ADM (Add/Drop Multiplexer)

    什么是ADM (Add/Drop Multiplexer)  英文縮寫: ADM (Add/Drop Multiplexer) 中文譯名: 分插復用器 分  類: 網絡與交換
    發(fā)表于 02-22 10:10 ?3236次閱讀

    Ethernet_NanoBoard_Add-On

    Ethernet NanoBoard Add-On,好東西,喜歡的朋友可以下載來學習。
    發(fā)表于 02-22 14:33 ?0次下載

    Camera_add_shuttersound_setting_

    Camera_add_shuttersound_setting_item
    發(fā)表于 05-16 11:05 ?10次下載

    3.1.2 c_add文件匯總

    3.1.2 c_add文件匯總
    發(fā)表于 04-10 10:00 ?6次下載

    numbers.vim顯示文本行數的Vim插件

    ./oschina_soft/numbers.vim.zip
    發(fā)表于 05-25 10:03 ?0次下載
    <b class='flag-5'>numbers</b>.vim顯示文本行數的Vim插件