题目:

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

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

C++解答一(传统方法,Runtime: 19 ms):

 struct Node
{
int num;
int pos;
}; bool cmp(Node a,Node b)
{
return a.num<b.num;
} class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) { vector<int> result;
vector<Node> tmp;
Node a; for(int i = ;i<numbers.size();i++)
{
a.pos = i+;
a.num = numbers[i];
tmp.push_back(a);
} sort(tmp.begin(),tmp.end(),cmp);
int j = tmp.size()-;
int tmpvalue = ; for(int i = ;i<tmp.size();)
{
tmpvalue = tmp[i].num+tmp[j].num;
if(tmpvalue==target)
{
if(tmp[i].pos<tmp[j].pos)
{
result.push_back(tmp[i].pos);
result.push_back(tmp[j].pos);
}
else
{
result.push_back(tmp[j].pos);
result.push_back(tmp[i].pos);
} break;
}
else if(tmpvalue>target)
{
j--;
}
else
{
i++;
}
} return result;
}
};

C++ 解答二(巧妙方法,Runtime: 35 ms):

 class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
int i, sum;
vector<int> results;
map<int, int> hmap;
for(i=; i<numbers.size(); i++){
if(!hmap.count(numbers[i])){
hmap.insert(pair<int, int>(numbers[i], i));
}
if(hmap.count(target-numbers[i])){
int n=hmap[target-numbers[i]];
if(n<i){
results.push_back(n+);
results.push_back(i+);
return results;
} }
}
return results;
}
};

LeetCode 之 TwoSum的更多相关文章

  1. LeetCode #1 TwoSum

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

  2. Leetcode 1——twosum

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

  3. leetcode之twosum

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

  4. leetcode ----ARRAY TWOSUM

    代码的(判断nums[i]或者是target-nums[i]都可以):

  5. [LeetCode_1] twoSum

    LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...

  6. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  7. ANDROID学习书单

    Skip to content PersonalOpen sourceBusinessExplore Sign upSign in PricingBlogSupport   This reposito ...

  8. Android技能树

    第一部分:Android(安卓)Android基础知识Android内存泄漏总结Handler内存泄漏分析及解决Android性能优化ListView详解RecyclerView和ListView的异 ...

  9. LeetCode初体验—twoSum

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

随机推荐

  1. pc端和移动端的区别

    以下都是自己的个人理解,说错了希望大家多交流交流.1,普通pc端开发与移动端开发区别.普通pc端开发,我理解就是你拿电脑打开的网页都算[这相信大部分人都知道].那么移动端开发工程师,说白了就很好理解了 ...

  2. Android自定义上拉控件SpringView

    Demo 先看一下SpringView的效果图: 1.拖动灰色部分可拖动下方视图,点击jump按钮可让下方视图自行滑动. 使用方法 布局文件: <com.zql.android.springvi ...

  3. 决策树ID3算法python实现 -- 《机器学习实战》

    from math import log import numpy as np import matplotlib.pyplot as plt import operator #计算给定数据集的香农熵 ...

  4. 你不知道的Javascript(上卷)读书笔记之三 ---- 函数作用域与块作用域

    1. 函数中的作用域 函数作用域的含义是指属于这个函数的全部变量都可以在整个函数范围内使用以及复用 2. 隐藏内部实现 函数经常使用于隐藏”内部实现”,可以把变量和函数包裹在一个函数的作用域中,然后用 ...

  5. 多个StoryBoard之间的跳转

    iOS项目中可以将同一业务流程的页面归置到一个StoryBoard中,项目中必然会包含多个StroryBoard,可以利用跳转,实现项目的不同业务流程页面间的跳转切换. 实现思路: 1,项目(Proj ...

  6. CListCtrl消息及解释

    对于CListCtrl消息的解释:[来自网络]LVN_BEGINDRAG 鼠标左键正在被触发以便进行拖放操作(当鼠标左键开始拖拽列表视图控件中的项目时产生) LVN_BEGINRDRAG 鼠标右键正在 ...

  7. iOS核心动画详解(一)

    前言 这篇文章主要是针对核心动画(Core Animation)的讲解,不涉及UIView的动画.因为内容较多,这篇文章会分为几个章节来进行介绍.本文主要是介绍核心动画的几个类之间的关系和CAAnim ...

  8. IO流入门-第一章-FileInputStream

    FileInputStreamj基本用法和方法示例 import java.io.*; public class FileInputStreamTest01 { public static void ...

  9. 最全的Eclipse使用快捷键

    Eclipse 是一种基于 Java 的可扩展开源开发平台.尽管 Eclipse 是使用 Java 语言开发的,但它的用途并不限于 Java 语言,Eclipse 还包括插件开发环境等,下面将为大家介 ...

  10. thinkphp使用阿里云OSS最新SDK,文件部署

    这文章是建立在你已经注册号阿里云的OSS,和创建好Bucket前提下: 其实阿里云的帮助与文档写的很详细,这里只说一下源码方式 1.phpsdk下载地址(摘自阿里云OSS的帮助与文档)(也有我自己下载 ...