為了練習尋找實習時會碰到的問題,我用別人推薦的 LeetCode online judge。
希望能直接寫程式,不用事先編譯就能直接AC。
To practice future interview question, I use LeetCode online judge.
I am trying to type code directly on website without compile in other tool.
希望能直接寫程式,不用事先編譯就能直接AC。
To practice future interview question, I use LeetCode online judge.
I am trying to type code directly on website without compile in other tool.
編譯錯誤次數 Compile Error number: 3
嘗試次數 Try number: 8 (AC: 1) (WA:2) (CE:3) (RE: 2)
是否事先在其他工具編譯 if compile first in other tool: No
使用的程式語言 using programming language: C++
以前是否看過 seen this problem before: No
嘗試次數 Try number: 8 (AC: 1) (WA:2) (CE:3) (RE: 2)
是否事先在其他工具編譯 if compile first in other tool: No
使用的程式語言 using programming language: C++
以前是否看過 seen this problem before: No
點這裡看題目 Click here to see this Problem!
1. use new(int val) to create new node and allocate space..
malloc is hard to use..
2. 在做 new的時候,要用nextNode -> next = new ListNode(tmpInt);
要是之前先去next, 就會變成NULL, 就會找不到指標了
ex:
nextNode = nextNode -> next;
nextNode = new ListNode(tmpInt);
If you want to use (copy, paste or quote) my original article,
please contact me through email. (autek.roy@gmail.com)
If there is any mistake or comment, please let me know. :D
如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
1. use new(int val) to create new node and allocate space..
malloc is hard to use..
2. 在做 new的時候,要用nextNode -> next = new ListNode(tmpInt);
要是之前先去next, 就會變成NULL, 就會找不到指標了
ex:
nextNode = nextNode -> next;
nextNode = new ListNode(tmpInt);
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { | |
ListNode *root = NULL; | |
ListNode *nextNode = NULL; | |
int carry = 0, tmpInt, num1, num2; | |
//maybe l1 = NULL and l2 = NULL and carry = 1 | |
while(l1 != NULL || l2 != NULL || carry != 0){ | |
num1 = (l1 == NULL)? 0: l1 -> val; | |
num2 = (l2 == NULL)? 0: l2 -> val; | |
tmpInt = num1 + num2 + carry; | |
carry = tmpInt/10; //(tmpInt >= 10)? 1: 0; | |
tmpInt = tmpInt % 10; | |
l1 = (l1 == NULL)? NULL: l1 -> next; | |
l2 = (l2 == NULL)? NULL: l2 -> next; | |
if(root == NULL){ | |
root = new ListNode(tmpInt); | |
nextNode = root; | |
} | |
else{ | |
//if just use nextNode = new ListNode(tmpInt); | |
//the previous nextNode is NULL, we will lost pointer | |
nextNode -> next = new ListNode(tmpInt); | |
nextNode = nextNode -> next; | |
} | |
} | |
return root; | |
} | |
}; |
如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!