#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,vector<int> > mp;
for(int i=0;i<nums.size();++i) mp[nums[i]].push_back(i);
sort(nums.begin(),nums.end());
vector<int> res;
for(int i=0;i<nums.size();++i)
{
for(int j=i+1;j<nums.size();++j)
{
if(nums[i]+nums[j]==target){
res.push_back(i);
res.push_back(j);
if((j-i)==1&&mp[nums[i]].size()>1)
{
res[0] = mp[nums[i]][0];
res[1] = mp[nums[i]][1];
}
else {
res[0] = mp[nums[i]][0];
res[1] = mp[nums[j]][0];
}
return res;
}
else if(nums[i]+nums[j]>target)break;//利用排序后的性质减少判定次数
}
}
return res;
}
};
int main()
{
Solution s;
int a[]={0,2,0};
vector<int> v(a,a+sizeof(a)/sizeof(int));
vector<int> res=s.twoSum(v,0);
for(int &i:res)cout<<i<<endl;
}

1.TwoSum-Leetcode的更多相关文章

  1. TwoSum leetcode

    class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector& ...

  2. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

  3. leetcode — two-sum

    package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...

  4. leetCode:twoSum 两数之和 【JAVA实现】

    LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...

  5. [leetcode]TwoSum系列问题

    1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int ...

  6. [LeetCode] TwoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  7. LeetCode——TwoSum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  8. LeetCode #1 TwoSum

    Description Given an array of integers, return indices of the two numbers such that they add up to a ...

  9. Leetcode 1——twosum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  10. leetcode题解 1.TwoSum

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

随机推荐

  1. numpy数组的计算

    1.数组的形状 查看数组的形状: import numpy as np a = np.array([[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]]) print(a.shape) ...

  2. pku 2425 A Chess Game (SG)

    题意: 给一个由N个点组成的一张有向图,不存在环.点的编号是0~N-1. 然后给出M个棋子所在的位置(点的编号)[一个点上可同时有多个棋子]. 每人每次可移动M个棋子中的一个棋子一步,移动方向是有向边 ...

  3. Serverless 工程实践|自建 Apache OpenWhisk 平台

    作者 | 刘宇(江昱) 前言:OpenWhisk 是一个开源.无服务器的云平台,可以在运行时容器中通过执行扩展的代码响应各种事件,而无须用户关心相关的基础设施架构. OpenWhisk 简介 Open ...

  4. 编译安装与gcc编译器

    先说一下gcc编译器,只知道这是一个老款的编译器.编译的目的也比较重要,就是将c语言的代码编译成可以执行的binary文件. gcc 简单使用(后期补充) eg: gcc example.c    # ...

  5. newusers 拷贝服务器A上的用户,批量添加到其它服务器

    服务器B 需要添加多个用户,要求与服务器A 的用户列表一致 1.拷贝服务器A 上的 /etc/passwd 中用户信息,用user1-10为例 #grep ^user /etc/passwd > ...

  6. 绚丽的色彩从何而来_LOTO示波器实测WS2812B系LED光源

    绚丽的色彩从何而来_LOTO示波器实测WS2812B系LED光源 不管你对 "RGB性能狂升300%" 的梗认同不认同,不可否认,绚丽的彩色很是酷炫,在现在市面上带"灯& ...

  7. 【Python+postman接口自动化测试】(7)Postman 的使用教程

    Postman v6的使用 Postman: 简单方便的接口调试工具,便于分享和协作.具有接口调试,接口集管理,环境配置,参数化,断言,批量执行,录制接口,Mock Server, 接口文档,接口监控 ...

  8. 华为C/C++编码规范+《数学之美》感想

    1.排版 1.1 程序块要采用缩进风格编写, 缩进的空格数为4个.(说明: 对于由开发工具自动生成的代码可以有不一致)1.2 相对独立的程序块之间.变量说明之后必须加空行.1.3 循环.判断等语句中若 ...

  9. 15. mac安装多版本jdk

    一.jdk下载地址 jdk官网下载地址:http://jdk.java.net/archive/ 二.安装jdk Mac的JDK都是安装到一个指定目录的:/Library/Java/JavaVirtu ...

  10. bat批处理命令及解释

    相关原文链接 一.批处理概念 批处理文件:包含DOS命令的可编辑可执行文件 批处理:可以对某一对象批量操作的文件 二.批处理命令简介 命令1~10 1 echo 和 @ 回显命令 @ #关闭单行回显 ...