[c] /** * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* nums, int numsSize, int target) { static int a[2]={0};
for (int i = 0; i < numsSize - 1; i++) { for (int j = i+1; j < numsSize; j++) { if (nums[i] + nums[j] == target) { a[0] = i; a[1] = j; return a; } } } return 0; }
[cpp] /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ #include<math.h> class Solution { public: struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { ListNode * ans = new ListNode(0); ListNode * s = ans; int flag = 0; int i = 0; int nt = 0; while(l1&&l2){ nt = l1->val + l2->val; if(flag==1){ nt+=1; } if(nt >= 10){ flag = 1; nt = nt % 10; }else{ flag = 0; } l1 = l1->next; l2 = l2->next; ans->val = nt; if(!l1&&!l2&&flag==1){ ans -> next = new ListNode(0); ans = ans -> next; ans -> val = 1; ans ->next = NULL; }else if(l1&&l2){ ans->next = new ListNode(0); ans = ans->next; }else{ while(l1){ if(flag == 1){ nt = 1 + l1->val; }else{ nt = l1->val; } if(nt >= 10){ flag = 1; nt = nt % 10; }else{ flag = 0; } ans -> next = new ListNode(0); ans = ans -> next; ans->val = nt; l1 = l1->next; if(!l1){ ans->next = NULL; } } while(l2){ if(flag == 1){ nt = 1 + l2->val; }else{ nt = l2->val; } if(nt >= 10){ flag = 1; nt = nt % 10; }else{ flag = 0; } ans -> next = new ListNode(0); ans = ans -> next; ans->val = nt; l2 = l2->next; if(!l2){ ans->next = NULL; } } if(flag == 1){ ans -> next = new ListNode(0); ans = ans -> next; ans->val = 1; ans -> next = NULL; } } } return s; } };