引用自 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问题的更多相关文章

  1. 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 ...

  2. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  5. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  9. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

随机推荐

  1. css3三角形冒泡泡图形制作

    图一: 图二: <!DOCTYPE html> <html> <head> <title>css 三角形</title> <style ...

  2. Delphi Setlength 内存释放总结

    https://blog.csdn.net/lotusyangjun/article/details/8203521 一.在Setlength 被调用次数不多时,可直接使用以下代码进行释放var aa ...

  3. [已解决]virtualBox安装CentOS-6.3-x86_64-bin-DVD1.iso为什么总是显示命令行界面

    CentOS 6.3的安装界面分为2种,一种是图形化安装界面,另一种则类似于Dos系统的纯文本安装界面. 进入图形安装界面的必要条件是硬件系统的物理内存大于628M以上即可,因为之前在VBox虚拟机里 ...

  4. net core体系-2继续认识net core

    认识net core,net core到底啥?从哪说起呢?我想作为开发的码农,web项目不陌生吧,那就从对应的.net web 对应的net core Web Application项目开始吧. 下面 ...

  5. SpringBoot Controller接收参数的几种常用方

    第一类:请求路径参数 1.@PathVariable 获取路径参数.即url/{id}这种形式. 2.@RequestParam 获取查询参数.即url?name=这种形式 例子 GET http:/ ...

  6. Python学习(二十七)—— Django和pymysql搭建学员管理系统

    转载自http://www.cnblogs.com/liwenzhou/p/8270250.html 一.学员管理系统 1.项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的e ...

  7. AtCoder Grand Contest 017D (AGC017D) Game on Tree 博弈

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC017D.html 题目传送门 - AGC017D 题意 给定一棵 n 个节点的以节点 1 为根的树. 两个 ...

  8. docker下搭建zipkin for mysql

    docker pull openzipkin/zipkin 新建docker-compose.yml加入以下内容,自行修改. version: ' services: # The zipkin pro ...

  9. React前端框架路由跳转,前端回车事件、禁止空格、提交方式等方法

    react router - historyhistory.push() 方法用于在JS中实现页面跳转history.go(-1) 用来实现页面的前进(1)和后退(-1) 访问js连接后+?v1清缓存 ...

  10. Codeforces 1105D Kilani and the Game【BFS】

    <题目链接> 题目大意: 每个玩家控制一个颜色去扩张,每个颜色的扩张有自己的速度,一个颜色跑完再跑下一种颜色.在所有颜色不能在继续扩张的时候停止游戏.询问此时各种颜色的数量. 解题分析: ...