感觉有必要重新刷刷题了,为以后找工作做做准备,选择LeetCode+topcoder上的Data Science Tutorials,

争取每天晚上10:00开始刷一道,复习一下相关知识点。

-------------------------------------------------------分割线啦-----------------------------------------------------------------------

Two Sum

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

【题意】:这题意思就是给定一些数,再给你一个目标数字,让你在给定的那些数字中找出两个数,这两个数的和等于目标数字。注意给定的这些数字是无序的。

【心路历程】:看到这题,简单想了想,就知道O(n*2)的遍历一定会超时。于是进一步的思考,感觉特别像hash,可是懒得实现hash。。。

就开始想想还有没有别的方法。感觉这种无序的数字,排序后都会比较好处理一点。就开始往排序上想,发现排好序后确实比较好处理。

我们用两个下标,一个指向开始beg,一个指向末尾end,我们去比较target减去beg指向的数字,与end指向的数字。如果target-beg == end ,就找到了;

如果 target-beg > end ,就beg++;如果target-beg < end ,就end--。这样的话,时间复杂度是O(n*logn)。我感觉差不多可以接受,就交了,ac。

为了学习知识,ac不是目的,我看了一下官方解法:

O(n2) runtime, O(1) space – Brute force:

The brute force approach is simple. Loop through each element x and find if there is another value that equals to target – x.

As finding another value requires looping through the rest of array, its runtime complexity is O(n2).

O(n) runtime, O(n) space – Hash table:

We could reduce the runtime complexity of looking up a value to O(1) using a hash map that maps a value to its index.

- -,最佳答案就是Hash,O(n)。

---------------------------------------------------------又是分割线啦----------------------------------------------------------------------

附上代码:

(1)这个是O(n*logn)的排序+首尾下标:

 /**
* Note: The returned array must be malloced, assume caller calls free().
*/
struct node {
int num;
int local;
}; int cmp(const void * a,const void *b) {
struct node * aa = (struct node *)a;
struct node * bb = (struct node *)b;
if(aa->num == bb->num) {
return aa->local - bb->local;
}else{
return aa->num - bb->num;
}
}
int* twoSum(int* nums, int numsSize, int target) { struct node* a = (struct node*)malloc(numsSize*(sizeof(struct node)));
int * vis = (int *)malloc(*sizeof(int));
int beg = ,end = numsSize-,i;
for(i = ; i < numsSize; i++) {
a[i].num = nums[i];
a[i].local = i+;
}
qsort(a,numsSize,sizeof(a[]),cmp);
for(; beg < end;){
int ans = target - a[beg].num;
while(){
if(a[end].num == ans){
if(a[beg].local < a[end].local){
vis[] = a[beg].local;
vis[] = a[end].local;
}else {
vis[] = a[end].local;
vis[] = a[beg].local;
}
return vis;
}else if(a[end].num < ans){
beg++;
break;
}else {
end--;
}
}
}
free(a);
}

(2)用c++提供的map模拟hash:

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,int>a;
int i,len;
len = nums.size();
for(i = ; i < len; i++) {
int ans = target - nums[i];
if(a.count(nums[i])){
vector<int> res;
int ans1,ans2;
ans1 = i+;
ans2 = a[nums[i]];
if(ans1 > ans2) {
res.push_back(ans2);
res.push_back(ans1);
return res;
}else {
res.push_back(ans1);
res.push_back(ans2);
return res;
}
}
a[ans] = i+;
}
}
};

一起刷LeetCode1-Two Sum的更多相关文章

  1. leetcode-1 Two Sum 找到数组中两数字和为指定和

     问题描写叙述:在一个数组(无序)中高速找出两个数字,使得两个数字之和等于一个给定的值.如果数组中肯定存在至少一组满足要求. <剑指Offer>P214(有序数组) <编程之美& ...

  2. LeetCode1 Two Sum

    题目 :Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  3. leetcode第一刷_Minimum Path Sum

    能够用递归简洁的写出,可是会超时. dp嘛.这个问题须要从后往前算,最右下角的小规模是已知的,边界也非常明显,是最后一行和最后一列,行走方向的限制决定了这些位置的走法是唯一的,能够先算出来.然后不断的 ...

  4. Leetcode--1. Two Sum(easy)

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

  5. 129. Sum Root to Leaf Numbers pathsum路径求和

    [抄题]: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a ...

  6. Add Strings大整数加法十进制求和 & Add Binary二进制求和

    [抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...

  7. 1001 Sum Problem [ACM刷题]

    这一段时间一直都在刷OJ,这里建一个博客合集,用以记录和分享算法学习的进程. github传送门:https://github.com/haoyuanliu/Online_Judge/tree/mas ...

  8. 周刷题第一期总结(two sum and two numbers)

    由于深深的知道自己是事件驱动型的人,一直想补强自己的薄弱环节算法,却完全不知道从哪里入手.所以只能采用最笨的办法,刷题.从刷题中遇到问题就解决问题,最后可能多多少少也能提高一下自己的渣算法吧. 暂时的 ...

  9. LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

    1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...

随机推荐

  1. Visual StudioTools for Unity 使用技巧2

    在之前的博客介绍了 Visual Studio Tools for Unity的安装和使用. http://www.cnblogs.com/petto/p/3886811.html 其实这个工具还提供 ...

  2. Orcle数据库查询练习复习:二

    一.题目 1.找出所有成绩均低于80的学生姓名 select sname from student where sid in( ) select sname from student where si ...

  3. android MD5

    public static String MD5(String str) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstan ...

  4. android移植

    root@phone-desktop:/opt/4418-source/android4.4.2_r1# ./device/nexell/tools/build.sh -b drone2 -t u-b ...

  5. mac下git中文乱码

    今天从window切mac,git使用时各种问题.典型的就是,git commit 可以使用中文注释,但是使用 git log 查看的时候发现都是乱码,乱码效果如下: <B1><E0 ...

  6. 购买使用Linode VPS必须知晓的十个问题

    Linode是国外非常著名的VPS商之一,目前在国内站长圈中备受推崇.有许多站长已经购买了Linode VPS,但是部分站长由于中英语言不通,对Linode的政策不了解,从而造成了许多不必要的损失.本 ...

  7. Codeforces Beta Round #2B(dp+数学)

    贡献了一列WA.. 数学很神奇啊 这个题的关键是怎么才能算尾0的个数 只能相乘 可以想一下所有一位数相乘 除0之外,只有2和5相乘才能得到0 当然那些本身带0的多位数 里面肯定含有多少尾0 就含有多少 ...

  8. Qt之自定义界面(添加自定义标题栏)

    简述 通过上节内容,我们实现了自定义窗体的移动,但是我们缺少一个标题栏来显示窗体的图标.标题,以及控制窗体最小化.最大化.关闭的按钮. 自定义标题栏后,所有的控件我们都可以定制,比如:在标题栏中添加换 ...

  9. 数据库锁机制(一)——概述

    注:内容为自己的推理认知+网络,如有错误和不合理之处,敬请指出. 在多线程环境中我用使用线程锁处理并发问题,而在数据库系统中,并发问题可以细化到事务级别,而DBMS对此的处理方案就是使用锁. 为了适应 ...

  10. Android ArrayAdapter 详解

    本文主要讲解ArrayAdapter的创建方法,我把ArrayAdapter分为三种:简单的.样式丰富的但内容简单的.内容丰富的. 默认的,ArrayAdapter期望接受的样式文件里只含有一个tex ...