#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. NorFlash、NandFlash在技术和应用上有些什么区别?

    首先你要搞懂什么是Flash Memory? Flash Memory(快闪存储器),是一种电子式可清除程序化只读存储器的形式,允许在操作中被多次擦或写的存储器.这种科技主要用于一般性数据存储,以及在 ...

  2. 计算机网络之流量控制(停止-等待协议、滑动窗口、后退N帧协议GBN、选择重传协议SR)、滑动窗口、可靠传输机制

    文章转自:https://blog.csdn.net/weixin_43914604/article/details/104908762 学习课程:<2019王道考研计算机网络> 学习目的 ...

  3. linux下文件后面带~

    之前发现有时候在命令行ls会看到一些文件后面带有-,而这些文件的名字和我们文件夹中的某些文件是一模一样的文件,在文件夹中没发现就很大胆地删掉了也没是,一直没管,觉得是什么临时复制的文件或者隐藏文件.今 ...

  4. reorder-list leetcode C++

    Given a singly linked list L: L 0→L 1→-→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→- You ...

  5. triangle leetcode C++

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  6. Oracle 扩容表空间

    system用户登陆oracle https://blog.csdn.net/zyingpei/article/details/88870693 首先查看表空间对应的数据文件位置以及大小 select ...

  7. openssh 8.2 升级 8.3

    openssh 8.2 存在安全漏洞,升级为 openssh 8.3 需要安装的包:openssh-8.3p1.tar.gz.zlib-1.2.11.tar.gz.openssl-1.1.1g.tar ...

  8. rocketmq有序消息的(四)

    opic的有序消息已经成为mq的标配.而RocketMQ中是这样区分消息类型的, 普通消息也叫做无序消息,简单来说就是没有顺序的消息,而有序消息就是按照一定的先后顺序的消息类型.举个例子,produc ...

  9. 如何系统学习C 语言(下)之 预处理命令篇

    大话c语言(下)之 预处理命令篇 预处理就是在编译之前,通过一些预处理命令对源代码进行管理和控制的过程. 由源代码得到可执行的程序,会经过预处理.编译.汇编和链接几个过程 预处理命令大致可以分为文件包 ...

  10. VM的三种连接方式(转载)

    概述: VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模 ...