1.TwoSum-Leetcode
#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的更多相关文章
- TwoSum leetcode
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector& ...
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
- leetcode — two-sum
package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...
- leetCode:twoSum 两数之和 【JAVA实现】
LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...
- [leetcode]TwoSum系列问题
1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int ...
- [LeetCode] TwoSum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- LeetCode——TwoSum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- LeetCode #1 TwoSum
Description Given an array of integers, return indices of the two numbers such that they add up to a ...
- Leetcode 1——twosum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode题解 1.TwoSum
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
随机推荐
- webpack基础以及webpack中babel的配置
webpack 安装 npm 初始化,控制台输入 npm init -y webpack 安装 npm i webpack webpack-cli -D 新建 webpack.config.js co ...
- RAW RGB格式
RAW RGB格式 10bit Raw RGB, 就是说用10bit去表示一个R, G, 或者B, 通常的都是用8bit的. 所以你后面处理时要把它转换为8bit的, 比较简单的方法就是将低两位去掉, ...
- stm32看门狗详细解答,看了觉得一下子明白了很多
一.独立看门狗 STM32 的独立看门狗由内部专门的 40Khz 低速时钟驱动,即使主时钟发生故障,它也仍然有效. 看门狗的原理:单片机系统在外界的干扰下会出现程序跑飞的现象导致出现死循环,看门狗电路 ...
- c++ IO库
1:为了支持使用宽字符的语言,标准库定义了一组类型和对象来操作wchar_t类型的数据.宽字符版本的类型和函数的名字以w开头.宽字符版本和普通的char版本定义在同一个头文件中,例如头文件fstrea ...
- ONVIF客户端中预置位设置代码实现过程
simpleOnvif的功能:提供支持Windows.Linux.arm.Android.iOS等各种平台的SDK库,方便集成,二次开发 之前跟大家分享了我们安徽思蔷信息科技的simpleOnvif的 ...
- k8s入坑之路(13)服务迁移(定时任务 微服务 传统服务)
定时任务迁移kubernetes 服务迁移步骤 1.安装好java 2.安装好maven 项目打包 mvn package 测试传参运行 java -cp cronjob-demo-1.0-SNAPS ...
- "简单"的优化--希尔排序也没你想象中那么难
写在前边 大家好,我是melo,一名大二上软件工程在读生,经历了一年的摸滚,现在已经在工作室里边准备开发后台项目啦. 不过这篇文章呢,还是想跟大家聊一聊数据结构与算法,学校也是大二上才开设了数据结构这 ...
- 【python+postman接口自动化测试】(1)网络基础知识
一.IP地址 就像每个人都有一个身份证号码 IP地址是IP协议提供的一种统一的地址格式,它为互联网上的每一个网络和每一台主机分配一个逻辑地址. 查看IP命令: Windows: ipconfig Li ...
- 【IDEA】IDEA项目没有被SVN管理问题
解决方法 VCS-Enable Version Control Integration
- 如何正确的找BUG
什么是BUG 漏洞是在硬件.软件.协议的具体实现或系统安全策略上存在的缺陷,从而可以使攻击者能够在未授权的情况下访问或破坏系统.具体举例来说,比如在Intel Pentium芯片中存在的逻辑错误,在S ...