题目:

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

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

题目大意:

给你一个数组nums和一个数target,找出数组里面两个相加等于target的数并返回其下标。比如数组nums为[2,7,11,15],target=9,因为2+7等于9,2的下标为0,7的下标为1,所以返回0和1.

解法:

方法一:

最容易想到的方法就是遍历整个数组,嵌套循环,先取出nums里面的一个数然后循环分别加加看后面的数,看是否等于target,这种方法的时间复杂度为o(n2)。代码如下:

int* twoSum(int* nums, int numsSize, int target) {
    );
    ;i<numsSize;++i)
        ;j<numsSize;++j)
            if(nums[i]+nums[j]==target){
                ans[]=i;
                ans[]=j;
                return ans;
            }
    return ans;
}

方法二:

原本应该是属于数据结构哈希表的方法,但是我还不会写哈希表,所以就利用了数组下标(和哈希表原理一样,但是这样会造成大量空间浪费)。我们从数组里面第一个数开始看,拿到一个数就看一下哈希表内target-当前数字的那个数是否存在。这个方法的时间复杂度为O(n)。代码如下:

int* twoSum(int* nums,int numsSize,int target){
    *sizeof(int));
    memset(fuck,-,*sizeof(int));
    *sizeof(int));
    ;i<numsSize;i++){
            ]==-)
                fuck[nums[i]+]=i;
            else{
                ans[]=fuck[target-nums[i]+];
                ans[]=i;
                free(fuck);
                return ans;
            }
    }
    return ans;
}

说明:Fuck存储了哈希表,对于不存在的元素就标记其下标,因为下标不能为负数,所以我以40000为0.

我写的代码还有缺陷,就是如果给的数组里面如果有-1,这个代码就出错了,也就是说我刚好避开了测试数据。运气好。

从0开始的LeetCode生活—001-Two Sum的更多相关文章

  1. 从0开始的LeetCode生活—9. Palindrome Number(回文数)

    题目大意: 判断输入的数字是不是回文数.所谓回文数就是正反读都一样的数字,比如说11,121,1221这样子的数字.负数不会是回文数. 解题思路: 思路一:如果这个数是负数,则返回false,否则用一 ...

  2. 从0开始的LeetCode生活—461-Hamming Distance(汉明距离)

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  3. 【LeetCode】001. Two Sum

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

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

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

  5. [从 0 开始的 Angular 生活]No.38 实现一个 Angular Router 切换组件页面(一)

    前言 今天是进入公司的第三天,为了能尽快投入项目与成为团队可用的战力,我正在努力啃官方文档学习 Angular 的知识,所以这一篇文章主要是记录我如何阅读官方文档后,实现这个非常基本的.带导航的网页应 ...

  6. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  7. leetcode 练习1 two sum

    leetcode 练习1  two sum whowhoha@outlook.com 问题描述 Given an array of integers, return indices of the tw ...

  8. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. 【一天一道LeetCode】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

随机推荐

  1. 复制粘贴之不带插件的jquery

    一.实现点击按钮,复制文本框中的的内容 <script type="text/javascript"> function copyUrl2() { var Url2=d ...

  2. [ZOJ3435]Ideal Puzzle Bobble

    题面戳我 题意:你现在处于\((1,1,1)\),问可以看见多少个第一卦限的整点. 第一卦限:就是\((x,y,z)\)中\(x,y,z\)均为正 sol 首先L--,W--,H--,然后答案就变成了 ...

  3. wso2ESB - 在eclipse中启用调试模式

    最近在使用wso2ESB,记录一下使用过程中碰到的坑,先写一篇调试的(前面的工具安装就不介绍了,既然想用调试了说明你已经看过一部分文档了),以后可能会介绍其他功能的使用. 在wso2 ei的文档中,介 ...

  4. Redis主从配置及HA方案

    首先说下主从同步Replication的原理 在Slave启动并连接到Master之后,它将主动发送一条SYNC命令.此后Master将启动后台存盘进程,同时收集所有接收到的用于修改数据集的命令,在后 ...

  5. 小米google play service停止工作解决办法,不root,不刷第三方recovery(也适用于其他的手机)

    问题: 原因是手机安卓系统是6.0.系统应用里面没有包含谷歌框架等一系列谷歌的小东西. 参考: http://www.miui.com/thread-3548436-1-1.html http://w ...

  6. xml 加载多个properties文件

    xml 配置项: <bean id="propertyConfigurer" class="com.boc.icms.archive.util.ExProperty ...

  7. js 数组 remove

    在写js代码时候,有时需要移除数组的元素,在js数组中没有remove 方法, 不过有splice 方法同样可以用于移除数组元素:(http://www.w3school.com.cn/jsref/j ...

  8. 网站压力测试ab 命令

    网站压力测试ab 命令 author: headsen   chen         2017-10-25   10:06:35 个人原创,转载请注明作者,出处,否则依法追究法律责任! 1,制作一个a ...

  9. Ameba读写分离_mycat分库分表_redis缓存

    1 数据库的读写分离 1.1 Amoeba实现读写分离 1.1.1 定义 Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy 优点: 配置读写分离时较为简单.配置 ...

  10. shiro(二)自定义realm,模拟数据库查询验证

    自定义一个realm类,实现realm接口 package com; import org.apache.shiro.authc.*; import org.apache.shiro.realm.Re ...