[LeetCode] 1.Two Sum - Swift
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的更多相关文章
- LeetCode 01 Two Sum swift
class TwoSum { func sumTow(nums: [Int], target: Int)->[Int]{ ,]; ;x<nums.count;x++){ ;y<num ...
- 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 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
随机推荐
- windows phone 切换多语言时,商店标题显示错误的问题
前段时间,用业余时间写了一款 wp8 app(“超级滤镜”商店,中文地址:英文地址),在多语言的时候,给 app title 和 app tile title 进行多语言时(参考 MSDN),中文商店 ...
- oracle 使用occi方式 批量插入多条数据
if (vecInfo.empty()) { ; //数据为空,不上传,不上传标志设置为1,只有0表示上传成功 } std::string strUserName = userName; std::s ...
- FreeRtos——任务删除,改变任务优先级
以下转载自安富莱电子: http://forum.armfly.com/forum.php vTaskDelete() API 函数任务可以使用 API 函数 vTaskDelete()删除自己或其它 ...
- (七十二)自己定义通知NSNotification实现消息传递
众所周知,iOS中一般在类之间传递消息使用较多的是delegate和block,另一种是基于通知进行的消息传递,我们经常是使用系统的通知.来实现一些功能.比如利用键盘尺寸改变的通知,我们能够依据键盘的 ...
- myeclipse 上安装 Maven3<转>
环境准备: JDK 1.6 Maven 3.0.4 myeclipse 8.6.1 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Maven是 Apache 下的一个项目,目前最新版 ...
- ajax——用ajax写用户注册
zhuce.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- C#中利用JQuery实现视频网站的缩略图采集
最近有朋友想要采集优酷的视频标题和缩略图 (哈哈, 并非商业目的). 找到我帮忙, 考虑到有我刚刚发布的SpiderStudio, 我毫不犹豫的答应了. 首先在网页上视频的基本结构为: div.v - ...
- keepalive脑裂的处理,从节点发现访问的虚拟IP就报警,同时尝试发送内容到主节点服务器关闭keepalive和nginx,或者关机
解决keepalived脑裂问题 检测思路:正常情况下keepalived的VIP地址是在主节点上的,如果在从节点发现了VIP,就设置报警信息 脚本如下: 1 2 3 4 5 6 7 8 9 10 ...
- 【BZOJ】1693: [Usaco2007 Demo]Asteroids(匈牙利)
http://www.lydsy.com/JudgeOnline/problem.php?id=1693 裸匈牙利.. #include <cstdio> #include <cst ...
- html的a标签的 href 和 onclick。
主要用于给超链接添加点击事件. aa.html <html> <body> <span>this si</span> </body> < ...