leetcode------第一题 Two Sum
来源:互联网 发布:网络隔离变压器 编辑:程序博客网 时间:2024/06/01 07:57
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.
Example:
Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].简要题解:题目需要在一个数组里面找到两个数,使得这两个数的和等于目标数,然后返回这两个数的序号。在这里,我使用的是最简单,最暴力的解法。先让每两个数相加,然后判断它们的和是否等于目标数 ,如果等于,则返回对应的序号。
源代码如下:
class Solution {
public:vector<int> twoSum(vector<int>& nums, int target) {
for (int i = 0; i < nums.size(); i++) {
for (int j = i+1; j < nums.size(); j++) {
if (nums[i]+nums[j] == target) {
vector<int> a;
a.push_back(i);
a.push_back(j);
return a;
}
}
}
}
};
阅读全文
1 0
- leetcode 第一题 two sum
- LeetCode 第一题,Two Sum
- leetcode第一题two sum
- Two Sum leetcode第一题
- two Sum leetCode第一题
- leetcode 第一题:two sum
- LeetCode第一题Two Sum
- Leetcode第一题--------- Two Sum
- Leetcode第一题<two sum>
- LeetCode第一题:Two Sum
- leetcode------第一题 Two Sum
- LeetCode 1.Two Sum ------------LeetCode 第一题
- Leetcode 第一题 Two Sum java代码
- Leetcode 之第一题 two sum
- Leetcode oj1 第一题 two sum
- 第一题: Two Sum
- 第一题 two-sum
- leetcode第一题(medium)——Two Sum
- 数据库事务的四大特性以及事务的隔离级别
- 搜索引擎solr系列---solr介绍(待扩充)
- JAVA中实现对Excel的导入与导出
- matlab安装
- git 的配置在哪
- leetcode------第一题 Two Sum
- Majority Element
- Tensorflow 实战 笔记
- 树的高度,深度,层数
- 强化学习基础学习系列之强化学习简介
- Oracle数据库--表锁的情况查询
- [ 工作--管理]-------项目管理软技术
- 寻向所志大学生
- Java 集合框架-Map