开始leetcode的第一天,以前都是刷的都是hdu和poj还有pat的题目,突然改做leetcode真的有点不习惯,今天做的题目不是很多呢,慢慢来叭,加油叭~
1.两数之和
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { int i,j; for(i=0;i<nums.size()-1;i++){ for(j=i+1;j<nums.size();j++){ if(nums[i]+nums[j]==target){ return {i,j}; } } } return {i,j}; } };
2.两数相加
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* result = new ListNode(0); ListNode* cur = result; int carry = 0; int sum = 0; while(l1!=nullptr||l2!=nullptr){ sum = carry; if(l1!=nullptr){ sum += l1->val; l1 = l1->next; } if(l2!=nullptr){ sum += l2->val; l2 = l2->next; } carry = sum/10; cur->next = new ListNode(sum%10); cur = cur->next; } if(carry!=0){ cur->next = new ListNode(carry); } return result->next; } };
3.无重复字符的最长子串
这个用hashmap做的效率是最好的
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20class Solution { public: int hashmap[130]; int lengthOfLongestSubstring(string s) { int max=0; int n = s.size(); for(int i=0,j=0;j<n;j++){ hashmap[s[j]]++; while(hashmap[s[j]]>1){ hashmap[s[i]]--; i++; } if(j-i+1>max){ max = j-i+1; } } return max; } };
最后
以上就是凶狠发夹最近收集整理的关于我的leetcode刷题之旅~day011.两数之和2.两数相加3.无重复字符的最长子串的全部内容,更多相关我内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复