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 ...
随机推荐
- OO第二单元电梯作业总结
目录 目录一.第一次作业分析设计策略基于度量分析程序结构二.第二次作业分析设计策略基于度量分析程序结构三.第三次作业分析设计策略基于度量分析程序结构四.分析自己程序的bug五.发现别人程序bug所采用 ...
- Node.js躬行记(13)——MySQL归档
当前我们组管理着一套审核系统,除了数据源是服务端提供的,其余后台管理都是由我们组在维护. 这个系统就是将APP中的各类社交信息送到后台,然后有专门的审核人员来判断信息是否合规,当然在送到后台之前已经让 ...
- [转]浅谈电路设计中应用DDR3处理缓存问题
本文转自:浅谈电路设计中应用DDR3处理缓存问题_若海人生的专栏-CSDN博客 DDR系列SDRAM存储芯片的高速率.高集成度和低成本使其理所当然成为存储芯片中的一霸.在PC和消费电子领域自是如此,它 ...
- git merge远程合并
当某个分支上的开发工作完成后需要将其合入主分支master 但是在提交合并前我们自己最好做一次衍合,目的是检测是否有冲突的风险,如果有应该在本分支先解决冲突然后在提交合并. 否则解决冲突的工作就全部转 ...
- 转:VCS仿真vivado IP的方法
vivado中的仿真库和模型与ISE中的是不一样的,因此在vivado中使用VCS进行仿真的方法也与ISE中不一样. VCS可以通过两种方法对XILINX的器件进行功能仿真和门级仿真,这两种方法是 P ...
- linux 安装rabbitmq
1.安装rabbitmq会依赖erlang.socat.unixodbc 下载 unixODBC-2.3.7.tar.gz ,创建路径/usr/local/unixODBC-2.3.7,解压到该路径下 ...
- Java基础语法5-运算符
运算符 基本运算符(算术.赋值.关系.逻辑等)不再赘述 位运算符 位运算符&.|.^.~.<<.>> &与 |或 ~非 ^异或 <<左移 >& ...
- 原生css实现fullPage的整屏滚动贴合
目录 1,前言 2,效果展示 3,属性说明 3.1 scroll-snap-type 3.2,scroll-snap-align 4,实际使用 4.1,兼容性 1,前言 今天摸鱼的时候,发现一个很有意 ...
- 『学了就忘』Linux基础命令 — 32、压缩和解压缩相关命令
目录 1.".zip"格式压缩 2.".gz"格式压缩 3.".bz2"格式压缩 4.".tar"格式打包 5.打包和压 ...
- 使用 SSL 加密的 JDBC 连接 SAP HANA 数据库
近期客户为满足安全要求,提了让业务应用使用 SSL 方式连接 SAP HANA 数据库的需求.本人查询 SAP官方文档 发现数据库支持 SSL 连接,有参数直接加到 JDBC 的 URL 后边就行了, ...