【LeetCode C++】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.
Example:
Given nums = [, , , ], target = , Because nums[] + nums[] = + = ,
return [, ].
解题:
题目的意思是给定一个一维的数组nums和一个目标值target,查找数组中是否存在两个整数元素的和为目标值target。返回符合条件的整数元素在一维数组中的位置。
遍历法:
时间复杂度:O(n2) Runtime: 760 ms
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> index;for(int i = ; i< nums.size(); i++)
{
for(int j = i+; j< nums.size(); j++)
{
if(target==(nums[i]+nums[j]))
{
index.push_back(i);
index.push_back(j);
return index;
}
}
}
return index;
}
};
运行结果:
遍历法是最耗时的算法,因此需要进一步优化算法。
【LeetCode C++】Two Sum的更多相关文章
- 【LeetCode OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Path Sum
Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...
- 【LeetCode练习题】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【Leetcode 167】Two Sum II - Input array is sorted
问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数.返回这两个数的下标(从1开始),其中第1个下标比第2个下标小. Input: numbers={2, 7, 11, 15}, t ...
- 【LeetCode OJ】Two Sum
题目:Given an array of integers, find two numbers such that they add up to a specific target number. T ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode题解】136_只出现一次的数字
目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...
- 【LeetCode题解】2_两数相加
目录 [LeetCode题解]2_两数相加 描述 方法一:小学数学 思路 Java 代码(非递归写法) Java 代码(递归写法) Python 代码(非递归写法) [LeetCode题解]2_两数相 ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
随机推荐
- servlet对应.net中的http上下文
java中的servlet在.net中其实就是http上下文.
- Julia - 复数
全局变量 im 即复数 i ,为复数的虚数单位,表示 -1 的正平方根 Julia 允许数值作为代数系数,这也适用于复数 julia> 1 + 2im 1 + 2im 复数的运算 julia&g ...
- vue-cli 自定义过滤器的使用
vue-cli 自定义过滤器的使用 vue2.0将内置过滤器去除,所以过滤器需要自己编写. Vue.js 允许你自定义过滤器,可被用作一些常见的文本格式化.过滤器可以用在两个地方:mustache 插 ...
- 微信小程序中this关键字使用技巧
转自:https://blog.csdn.net/qq_33956478/article/details/81348453 微信小程序中,在wx.request({});方法调用成功或者失败之后,有时 ...
- Eclipse 异常关闭
缺失 Java Builder 造成运行main方法,找不到主类, 系统没有自动编译 在.project 文件中添加 <buildSpec> <buildCommand> &l ...
- ubuntu下安装oracle java8
1.首先添加ppa $ sudo add-apt-repository ppa:webupd8team/java 2.然后更新系统 $ sudo apt-get update 3.最后开始安装 $ s ...
- Elasticsearch-2.4.3的单节点安装(多种方式图文详解)
前提: Elasticsearch-2.4.3的下载(图文详解) 1.新建es安装目录 [root@djt002 local]# mkdir elasticsearch [root@djt002 lo ...
- #if (DEBUG)
//DEBUG必须大写,其它是不认的#if (DEBUG) Console.WriteLine("Debug");#else Conso ...
- C#路径的相关操作
1.判定一个给定的C#路径是否有效,合法 通过Path.GetInvalidPathChars或Path.GetInvalidFileNameChars方法获得非法的C#路径/文件名字符,可以根据它来 ...
- FreeSWITCH 启用多域(多租户)的配置
如果将FreeSWITCH用于云端, 支持大规模并发呼叫, 就要用到 多域/多租户 技术了, FreeSWITCH 本身可以直接支持. 每个域可以单独, 拥有相同的分机号也互相打不通, 各自线路, I ...