1. Two Sum

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

===========大致题目意思================

1.两者求和

有一个整型数组,返回结果依然是数组,元素是给定数组的两个下标,这两个下标对应的元素相加等于一个特定的目标数值

思路一:

1. 需要进行两层for循环;

2. 第二层for循环下标应从第一个for循环下标+1开始,防止重复相加,及自己和自己相加;

3. 条件语句应该是两个元素相加等于目标数字时,获得这两个元素对应的下标放入数组,并返回

实现代码如下:

class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var resultNums:[Int] = [0, 0] for i in 0 ..< nums.count {
for j in i+1 ..< nums.count {
if nums[i]+nums[j] == target {
resultNums[0] = i
resultNums[1] = j return resultNums
}
}
} return resultNums
}
}

两层for循环好理解但是效率太低,可以使用一层for循环就可以实现 ,运行效率大大提高:

思路二:

1. 定义一个字典,key放数组的元素值,value放下标;

2. 一层for循环记录一个下标;

3. 判断字典中指定目标数减去当前下标对应的元素值的差值为字典的key,看对应的这个key存不存在value

3.1 若不存在,则将这个元素为key的字典设置value值为当前下标;

3.2 若存在,则将这个差值为下标对应的字典value值追加到结果数组中,在追加当前下标,再返回目标数据

实现代码如下:

class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var resultNums:[Int] = []
var numberDic:[Int : Int] = [:] for i in 0 ..< nums.count {
if numberDic[target-nums[i]] != nil {
resultNums.append(numberDic[target - nums[i]]!)
resultNums.append(i) return resultNums
} else {
numberDic[nums[i]] = i
}
} return resultNums
}
}

PS:题目应该注意的点:返回的是下标,而不是下标对应的元素

[LeetCode] 1.Two Sum - Swift的更多相关文章

  1. LeetCode 01 Two Sum swift

    class TwoSum { func sumTow(nums: [Int], target: Int)->[Int]{ ,]; ;x<nums.count;x++){ ;y<num ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  5. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  6. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  7. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  8. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  9. LeetCode one Two Sum

    LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...

随机推荐

  1. java中null和""的区别

    问题一: null和""的区别 String s=null; s.trim()就会抛出为空的exception String s=""; s.trim()就不会 ...

  2. Getting Started with Amazon EC2 (1 year free AWS VPS web hosting)

    from: http://blog.coolaj86.com/articles/getting-started-with-amazon-ec2-1-year-free-aws-vps-web-host ...

  3. array2json

    原文:jQuery方法扩展:type, toJSON, evalJSON. http://zhkac.iteye.com/blog/499330 .2013-05-19 (function($) { ...

  4. .NET Standard - 揭秘 .NET Core 和 .NET Standard[转自MSDN]

    作为 .NET 系列的最新成员,.NET Core 和 .NET Standard 的概念及其与 .NET Framework 的区别并不十分明确.在本文中,我将准确介绍每个产品及其适用场景. 在详细 ...

  5. C语言 · 图形输出

    算法提高 图形输出   时间限制:1.0s   内存限制:512.0MB      编写一程序,在屏幕上输出如下内容: X | X | X ---+---+--- | | ---+---+--- O ...

  6. mysql数据库 详解

    一.学习目录 1.认识数据库和mysql 2.mysql连接 3.入门语句 4.详解列类型 5.增删改查 INSERT INTO  表名(列1,……  列n)  VALUES(值 1,……  值 n) ...

  7. postgresql data数据目录路径迁移

    默认的数据库路径是/var/lib/pgsql/9.x/data 将现有的数据库文件全部拷贝到新的数据库路径下,然后重启 新建一个路径作为新的数据库数据路径,假如是/home/data sudo mk ...

  8. 2015 Multi-University Training Contest 5 1009 MZL's Border

    MZL's Border Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5351 Mean: 给出一个类似斐波那契数列的字符串序列 ...

  9. imx lcd HV和DE模式转换

    有些时候拿到的lcd手册中关于芯片的时序使用的DE模式的,而imx6内核中使用的参数设置趋势HV模式,应此就需要将DE模式的参数转化为HV模式. 参考链接: https://community.nxp ...

  10. Canvas组件:画布,可以实现动画操作

    Canvas组件:画布,可以实现动画操作. TextArea:文本域. 在单行文本域中回车会激发ActionEvent. 用CheckBoxGroup实现单选框功能. Java中,单选框和复选框都是使 ...