leetcode的c++实现(一)
2016-04-28 19:23
459 查看
说在前面的话:c++的书看了几遍,但是平时写的机会比较少,所以忘得很快,俗话说读万卷书行万里路,没有实践理论的东西都是浮云。正好leetcode给我们提供了一个温故而知新的平台,我就计划借助这个平台在无聊的时候写写代码,慢慢把c++捡起来。
下面的代码有的是自己写的,有的是在讨论区看到的让我醍醐灌顶的。这个系列的博客用来记录自己这个渐渐追忆、慢慢进步的过程,也方便以后找工作的时候复习。
题目:
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.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
方法一:两个for循环,时间复杂度为O(N2)
方法二:关联容器
参考:
https://leetcode.com/discuss/99740/leetbook-%E4%B8%89%E7%A7%8D%E6%80%9D%E8%B7%AF%E8%A7%A3%E5%86%B3%E8%BF%99%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E9%97%AE%E9%A2%98%EF%BC%88%E9%99%84%E8%A7%A3%E9%87%8A%EF%BC%8C%E5%9B%BE%E7%A4%BA%EF%BC%8C%E4%BB%A3%E7%A0%81%EF%BC%89
这一题主要让我复习了向量和关联容器的操作以及哈希表的相关知识。
ps:在写代码的过程中遇到了下面这个bug:
control reaches end of non-void function [-Werror=return-type]
这好像是因为不一定能return而导致的,把return放对位置就可以了。譬如把return放在了判断语句里面,万一那个判断语句一直不执行那么就没有返回值了。
下面的代码有的是自己写的,有的是在讨论区看到的让我醍醐灌顶的。这个系列的博客用来记录自己这个渐渐追忆、慢慢进步的过程,也方便以后找工作的时候复习。
题目:
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.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
方法一:两个for循环,时间复杂度为O(N2)
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> result; int sum = 0; for (int i = 0;i < nums.size();i++){ for (int j = i+1;j < nums.size();j++){ sum = nums[i] + nums[j]; if (sum == target){ result.push_back(i); result.push_back(j); break; } } } return result; } };
方法二:关联容器
参考:
https://leetcode.com/discuss/99740/leetbook-%E4%B8%89%E7%A7%8D%E6%80%9D%E8%B7%AF%E8%A7%A3%E5%86%B3%E8%BF%99%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E9%97%AE%E9%A2%98%EF%BC%88%E9%99%84%E8%A7%A3%E9%87%8A%EF%BC%8C%E5%9B%BE%E7%A4%BA%EF%BC%8C%E4%BB%A3%E7%A0%81%EF%BC%89
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> result; unordered_map<int,int> aMap; int res; for(int i = 0;i < nums.size();i++) { res = target - nums[i]; unordered_map<int,int>::iterator it = aMap.find(res); if(it != aMap.end()) { result.push_back(it->second); result.push_back(i); break; } aMap[nums[i]] = i; } return result; } };
这一题主要让我复习了向量和关联容器的操作以及哈希表的相关知识。
ps:在写代码的过程中遇到了下面这个bug:
control reaches end of non-void function [-Werror=return-type]
这好像是因为不一定能return而导致的,把return放对位置就可以了。譬如把return放在了判断语句里面,万一那个判断语句一直不执行那么就没有返回值了。
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解