一、题目要求

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].

二、解法

C语言

分析:采用的两次for循环进行遍历,算法复杂度O(n^2)

 /**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target) {
int* result = (int *)malloc( * sizeof(int));
for(int i = ; i < numsSize - ; i++) {
int a = nums[i];
for(int j = i + ; j < numsSize; j++ ) {
if(nums[i] + nums[j] == target) {
result[] = i;
result[] = j;
return result;
}
}
}
return result;
}

运行结果:

Python

分析:利用dict结构的查找特性

 class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
if len(nums) < 2:
return False buff_dict = {}
for i in range(len(nums)):
if nums[i] in buff_dict:
return [buff_dict[nums[i]], i]
else :
buff_dict[target - nums[i]] = i

运行结果

LeetCode(Two Sum)的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)

    Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...

  2. Leetcode之回溯法专题-39. 组合总数(Combination Sum)

    Leetcode之回溯法专题-39. 组合总数(Combination Sum) 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  3. 部分和(partial sum)在算法求解中的作用

    C++ 的 STL 库的 <numeric> 头文件的 partial_sum 函数已实现了对某一序列的 partial sum. partial_sum(first, last, des ...

  4. mysql扩展百分位函数(类似SUM)

    mysql扩展百分位函数(类似SUM) 参考:https://my.oschina.net/waterbear/blog/1186744 百度搜索:mysql percentile

  5. LeetCode 39. 组合总和(Combination Sum)

    题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限 ...

  6. C#LeetCode刷题之#39-组合总和(Combination Sum)

    目录 问题 示例 分析 问题 该文章已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3663 访问. 给定一个无重复元素的数组 candi ...

  7. 闵可夫斯基和(Mincowsky sum)

    一.概述 官方定义:两个图形A,B的闵可夫斯基和C={a+b|a∈A,b∈B}通俗一点:从原点向图形A内部的每一个点做向量,将图形B沿每个向量移动,所有的最终位置的并便是闵可夫斯基和(具有交换律) 例 ...

  8. LeetCode 刷题笔记 1. 两数之和(Two Sum)

    tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...

  9. LeetCode 112. 路径总和(Path Sum)

    题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum ...

随机推荐

  1. vue vue-resource网络请求

    在使用get/post 网络请求,需要下载插件 "vue-resource" npm install vue-resource -s 在路由要导入及注册 import Vue fr ...

  2. redis初步学习 0

    2.1 Redis是什么 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis提供了一些丰富的数据 ...

  3. hybird app 工具选型

    目前hybird app工具众多,如何选择?哪个坑少点呢? 下面来分析一下: 1开发工具都开源.基于Eclipse的有:apicloud,WeX5 2热门指数.下面的百度的搜索结果数,代表不了什么,至 ...

  4. Unity Input.GetMouseButtonDown 拿到鼠标按键

    //点击按键,生成子弹,并射向前方 void ShootBullet() { if (Input.GetMouseButtonDown(0)) { GameObject temp_Buller = G ...

  5. git使用笔记-基础篇

    git使用手册:https://git-scm.com/book/zh/v1/ 一.分支 1.查看所有本地分支 git branch 2.查看所有本地分支和远程分支 git branch -a 3.查 ...

  6. Kudu compaction design

    不多说,直接上干货! http://blog.csdn.net/lookqlp/article/details/51438109

  7. 为什么阿里云服务器的docker启动tomcat这么慢??

    https://blog.csdn.net/tianyiii/article/details/79314597 最近在阿里云服务器使用Docker启动Tomcat,发现tomcat服务器启动过程很慢. ...

  8. 设置windows 10 wifi

    1.cmd 管理员 2.执行:netsh wlan set hostednetwork mode=allow ssid=test key=123456789 3.执行:netsh wlan start ...

  9. CRLF注入攻击

      原理:http数据包通过\r\n\r\n来分开http header何http body 实现:首先这种攻击发生在应用层,且发生在服务器返回给我们的http reponse没有经过敏感字符的过滤, ...

  10. 未能解析引用的程序集......因为它对不在当前目标框架“.NETFramework,Version=v4.0,Profile=Client”中的

    解决方法:资源管理器下点击项目名(右键)属性--将.NET Framework 4 Client Profile改成.NET Framework 4 . 传送门:http://bbs.csdn.net ...