题目:

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. 【原】【译文】理解storm拓扑并行度

    原文地址: http://storm.apache.org/releases/1.2.1/Understanding-the-parallelism-of-a-Storm-topology.html ...

  2. 爬虫工具fiddle在firefox浏览器中的使用

    1.fiddle工作原理 浏览器与服务器之间通过建立TCP连接以HTTP协议进行通信,默认通过自己发送HTTP(或HTTPS)请求到服务器. 使用Fiddler之后,浏览器像目标服务器发送的请求都会经 ...

  3. java中垃圾回收机制和引用类型

    在java中JDK1.2版本以后,对象的引用类型分为四种,从高到低依次为:强引用.软引用.弱引用.虚引用. ①强引用的特点:垃圾回收机制绝不会回收它,即使内存不足时,JVM宁愿抛出OutOfMemor ...

  4. [BZOJ3207] 花神的嘲讽计划Ⅰ (主席树)

    Description 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程序怎么跑这么快!J要逆袭了!” …… 描述 这一天D ...

  5. Windows Developer Day - MSIX and Advanced Installer

    前面一篇我们介绍了 Adaptive Cards 的基础知识,而在 Windows Developer Day 的 Modern Application Experience 环节,还有一个需要划重点 ...

  6. jQuery中的自定义插件之----工厂方法(Factory Widget)

    jQuery赋予了我们很强大的插件自定义的功能,可以作为我们的武器库,应用到所有的网页中,使用的语法比较有意思,下面是对它的一些探讨. 遵循以下的原则: 1 IIFE 创建一个jQuery的scope ...

  7. js复制内容到剪贴板

    我们web上的复制,有时候尽管可以用鼠标选中,然后复制,但是某些时候,文字不方便选中.因此,我们自定义一个复制按钮,然后通过点击它,把想要的内容复制到剪贴板上.我归纳总结了几种方法: 1.ZeroCl ...

  8. 灵感&小思路

    1.被处理的数据量级比较大.没有明显规律可循,可以分割.或者提取文件,采用分步和保存思想去解决. 比如在使用python的正则re模块时候,根据被处理对象提取属性抽象一个基类,方法实现分步,并且把中间 ...

  9. 使用BigDecimal报的错

    错误:java.lang.arithmeticexception: non-terminating decimal expansion; no exact representa  小数位膨胀 解决方法 ...

  10. PDF加密无法做笔记

    尝试打印PDF,若无法打印,可以利用PDFescape(http://www.pdfescape.com/) PDFescape是一个可以在线修改.做笔记的网站,但是在线使用有上传PDF大小限制(小于 ...