一、题目要求

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. java编程--04比较几个常用的日期时间相关类的区别

    第一篇,介绍日期的比较 第二篇,介绍日期的格式化 第三篇,介绍关于日期常用的计算 第四篇,比较几个常用的日期时间相关类的区别 第五篇,jdk9对日期类进行了更新,写一些i自己的学习心得. 下面以一组思 ...

  2. (转)Shell脚本之break,continue,和exit区别

    Linux脚本中的break continue exit return break结束并退出循环 continue在循环中不执行continue下面的代码,转而进入下一轮循环 exit退出脚本,常带一 ...

  3. swing线程机制

    在介绍swing线程机制之前,先介绍一些背景概念. 背景概念 同步与异步:     同步是指程序在发起请求后开始处理事件并等待处理的结果或等待请求执行完毕,在此之前程序被阻塞(block)直到请求完成 ...

  4. POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】

    Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Sub ...

  5. Session有什么重大BUG,有什么方法可以解决

    [考点]ASP.NET中Session的多种保存方法.[出现频率]★★★☆☆[解答]使用进程内会话状态模式时,如果aspnet_wp.exe或应用程序域重新启动,则会话状态数据将丢失.可以用Sate ...

  6. Node.js资料

    http://developer.51cto.com/art/201109/290443.htmhttp://www.nodebeginner.org/index-zh-cn.html node 各种 ...

  7. 一、简单gridview列表展示

    1.HomeController public ActionResult Index() { //返回绑定models的index.cshtml return View(NorthwindDataPr ...

  8. #include stdio.h(5)

    #include <stdio.h> int main() { //1.数组的排序-冒泡排序 /* 1.规则:相邻的两个数据进行比较 2.如果有N个数据,需要选择N-1次参照物来比较, 因 ...

  9. Java Knowledge series 7

    Pepole who make a greate contribution on common libaraies deserve our respect. Component(Widget) / S ...

  10. Android 监听APP进入后台或切换到前台方案对比

    在我们开发的过程中,经常会遇到需要我们判断app进入后台,或者切换到前台的情况.比如我们想判断app切换到前台时,显示一个解锁界面,要求用户输入解锁密码才能继续进行操作:我们想判断app切换到后台,记 ...