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. deb包的2种安装安装方法

    一.cydia重启自动安装:用ifunbox进入//var/root/Media/Cydia/AutoInstallCydia/AutoInstall 需要分别单独建立,注意大小写.然后把你要安装的d ...

  2. js eval深入

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 使用eval函数来解析,并且使用jquery的ea ...

  3. Python异常处理try...except...finally raise assert

    异常处理:try ...except  try代码块放置容易发生异常的语句:except代码块放置处理异常的语句try ...except...finally finally代码快是任何时候都会执行的 ...

  4. python学习笔记(10)--爬虫下载煎蛋图片

    说明: 1. 有很多细节需要注意! 2. str是保留字,不要作为变量名 3. 保存为txt报错,encoding=utf-8 4. 403错误,添加headers的方法 5. 正则match只能从开 ...

  5. 上传绕过WAF几种常见的姿势

    1:WTS-WAF 绕过上传原内容:Content-Disposition: form-data; name="up_picture"; filename="xss.ph ...

  6. 最新phpcms v9.6.0 sql注入漏洞分析

    昨天爆出来的,但其实在此之前就i记得在某群看见有大牛在群里装逼了.一直也没肯告诉.现在爆出来了.就来分析一下.官方现在也还没给出修复.该文不给出任何利用的EXP. 该文只做安全研究,不做任何恶意攻击! ...

  7. Extjs,实现树形结构的总结

    工作总结,用extjs.mybatis.springMVC实现树形显示班级 前台extjs实现树形代码如下: xtype : 'combotree', fieldLabel : '部门名称', nam ...

  8. call_usermodehelper内核中运行用户应用程序

    init是用户空间第一个程序,在调用init前程序都运行在内核态,之后运行init时程序运行到用户态. 操作系统上,一些内核线程在内核态运行,它们永远不会进入用户态.它们也根本没有用户态的内存空间.它 ...

  9. EF常用查询语句

    //方法一 Linq to Entities            var info = from p in entity.Users where p.ID >= 10 orderby p.ID ...

  10. Android应用双击返回键退出

    @Override public void onBackPressed() { // TODO 退出提示 if (System.currentTimeMillis() - mExitTime > ...