LeetCode - 1. Two Sum
2016-04-05 19:18
239 查看
方法一
知识点:
1. vector中成员函数size返回相应vector类定义的size_typed的值
2. vector常用操作,v.empty():如果v为空则返回true,否则返回false;v.size();v.push_back()
3. 每种容器都定义了自己的迭代器类型
4. 迭代器的应用
5. java中array可以使用.length得到长度
方法二
思路:这一题目同样可以理解为在已知两个数字的情况下从一个数组中寻找另一个数是否存在的问题,因为题目所要返回的是index,而找到另一个数的关键在于value,所以可以想到使用hashmap来建立两者之间的联系
public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
//init hashmap
for(int i = 0; i < nums.length; i++){
map.put(nums[i], i);
}
//find the complement
int complement;
for(int i = 0; i < nums.length; i++){
complement = target - nums[i];
if(map.containsKey(complement) && map.get(complement) != i){
return new int[] {i, map.get(complement)};
}
}
throw new IllegalArgumentException("No such numbers");
}
}
知识点:
1. java中数组有length这一个属性,可以使用.length获取数组长度
2. map.put(k, v); map.containsKey(k); map.get(v)
3. java中快速创建数组的方法new int[] {a, b};
4. 在没有答案的时候可以不使用return语句,而是使用throw new IllegalArgumentException(String);
方法三
思路:在方法二中,我们首先iterate数组初始化hashmap,然后再次遍历数组寻找目标结果,其实这两个步骤可以在同一次遍历中完成。但是在同一次遍历完成的时候,要注意是先找还是先存,这里使用了先找的方式,因为这样可以明确在map.put时,把index小的数放在前面,而且也避免了检查自身的问题
public class Solution {
public int[] twoSum(int[] nums, int target) {
int complement;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
//find and store in one iteration
for(int i = 0; i < nums.length; i++){
//find
complement = target - nums[i];
if(map.containsKey(complement)){
return new int[] {map.get(complement), i};
}
//store
map.put(nums[i], i);
}
throw new IllegalArgumentException("No such numbers");
}
}
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { for(vector<int>::size_type i = 0; i != nums.size(); ++i){ for(vector<int>::size_type j = i + 1; j != nums.size(); ++j){ if(nums[i] + nums[j] == target){ //construct result vector<int> result; result.push_back(i); result.push_back(j); return result; } } } } };
public class Solution { public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; for(int i = 0; i < nums.length; ++i){ for(int j = i + 1; j < nums.length; ++j){ if(nums[i] + nums[j] == target){ //construct result result[0] = i; result[1] = j; break; } } } return result; } }
知识点:
1. vector中成员函数size返回相应vector类定义的size_typed的值
2. vector常用操作,v.empty():如果v为空则返回true,否则返回false;v.size();v.push_back()
3. 每种容器都定义了自己的迭代器类型
4. 迭代器的应用
for(vector<int>::size_type ix; ix != ivec.size(); ++ix) ivec[ix] = 0;
for(vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter) *iter = 0;
for(vector<string>::const_iterator iter = text.begin(); iter != text.end(); ++iter) *iter = " "; //error, *iter is const
const_iterator类型只能用于读取容器内的元素,但不能改变其值
5. java中array可以使用.length得到长度
方法二
思路:这一题目同样可以理解为在已知两个数字的情况下从一个数组中寻找另一个数是否存在的问题,因为题目所要返回的是index,而找到另一个数的关键在于value,所以可以想到使用hashmap来建立两者之间的联系
public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
//init hashmap
for(int i = 0; i < nums.length; i++){
map.put(nums[i], i);
}
//find the complement
int complement;
for(int i = 0; i < nums.length; i++){
complement = target - nums[i];
if(map.containsKey(complement) && map.get(complement) != i){
return new int[] {i, map.get(complement)};
}
}
throw new IllegalArgumentException("No such numbers");
}
}
知识点:
1. java中数组有length这一个属性,可以使用.length获取数组长度
2. map.put(k, v); map.containsKey(k); map.get(v)
3. java中快速创建数组的方法new int[] {a, b};
4. 在没有答案的时候可以不使用return语句,而是使用throw new IllegalArgumentException(String);
方法三
思路:在方法二中,我们首先iterate数组初始化hashmap,然后再次遍历数组寻找目标结果,其实这两个步骤可以在同一次遍历中完成。但是在同一次遍历完成的时候,要注意是先找还是先存,这里使用了先找的方式,因为这样可以明确在map.put时,把index小的数放在前面,而且也避免了检查自身的问题
public class Solution {
public int[] twoSum(int[] nums, int target) {
int complement;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
//find and store in one iteration
for(int i = 0; i < nums.length; i++){
//find
complement = target - nums[i];
if(map.containsKey(complement)){
return new int[] {map.get(complement), i};
}
//store
map.put(nums[i], i);
}
throw new IllegalArgumentException("No such numbers");
}
}
相关文章推荐
- 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 题解