leetcode-2 100%submissions
第二題:(網上借鑑)
1.邊界情況:
兩個連結串列都為空 或其中一個為空
2.思路:
一般這種涉及兩個連結串列的題目都會建立一個輔助用的頭節點(即自己建立一個結點,這裡我稱為pre)
從個位開始把兩個連結串列相加的結果儲存到pre中,(具體實現就是一位一位向前加,最後加完了在遍歷一遍pre連結串列解決進位)
這裡有個增加這個submissions百分比的小技巧(即加入以下程式碼)
static const auto _____ = []() { ios::sync_with_stdio(false); cin.tie(nullptr); return nullptr; }();
演算法程式碼
class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { if(!l1 || (l1 && !l1->next && l1->val == 0)) return l2; if(!l2 || (l2 && !l2->next && l2->val == 0)) return l1; ListNode *res = new ListNode(-1); res->next = l1; ListNode *it = NULL; int carry = 0; while(l1 && l2) { int val = carry + l1->val + l2->val; // carry = 0; if(val <=9) { l1->val = val; carry = 0; } else { carry = val/10; val = val%10; l1->val = val; } if(!l1->next && l2->next) it = l1; l1 = l1->next; l2 = l2->next; } if(l2) { it->next = l2; it = it->next; } if(l1) { it = l1; } ListNode *tail = NULL; while(it) { int val = it->val + carry; if(val <= 9) { it->val = val; carry = 0; } else { carry = val/10; val = val%10; it->val = val; } it = it->next; } if(carry) { tail = res; while(tail->next) { tail = tail->next; } tail->next = new ListNode(carry); } return res->next; } }; static const auto _____ = []() { ios::sync_with_stdio(false); cin.tie(nullptr); return nullptr; }();
-------wang
相關推薦
leetcode-2 100%submissions
第二題:(網上借鑑) 1.邊界情況: 兩個連結串列都為空 或其中一個為空 2.思路: 一般這種涉及兩個連結串列的題目都會建立一個輔助用的頭節點(即自己建立一個結點,這裡我稱為pre) 從個位開始把兩個連結串列相加的結果儲存到pre中,(具體實現就是一位一位向前加,
leetcode-103 100%submissions
第一百零三題:(層次遍歷二叉樹問題)自己寫的 1.邊界情況: 根結點為空。 2.思路: 就用一個雙端佇列(可以從頭尾進出),利用變數儲存每層的結點個數,每層交替進隊和出隊規則。 演算法程式碼 class Solution { public: vector<
leetcode-107 100%submissions
第一百零七題:(二叉樹層次遍歷問題)網上借鑑 1.邊界情況: 根結點為空。 2.思路: 前面都是老方法用佇列遍歷然後存在二維陣列中,然後用reverse函式反轉陣列即可。 演算法程式碼 class Solution { public: vector<
leetcode-1 100%submissions
-----以後每道題都一樣不放題目 --前言: 1.每一題都是為了達到最優時間,我的表達能力不是很好,但我會把思路盡我所能表達出來。 2.我覺得一道題最重要的是思路其次是程式碼。 第一題: 1.此題無邊界情況 2.建立一個map<int,int> th
leetcode-3 100%submissions
第二題:(網上借鑑)滑動視窗問題 (最快的是8ms,但是我提交一模一樣的程式碼也只有12ms) 1.邊界情況: 無 2.思路: 其中有個小規律,例如字串 pwwkewasd 在字串不斷向後遍歷的過程中如果遇到已經遍歷的字元,那麼這個字串從開始遍歷到重複字元所組成的
LeetCode - 2 - Add Two Numbers
dtw ble bind ber plus binding bsp pan number 題目 URL:https://leetcode.com/problems/add-two-numbers/ 解法 沒什麽特殊的解法,按位相加即可。 註意點: 1、l1 和 l
LeetCode 2: Add Two Numbers
ret change add cal lis || div span style Tips: Tried to use a private method to simplize the code. But found that object is passed by ref
[LeetCode] 2 Keys Keyboard 兩鍵的鍵盤
post 聯系 not 註意 quest ted 不能 vector ... Initially on a notepad only one character ‘A‘ is present. You can perform two operations on t
LeetCode - 2. Add Two Numbers
ont and contain nod emp list sts init pre You are given two non-empty linked lists representing two non-negative integers. The digits are
shell編程基礎一(多種方法求值1+2+..+100)
循環#SHELL編程基礎一(多種方法求值1+2+..+100)##為什麽要學好shell shell腳本語言是實現linux系統管理及自動化運維所必備的重要工具,linux系統的底層及基礎應用軟件的核心大都涉及shell腳本的內容。 每一個合格的linux系統管理員或運維工程師,都需要能夠熟練地編寫shell
[leetcode] 2. Add Two Numbers
code eve ini emp any n-n self 申請 tno You are given two non-empty linked lists representing two non-negative integers. The digits are stor
Leetcode #2 Add two numbers
理解 contains linked cti tin arr != 情況 目的 ## 題目 >Add Two NumbersYou are given two linked lists representing two non-negative numbers. Th
leetcode:2-Add Two Numbers
另一個 plan example num 提前 除了 += 指向 pre You are given two non-empty linked lists representing two non-negative integers. The digits are stor
[LeetCode]2. Add Two Numbers鏈表相加
pre int 處理 span node new spa ont lag 註意進位的處理和節點為null的處理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0;
LeetCode 2.Add Two Numbers
elf output git any == != ret ber spa You are given two non-empty linked lists representing two non-negative integers. The digits are stor
Leetcode 2. Add Two Numbers (java)
style 相加進位 ret java numbers null bool ext leet 解法: class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
leetcode[2-]
desc 解題思路 des leet esc ber nbsp car 比較 2. Add Two Numbers 解題思路還是比較簡單的,只是要註意向高位進位。 比較優的解法借鑒思路是將所有可能的情況放入while中,用或運算進行判斷,進而少掉了很多冗余的寫法。wh
用匯編語言實現從1加到100(1+2+...+100)
AS tac class 字符 執行 sum 進制 循環 AD 用匯編語言實現1+2+...+100 ;課堂作業 ;計算1+2+...+100 DATA SEGMENT COUNT DW 0 ;計數 DATA ENDS STACK SEGMENT PAR
[leetcode][2] Add Two Numbers
plan leet pub repr put any 原來 integer exce 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative inte
leetcode-2 Add Two Numbers
single round output num spa tor repr tro 情況 You are given two non-empty linked lists representing two non-negative integers. The digits a