2020年3月26日 星期四
2020年3月21日 星期六
2020年3月19日 星期四
LeetCode #2 Add Two Numbers [ C language ] [ Medium ]
今天這題蠻有趣的,想練習 Link List 的朋友可以試試看,終於知道為甚麼資料結構一直都學不好了,因為在學生成樹之前,基本觀念都還不清楚啊,用筆畫那些樹和用程式語言去實現差別實在是太大了,所幸藉由這題把以往的記憶都找回來了,繼續加油。
This is a basic problem using link list, if you don't know the concept of pointer and function, try to solve it on LeetCode problem #2.
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode *newNode(int data) { struct ListNode *NewNode = (struct ListNode *) malloc(sizeof(struct ListNode)); NewNode->val = data; NewNode->next = NULL; return NewNode; } struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){ struct ListNode *answer = NULL; int sum=0,tmp; int carry=0; answer=newNode(0); struct ListNode *end =answer; while(l1!=NULL ||l2!=NULL || carry>0){ tmp=(l1?l1->val:0)+(l2?l2->val:0); if(carry==1) tmp=tmp+1; carry=(tmp>=10)?1:0; tmp=(tmp>=10)?(tmp-10):tmp; answer->next= newNode(tmp); if(l1) l1=l1->next; if(l2) l2=l2->next; answer=answer->next; } return end->next; }
This is a basic problem using link list, if you don't know the concept of pointer and function, try to solve it on LeetCode problem #2.
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode *newNode(int data) { struct ListNode *NewNode = (struct ListNode *) malloc(sizeof(struct ListNode)); NewNode->val = data; NewNode->next = NULL; return NewNode; } struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){ struct ListNode *answer = NULL; int sum=0,tmp; int carry=0; answer=newNode(0); struct ListNode *end =answer; while(l1!=NULL ||l2!=NULL || carry>0){ tmp=(l1?l1->val:0)+(l2?l2->val:0); if(carry==1) tmp=tmp+1; carry=(tmp>=10)?1:0; tmp=(tmp>=10)?(tmp-10):tmp; answer->next= newNode(tmp); if(l1) l1=l1->next; if(l2) l2=l2->next; answer=answer->next; } return end->next; }
2020年3月18日 星期三
2020年3月17日 星期二
2020年3月15日 星期日
2020年3月14日 星期六
2020年3月12日 星期四
leetcode #1 Two Sum #7 Reverse Integer
剛開始接觸leetcode,聽說Google Interview有些題目是從這裡來的,不過想也知道難度是Hard等級的,我寫第一題的時候是用C,但是一直是wrong answer,google別人的code拿來用也沒辦法,最後只能參考別人的C++版本,幸好第7題Submit就沒什麼問題,可能I/O相對單純吧,畢竟第1題的Two Sum有用到指標,而且我不知道是要回傳什麼,是傳陣列嗎,還是傳兩個整數,如果有人有Two Sum的C語言版本,希望能留言一下,網路找的我都試不過。
之後會嘗試Normal的題型,Easy題目適合給剛接觸程式語言的人,訓練一下程式的邏輯思維,比如#7 Reverse Integer就需要考慮很多東西,你們一定可以寫得更精簡的,Leetcode Easy題目和Uva一顆星都可以練練,。
說到邏輯,科技公司的面試都會考比如兩個雞蛋,或是紅球白球的問題,這就不知道怎麼訓練了,而且我很容易面試一緊張甚麼都答不出來,悽悽慘慘戚戚。 ----《聲聲慢》作者,李清照
------------------------------------------------------------------------------------------------------------
I am the beginner in LeetCode, I heard that Google Interview problems are coming from this. Hope to one day I can solve rank hard question, by the way, rank easy problems are pretty suitable for programming rookie.
#1 Two Sum [Easy]
C++
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int *a = (int*)malloc(2*sizeof(int));
for(int i = 0;i<numsSize;i++)
{
for(int j = i+1;(j<numsSize && j != i);j++)
{
if(nums[i] + nums[j] == target)
{
a[0] = i;
a[1] = j;
}
}
}
a[0]=5;
return 1;
}
#7 Reverse Integer [Easy]
int reverse(int x){
int max = 2147483648-1;
int min = -2147483648 ;
int output=0;
int tmp,flag;
if(x>=min && x<=max){
if(x>0){
while(x!=0){
tmp=x%10;
if(output>=214748364){
if(output==214748364){
if(tmp<=7){
output=output*10;
output=output+tmp;
x=x/10;
}else{
output=0;
x=0;
}
}else{
output=0;
x=0;
}
}else{
output=output*10;
output=output+tmp;
x=x/10;
}
}
return output;
}else if(x<0){
if(x==-2147483648){
x=x+1;
flag=1;
}else{
flag=0;
}
x=x*(-1);
while(x!=0){
tmp=x%10;
if(output>=214748364){
if(output==214748364){
if(tmp<=7){
output=output*-1;
output=output*10;
output=output-tmp;
x=x/10;
}else{
output=0;
x=0;
}
}else{
output=0;
x=0;
}
}else{
output=output*10;
output=output+tmp;
x=x/10;
}
}
if(output>0) output=output*-1;
if(flag==1 && output!=0) output=output-1;
return output;
}else{
return 0;
}
}else{
return 0;
}
}
之後會嘗試Normal的題型,Easy題目適合給剛接觸程式語言的人,訓練一下程式的邏輯思維,比如#7 Reverse Integer就需要考慮很多東西,你們一定可以寫得更精簡的,Leetcode Easy題目和Uva一顆星都可以練練,。
說到邏輯,科技公司的面試都會考比如兩個雞蛋,或是紅球白球的問題,這就不知道怎麼訓練了,而且我很容易面試一緊張甚麼都答不出來,悽悽慘慘戚戚。 ----《聲聲慢》作者,李清照
------------------------------------------------------------------------------------------------------------
I am the beginner in LeetCode, I heard that Google Interview problems are coming from this. Hope to one day I can solve rank hard question, by the way, rank easy problems are pretty suitable for programming rookie.
#1 Two Sum [Easy]
C++
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int *a = (int*)malloc(2*sizeof(int));
for(int i = 0;i<numsSize;i++)
{
for(int j = i+1;(j<numsSize && j != i);j++)
{
if(nums[i] + nums[j] == target)
{
a[0] = i;
a[1] = j;
}
}
}
a[0]=5;
return 1;
}
#7 Reverse Integer [Easy]
int reverse(int x){
int max = 2147483648-1;
int min = -2147483648 ;
int output=0;
int tmp,flag;
if(x>=min && x<=max){
if(x>0){
while(x!=0){
tmp=x%10;
if(output>=214748364){
if(output==214748364){
if(tmp<=7){
output=output*10;
output=output+tmp;
x=x/10;
}else{
output=0;
x=0;
}
}else{
output=0;
x=0;
}
}else{
output=output*10;
output=output+tmp;
x=x/10;
}
}
return output;
}else if(x<0){
if(x==-2147483648){
x=x+1;
flag=1;
}else{
flag=0;
}
x=x*(-1);
while(x!=0){
tmp=x%10;
if(output>=214748364){
if(output==214748364){
if(tmp<=7){
output=output*-1;
output=output*10;
output=output-tmp;
x=x/10;
}else{
output=0;
x=0;
}
}else{
output=0;
x=0;
}
}else{
output=output*10;
output=output+tmp;
x=x/10;
}
}
if(output>0) output=output*-1;
if(flag==1 && output!=0) output=output-1;
return output;
}else{
return 0;
}
}else{
return 0;
}
}
2020年3月10日 星期二
訂閱:
文章 (Atom)
2025 MTK 韌體工程師 上機考心得 前言: 以前, 我覺得寫前後端的人才是真正的寫程式, 很羨慕那些大神 直到這次準備, 我才發現靠杯, 原來寫底層的程式也那麼硬派, XOR 一些奇奇怪怪的加速運算操作, 剛看到真的是無法想像, 有夠虧賊! 1. C/C++ Pro...
-
感覺筆電都好貴喔
-
本題難度為 medium,剛開始看到題目就傻住了,知道要用 Dynamic Programming 的解法,不然可能會超時,看一下台師大的演算法筆記,原來這種題目時間複雜度是O(N^4),例如 Uva 12473,參考中英文的資料後,用習慣的 C 語言,總算是把這題解出來了,...
-
# cat /etc/centos-release CentOS Linux release 7.5.1804 (Core) # yum install python-devel # yum install python-crypto # ...