Leetcode 1-Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
思路:用unordered_map来存储元素和索引的对应关系,
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> m;
vector<int> res;
for(int i=0;i<nums.size();++i){
m[nums[i]]=i;//将元素和索引建立对应关系
}
for(int i=0;i<nums.size();++i){
int t=target-nums[i];//需要找的第二个数
if(m.count(t) && m[t]!=i){//t要存在,且假设t和num[i]是相同的数,不能返回同一个索引,应该找到另一个索引
res.push_back(i);
res.push_back(m[t]);
break;
}
}
return res;
}
};
Leetcode 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.
思路:两个链表中的数从左往右是从低位到高位,正好就可以依着链表顺序相加。
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* res=new ListNode(0);//每次可以首先看看返回类型
ListNode* p=l1;
ListNode* q=l2;//用p,q存储当前计算到的数
ListNode* cur=res;//cur指向结果链表的当前元素
int carry=0;//进位
while(p!=NULL || q!=NULL){//若当前链表元素不全为空时,进行加法
int sum=0,x=0,y=0;
if(p){
x=p->val;
p=p->next;
}
if(q){
y=q->val;
q=q->next;
}
sum=x+y+carry;
carry=sum/10;
cur->next=new ListNode(sum%10);
cur=cur->next;
}
if(carry){//如果最后链表中的元素都计算完了,但carry不为0,说明还要再向前进一位
cur->next=new ListNode(carry);
}
return res->next;//因为res第一个位置没有存
}
};
Leetcode 445- Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
思路:两个链表从左到右是高位到低位,不让翻转链表,考虑用栈的方式,把链表中的数据存储起来,然后在出栈依次相加。
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* res=nullptr;//声明为头指针,没有生成头节点
vector<int> num1,num2;//用来存储链表中的数字
while(l1){
num1.push_back(l1->val);
l1=l1->next;
}
while(l2){
num2.push_back(l2->val);
l2=l2->next;
}
int carry=0,sum=0;
ListNode* tmp=nullptr;
int m=num1.size(),n=num2.size();
for(int i=m-1, j=n-1;i>=0 ||j>=0 ||carry>0;i--,j--){
sum=carry;
if(i>=0) sum+=num1[i];
if(j>=0) sum+=num2[j];
carry=sum/10;
tmp=new ListNode(sum%10);
tmp->next=res;
res=tmp;
}
return res;//因为第一个位置的节点有存储
}
};
参考文献:
https://www.cnblogs.com/chenyingjie1207/p/10037008.html
https://blog.csdn.net/oqqHuTu12345678/article/details/73556599