题目:

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. GAN 转

    生成式对抗网络(GAN)是近年来大热的深度学习模型.最近正好有空看了这方面的一些论文,跑了一个GAN的代码,于是写了这篇文章来介绍一下GAN. 本文主要分为三个部分: 介绍原始的GAN的原理 同样非常 ...

  2. I2C总线通讯协议

    I2C总线通讯协议 1. I2C总线简介 I2C是Inter-Integrated Circuit的简称,读作:I-squared-C.由飞利浦公司于1980年代提出,为了让主板.嵌入式系统或手机用以 ...

  3. Spring入门看这一篇就够了

    前言 前面已经学习了Struts2和Hibernate框架了.接下来学习的是Spring框架...本博文主要是引入Spring框架... Spring介绍 Spring诞生: 创建Spring的目的就 ...

  4. 1.4 如何在main()方法之前执行输出“hello world”

    public class Test{ static{ System.out.println("hello world"); } public static void main(St ...

  5. css边框小结

    css边框 CSS对界面的分割如上图,他们的含义如下: contend:包含HTML元素中包含的文本,图像或其他媒体.      padding:内容和边框之间的空格. 你可以想像这样的内在空间.   ...

  6. python数据类型——字典类型

    字典(dictionary) python中唯一的映射类型,采用键值对(key-value)的形式储存数据,python对key进行哈希函数运算,所以key值必须是可哈希的,可哈希表示key必须是不可 ...

  7. Apache中限制和允许特定IP访问

    Apache中限制和允许特定IP访问<Directory "/var/www">Options AllAllowOverride NoneOrder Deny,Allo ...

  8. vscode前端常用插件推荐,搭建JQuery、Vue等开发环境

    vscode是微软开发的的一款代码编辑器,就如官网上说的一样,vscode重新定义(redefined)了代码编辑器.当前市面上常用的轻型代码编辑器主要是:sublime,notepad++,edit ...

  9. 详解apply

    先谈this 关于this的指向就不细说了,网上有无数博客以及you don't kown js这类书讲的已经很详细了这里就不炒冷饭了. 举几个简单的例子 function foo() { conso ...

  10. 基于双向BiLstm神经网络的中文分词详解及源码

    基于双向BiLstm神经网络的中文分词详解及源码 基于双向BiLstm神经网络的中文分词详解及源码 1 标注序列 2 训练网络 3 Viterbi算法求解最优路径 4 keras代码讲解 最后 源代码 ...