【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 ...
随机推荐
- question?
- freebsd静态路由
FreeBSD下增进静态路由的行动 1.手工添加 # route add -net 192.168.2.0/24 192.168.1.2 2. 通过rc.conf永世 设置 # Add static ...
- Warning: require(): open_basedir restriction in effect. File(/www/wwwroot/../thinkphp/start.php) is not within the allowed path(s):
Warning: require(): open_basedir restriction in effect. File(/www/wwwroot//../thinkphp/start.php) is ...
- jquery制作滚动条到一定位置触发
$(function(){ var nav=$(".nav"); //得到导航对象 var win=$(window); //得到窗口对象 var sc=$(document);/ ...
- 微信小程序从入坑到放弃之坑十二:navigator无法跳转的坑
转自:http://www.yilingsj.com/xwzj/2018-11-25/weixin-miniprogram-navigator.html 微信小程序中的页面跳转用navigator就行 ...
- 无法访问win8默认共享(如C$)解决办法
可以使用此过程允许作为本地 Administrators 组的成员并使用密码身份验证登录的用户在会话过程中使用其管理权限.启动注册表编辑器.单击“开始”,在“开始搜索”框中键入 regedit,然后按 ...
- 多线程 同步对象 event 简单实例 &进程间通信
多线程 同步对象event import threading,time class Boss(threading.Thread): def run(self): print("BOSS:今晚 ...
- Windows Nginx 基本操作
Nginx 下载 下载路径:Nginx下载 下载后解压如下图 常用命令 在nginx目录下打开命令行工具(可能需要管理员权限). start nginx : 启动nginxnginx -t 测试ngi ...
- jquery排序与动态添加option以及属性
function getOrgansid() { url="<%=basePath%>/rest/bsc/organ/selectOrganSidAllList"; $ ...
- 找不到 org/springframework/dao/support/PersistenceExceptionTranslator
如果用的spring2 则原因是缺少spring-dao.jar 如果用的是spring3(我就栽这儿了) 则原因是缺少org.springframework.transaction-3.0.4.R ...