#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. Spring Cloud Gateway GatewayFilter的使用

    Spring Cloud Gateway GatewayFilter的使用 一.GatewayFilter的作用 二.Spring Cloud Gateway内置的 GatewayFilter 1.A ...

  2. MD支持程度测试

    Editor.md 目录 (Table of Contents) [TOCM] 目录 Editor.md Heading 1 Heading 2 Heading 3 Heading 4 Heading ...

  3. 零基础入门c语言函数之递归函数

    今天来总结一下关于递归函数的使用方面的问题. 递归函数就是在函数使用的时候自己调用自己,层层调用,来实现你想要的功能. 有两个最常用的例子,我们来写一下. (1)计算阶乘 #include int f ...

  4. Netty:Reactor Pattern 与 Dubbo 底层传输中的 NettyServer

    首先,我们需要了解Reactor模式的三种线程模型: 1)单线程模型 Reactor 单线程模型,指的是所有的 IO 操作都在同一个 NIO 线程上面完成,NIO 线程的职责如下: 作为 NIO 服务 ...

  5. Linux cat文件正常,vim文件乱码

    cat: vim: 1.临时解决 vim 文件后,命令模式下执行: :set encoding=utf-8 2.永久解决 vi    配置文件路径:/etc/virc vim 配置文件路径:/etc/ ...

  6. DDTP 分布式数据传输协议白皮书

    声明 本文非本人原创,主要参考文献[1]编写的阅读笔记.本博客仅发表在博客园,作者LightningStar,其他平台均为转载. 摘要 本白皮书对全球现有主要个人信息可携带权的实践模式进行梳理,分析其 ...

  7. 小入门 Django(做个疫情数据报告)

    Django 是 Python web框架,发音 [ˈdʒæŋɡo] ,翻译成中文叫"姜狗". 为什么要学框架?其实我们自己完全可以用 Python 代码从0到1写一个web网站, ...

  8. Qt 使用大神插件快速创建树状导航栏

    前言 本博客仅仅记录自己的采坑过程以及帮助网友避坑,方便以后快速使用自定义控件,避免重复出错. 下载插件 大神 Github Qt 自定义控件项目地址:https://github.com/feiya ...

  9. Mysql教程:(一)数据库常用基础命令

    数据库常用命令 1.登录 进入数据库,在win系统下,打开cmd,切换用户权限,进入root: 沒权限,用root登录: mysql -uroot 如果root有密码: mysql -uroot -p ...

  10. sqlalchemy mysql server has gone

    mixing multiprocessing and SQLAlchemy is a bad idea. In general your processes should each contain a ...