leetcode笔记--SUM问题
引用自 http://blog.csdn.net/wangxiaojun911/article/details/18922337,此处仅作为自己参考
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.
方法1://该算法找出排好序的vector中相加等于target的两个数值 //最小和最大相加,然后和target比较。如果和比较小,则左侧移动;如果和比较大,右侧移动
class Solution {
public:
/*Below is the 2 sum algorithm that is O(NlogN) + O(N)*/
/*Alternative: hash从左往右扫描一遍,然后将数及坐标,存到map中。然后再扫描一遍即可。时间复杂度O(n)*/
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> numbersCopy;
for(int i = ; i < numbers.size(); i++) numbersCopy.push_back(numbers[i]);
sort(numbersCopy.begin(), numbersCopy.end()); //O(NlogN)
vector<int> returnNumbers = twoSumAlgorithm(numbersCopy, target);//O(N)
//遍历查找返回的两个值的下标,时间复杂度为O(n);
vector<int> returnIndexes;
for(int j = ; j < returnNumbers.size(); j++)
for(int i = ; i < numbers.size(); i++)//O(N)
if(numbers[i] == returnNumbers[j]) returnIndexes.push_back(i + );
if(returnIndexes[] > returnIndexes[]){
returnIndexes[] = returnIndexes[]^returnIndexes[];
returnIndexes[] = returnIndexes[]^returnIndexes[];
returnIndexes[] = returnIndexes[]^returnIndexes[];
}
return returnIndexes;
}
/*Core algorithm is linear*/
//该算法找出排好序的vector中相加等于target的两个数值
//最小和最大相加,然后和target比较。如果和比较小,则左侧移动;如果和比较大,右侧移动
vector<int> twoSumAlgorithm(vector<int> &numbers, int target) {
int len = numbers.size();
vector<int> r;
int i = ; int j = len - ;
while(i < j){
int x = numbers[i] + numbers[j];
if(x == target){
r.push_back(numbers[i]);
r.push_back(numbers[j]);
i++; j--;
}else if(x > target) j--;
else i++;
}
return r;
}
};
方法2://unordered_map
2.Three SUM
leetcode笔记--SUM问题的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
随机推荐
- [SDOI2018]荣誉称号
题解: 并不需要什么算法 首先我们随便画一画就会发现 能画出一颗满二叉树 然后要满足每个点从上往下的路径和都相同(%m意义下) 一个点上可能对应了多个点 然后这样我们可以暴力dp $2^k*m^2+n ...
- 总结Flink状态管理和容错机制
本文来自8月11日在北京举行的 Flink Meetup会议,分享来自于施晓罡,目前在阿里大数据团队部从事Blink方面的研发,现在主要负责Blink状态管理和容错相关技术的研发. 本文主要内容如 ...
- MySQL 命令总结
MySQL命令总结 1.数据库操作 查看在当前服务器中有多少个数据库 创建数据库 >CREATE DATABASE db_name DEFAULT CHARACTER SET utf8 COLL ...
- lvs-nat 实验
1 ipvsadm集群服务管理工具使用 安装ipvsadm: yum install ipvsadm 参数: Ipvsadm -h : 获取帮助 -A 创建一个新的集群服务 -E 修 ...
- jQuery Validate自定义错误信息,自定义方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ArrayList类中的contains()方法底层依赖的是equals()方法
ArrayList类中的contains()方法底层依赖的是equals()方法.若集合中的元素是自定义对象,则应该重写该类父类Object的equals()方法,否则对象永远都不相同(因为都是new ...
- Shiro笔记(二)Shiro集成SpringMVC的环境配置
0.pom文件引入 <!-- SECURITY begin --> <dependency> <groupId>org.apache.shiro</group ...
- mybatis提示Invalid bound statement (not found)错误的可能原因
https://www.cnblogs.com/liaojie970/p/8034525.html
- scala-Unit-3-Scala基础语法2
一.定义方法.函数.及方法转换为函数 1.方法的定义: def 方法名(参数1:数据类型,参数2:数据类型...):返回值类型 ={ } 2.定义函数: val h1 = (a:Int,b:Int) ...
- codeforces 1058B - Vasya and Cornfield
<题目链接> 题目大意: 给出一个矩形,该矩形的四个顶点分别为:(0,d),(d,0),(n,n−d) and (n−d,n).然后给出一些点的坐标,分别判断这些点是否在该矩形内. 解题分 ...