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 ...
随机推荐
- [转] js对象监听实现
前言 随着前端交互复杂度的提升,各类框架如angular,react,vue等也层出不穷,这些框架一个比较重要的技术点就是数据绑定.数据的监听有较多的实现方案,本文将粗略的描述一番,并对其中一个兼容性 ...
- Comparison of several types of convergence
In functional analysis, several types of convergence are defined, namely, strong convergence for ele ...
- nginx 正则及rewrite常用规则实例
一.正则表达式匹配,其中:* ~ 为区分大小写匹配* ~* 为不区分大小写匹配* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配二.文件及目录匹配,其中:* -f和!-f用来判断是否存在文件* ...
- 【AtCoder】AGC016
A - Shrinking 用每个字母模拟一下就行 #include <bits/stdc++.h> #define fi first #define se second #define ...
- 【Android】Android apk默认安装位置设置
在Android工程中,设置apk的默认安装位置 在AndroidManifest.xml文件Manifest标签中添加android:installLocation属性 android:instal ...
- 未在本地计算机上注册“OraOLEDB.Oracle.1”提供程序。
问题描述:运行访问oracle数据库的.net程序时,弹出错误"未在本地计算机上注册“OraOLEDB.Oracle.1”提供程序". 系统环境:windows server 2 ...
- JS元素意外点击元素消失
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- systemd创建自定义服务(Ubuntu)
/lib/systemd/system下创建test.service文件 vim /lib/systemd/system/test.service [Unit] Description=test [S ...
- 07. Matplotlib 3 |表格样式| 显示控制
1.表格样式创建 表格视觉样式:Dataframe.style → 返回pandas.Styler对象的属性,具有格式化和显示Dataframe的有用方法 样式创建:① Styler.applymap ...
- BroadcastReceiver插件化解决方案
--摘自<android插件化开发指南> 1.静态广播和动态广播仅区别于注册方式的不同.静态广播的注册信息保存在PMS中,动态广播的注册信息保存在AMS中 2.发送广播,也就是Contex ...