题目:

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. 异常-----freemarker.core.ParseException: Token manager error

    一,案例一 1.1.错误描述 freemarker.core.ParseException: Token manager error: freemarker.core.TokenMgrError: L ...

  2. Centos7.2 搭建Lamp服务器以及迁移WordPress个人博客详细过程

    其实自己的博客搭了有段时间了,但是由于自己不太确定是不是一定要用wd的框架,以及实验室公网服务器的不稳定,就一直荒废着. 今天偶然间看到了腾讯云对于学生的优惠活动,毕业之前每月只要8元的云服务器(就算 ...

  3. js拖拽分析

    js拖拽分析 思路 1.三个鼠标事件,mousedown,mousemove,mouseup 2.可移动性absolute 3.边界限制 得到鼠标点击处和div边界的距离,然后得出top 和 left ...

  4. mybatis快速入门(七)-spring-mybatis整合

    今天写写spring-mybatis整合吧,先写个原始dao类的整合,下一节在写个动态代理的整合,我就不写太详细了,因为前面的章节基本上都有了,我直接就一口气都写出来需要那些文件然后在直接贴代码,首先 ...

  5. POJ 2187 Beauty Contest(凸包,旋转卡壳)

    题面 Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the ...

  6. datatable 参数详细说明

    页面参数说明 var docrTable = $('#docrevisontable').dataTable({ language:lang, //提示信息 autoWidth: false, //禁 ...

  7. 【最新】Power BI混合现实应用Mixed Reality app预览版正式发布

    1.介绍 2018年3月13日,Power BI在官方博客和Docs文档发布了Power BI for Mixed Reality应用预览版的消息, 也就是可以以后在更虚拟的世界中来观察你的报表,想象 ...

  8. Spring9:Autowire(自动装配)机制

    为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: public interface Animal { public void eat(); } ...

  9. FPGA加三移位算法:硬件逻辑实现二进制转BCD码

    本文设计方式采用明德扬至简设计法.利用FPGA来完成显示功能不是个很理想的方式,当显示任务比较复杂,要通过各种算法显示波形或者特定图形时,当然要用单片机通过C语言完成这类流程控制复杂,又对时序要求不高 ...

  10. 基于jenkins的go语言项目自动化发布遇到的坑

    之前我们研究dep,就是为了有一天可以实现go语言项目在我们系统里的CI. 之前联物科技的项目主要是使用java作为后端开发语言,基于jenkins的自动发布,使用了pipeline编写脚本,从拉取代 ...