最简单的思想,遍历,

1、两层循环,自己写的,没有用STL,时间花费较长

  vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
for(int i=;i<nums.size();i++){
int temp = target-nums[i];
for(int j=i+;j<nums.size();j++){
if(temp==nums[j]){
result.push_back(i);
result.push_back(j);
return result;
}
}
}
return {, };
}

2、使用Map先插入元素,再对map内的元素进行搜索

 vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map; for (int i = ; i < nums.size(); i ++) {
auto it = map.find(target - nums[i]); if (it != map.end()) {
return { i, it->second };
} map[nums[i]] = i;
} return { , };
}

Leetcode[1]Two Sum C++的更多相关文章

  1. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  4. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  5. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  6. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  7. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  8. LeetCode one Two Sum

    LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...

  9. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  10. [LeetCode] 437. Path Sum III_ Easy tag: DFS

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

随机推荐

  1. 十天精通CSS3(12)

    自由缩放属性resize 为了增强用户体验,CSS3增加了很多新的属性,其中resize就是一个重要的属性,它允许用户通过拖动的方式来修改元素的尺寸来改变元素的大小.到目前为止,可以使用overflo ...

  2. ios 工作日志

    1.设计模式 1.1 想用一个controllerK控制多个页面的切换 但是每一个页面必须要引用这个controller,这样才能控制进度, 所以必须是弱引用.controller必须被某一个实例强引 ...

  3. pandas取dataframe特定行/列

    1. 按列取.按索引/行取.按特定行列取 import numpy as np from pandas import DataFrame import pandas as pd df=DataFram ...

  4. eigen 笔记2

    4. Block operations 1) Using block operations Block of size(p, q), starting at (i, j) dynamic-size b ...

  5. 7.6 Models -- Finding Records

    Ember Data的store为检索一个类型的records提供一个接口. 一.Retrieving a single record(检索单记录) 1. 通过type和ID使用store.findR ...

  6. ac1066

    经过分析后的二分 题目是 Josnch星球是一个赌博之风盛行的星球.每个人一出生就有一定数额的钱,之后的所有收入只能由赌博获得(OMG,如果RP不好,输光了所有的 钱...)假设赌博公司的某场赌博有N ...

  7. python isinstance()与type()的区别

    例如在继承上的区别: isinstance() 会认为子类是一种父类类型,考虑继承关系. type() 不会认为子类是一种父类类型,不考虑继承关系. class A: pass class B(A): ...

  8. centos7源码编译安装Subversion 1.9.5

    svn是Subversion的简称,是一个开放源代码的版本控制系统.svn有两种运行方式:1.独立服务器(svn://xxx.xxx/xxx) 2.借助apache(http://svn.xxx.xx ...

  9. Linux基础命令---which

    which 在环境变量PATH中搜索某个命令,返回命令的执行文件或者脚本位置,默认只显示第一个结果.这需要一个或多个参数.对于它的每个参数,它会打印出当在shell提示符下输入该参数时将执行的可执行文 ...

  10. Notepad++ 主题配色配置

    以下方法来源网络,仅供参考: 方案1: 1.主题名称:Obsidian 2.字体字号:Courier New 10 (这里字体你可以随意选择,让自己满意即可) 3.设置方法:设置---语言格式设置-- ...