引用自 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. element-ui上传文件带token

    template> <el-upload action="test" :headers="myHeaders"></el-upload& ...

  2. UICollectionViewController的用法1

    UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableVie ...

  3. jenkins(8): 实战jenkins+gitlab持续集成发布php项目(代码不需要编译)

    一. jenkins 的配置 1.前提条件安装了GitLab Plugin (源码管理使用),GitLab Hook(gitlab webhook需要) Manage Jenkins--->Ma ...

  4. 创建自己的composer包

    需求:在项目中输入 p($arr); 将会格式化输出 一.在GitHub上创建仓库 1.1这个仓库必须包含composer.json文件,内容如下. { "name": " ...

  5. BZOJ4237 稻草人 分治 单调栈

    原文链接https://www.cnblogs.com/zhouzhendong/p/8682572.html 题目传送门 - BZOJ4237 题意 平面上有$n(n\leq 2\times 10^ ...

  6. 9、Qt Project之简单的数据库接口

    简单的数据库接口  Step1:首先完成整个UI界面的额设计: <?xml version="1.0" encoding="UTF-8"?> < ...

  7. group by 和where 条件后面不能用刚设置的别名。

    select count(*),c_xy_bj a from z_user group by c_xy_bj     这个group by后面不能使用c_xy_bj 字段的别名a,只有外面再嵌套sel ...

  8. Java常用API及Math类

    一.API的概述 API——Application Programing Interface:应用程序编程接口,是java提供的一些预定义的函数: 目的:基于API实现程序的快速编写,只需了解其作用, ...

  9. es 模块的基础知识,深度了解

    // 一模块的基础知识 /** * export :用于模块输出的出口 * import :文件引入的入口 */ // 1,第一种方式使用export方式输出 var a = 'a'; var b = ...

  10. ZOJ 1109 Language of FatMouse 【Trie树】

    <题目链接> 题目大意: 刚开始每行输入两个单词,第二个单词存入单词库,并且每行第二个单词映射着对应的第一个单词.然后每行输入一个单词,如果单词库中有相同的单词,则输出它对应的那个单词,否 ...