Skip to content

Commit c6ed9a3

Browse files
committed
update(2021-04-08 17:23)
1 parent e4f591a commit c6ed9a3

File tree

72 files changed

+229
-206
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+229
-206
lines changed

_posts/2021-04-08-swift-leetcode.md

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: LeetCode - 两数之和
3+
date: 2021-04-08 12:00:00 +0800
4+
tags: [Swift, LeetCode, 算法, 数据结构, 数组]
5+
categories: [代码人生, LeetCode]
6+
---
7+
8+
题目链接:[两数之和](https://leetcode-cn.com/problems/two-sum/)
9+
10+
## 主要思路
11+
12+
遍历数组,并且使用 map 存储 target - nums[i] 的值
13+
14+
## 代码实现
15+
16+
```swift
17+
/**
18+
* 时间复杂度: O(n), 空间复杂度: O(n)
19+
*/
20+
class Solution {
21+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
22+
var dict = [Int: Int]()
23+
for (i, num) in nums.enumerated() {
24+
if let lastIndex = dict[target - num] {
25+
return [lastIndex, i]
26+
}
27+
dict[num] = i
28+
}
29+
fatalError("No valid outputs")
30+
}
31+
}
32+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: LeetCode - 删除有序数组中的重复项
3+
date: 2021-04-08 12:00:00 +0800
4+
tags: [Swift, LeetCode, 算法, 数据结构, 数组]
5+
categories: [代码人生, LeetCode]
6+
---
7+
8+
题目链接:[删除有序数组中的重复项](https://leetcode.com/problems/remove-duplicates-from-sorted-array)
9+
10+
## 主要思路
11+
12+
定义一个索引index, 遍历数组过程中与该索引的值进行对比,如果不一致,则修改对应索引的值
13+
14+
## 代码实现
15+
16+
```swift
17+
/**
18+
* 时间复杂度: O(n), 空间复杂度: O(1)
19+
*/
20+
class Solution {
21+
func removeDuplicates(_ nums: inout [Int]) -> Int {
22+
guard nums.count > 0 else {
23+
return 0
24+
}
25+
var index = 0
26+
for num in nums where num != nums[index] {
27+
index += 1
28+
nums[index] = num
29+
}
30+
return index + 1
31+
}
32+
}
33+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: LeetCode-Swift 刷题笔记
3+
date: 2021-04-08 12:00:00 +0800
4+
tags: [Swift, LeetCode, 算法, 数据结构]
5+
categories: [代码人生, LeetCode]
6+
pin: true
7+
---
8+
9+
[LeetCode](https://leetcode-cn.com) 刷题笔记,持续更新ing...
10+
11+
<!-- more -->
12+
13+
| 序号 | 标题 | 难度 | 时间复杂度 | 空间复杂度 | 题解 |
14+
|:---:|:---|:---:|:---:|:---:|:---:|
15+
| 1 | [两数之和](https://leetcode-cn.com/problems/two-sum/) | 简单 | O(n) | O(n) | [Swift](/posts/swift-leetcode-0001/) |
16+
| 26 | [删除有序数组中的重复项](https://leetcode.com/problems/remove-duplicates-from-sorted-array) | 简单 | O(n) | O(1) | [Swift](/posts/swift-leetcode-0026/) |

_site/404.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/about/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/archives/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/assets/js/data/search.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/categories/flutter/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/categories/git/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/categories/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/categories/ios/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/categories/leetcode/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/categories/代码人生/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/categories/小程序/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/categories/设计模式/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/feed.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<feed xmlns="http://www.w3.org/2005/Atom"> <id>https://kokohuang.github.io/</id><title>kokohuang's blog</title><subtitle>kokohuang's blog.</subtitle> <updated>2021-04-08T17:14:17+08:00</updated> <author> <name>kokohuang</name> <uri>https://kokohuang.github.io/</uri> </author><link rel="self" type="application/atom+xml" href="https://kokohuang.github.io/feed.xml"/><link rel="alternate" type="text/html" hreflang="zh-CN" href="https://kokohuang.github.io/"/> <generator uri="https://jekyllrb.com/" version="4.2.0">Jekyll</generator> <rights> © 2021 kokohuang </rights> <icon>/assets/img/favicons/favicon.ico</icon> <logo>/assets/img/favicons/favicon-96x96.png</logo> <entry><title>LeetCode-Swift 刷题笔记</title><link href="https://kokohuang.github.io/posts/swift-leetcode/" rel="alternate" type="text/html" title="LeetCode-Swift 刷题笔记" /><published>2021-04-08T12:00:00+08:00</published> <updated>2021-04-08T16:55:43+08:00</updated> <id>https://kokohuang.github.io/posts/swift-leetcode/</id> <content src="https://kokohuang.github.io/posts/swift-leetcode/" /> <author> <name>kokohuang</name> </author> <category term="代码人生" /> <category term="LeetCode" /> <summary> LeetCode 刷题笔记,持续更新ing… 序号 标题 难度 时间复杂度 空间复杂度 题解 1 两数之和 简单 O(n) O(n) Swift 26 删除有序数组中的重复项 简单 O(n) O(1) Swift 两数之和 /** * 主要思路: * 遍历数组,并且使用 map 存储 target - nums[i] 的值 * * 时间复杂度: O(n), 空间复杂度: O(n) */ class Solution { func twoSum(_ n... </summary> </entry> <entry><title>Swift设计模式概览</title><link href="https://kokohuang.github.io/posts/swift-design-patterns/" rel="alternate" type="text/html" title="Swift设计模式概览" /><published>2020-05-01T12:00:00+08:00</published> <updated>2020-05-01T12:00:00+08:00</updated> <id>https://kokohuang.github.io/posts/swift-design-patterns/</id> <content src="https://kokohuang.github.io/posts/swift-design-patterns/" /> <author> <name>kokohuang</name> </author> <category term="代码人生" /> <category term="设计模式" /> <summary> 创建型模式 工厂方法模式:在父类中提供一个创建对象的方法,允许子类决定实例化对象的类型。 抽象工厂模式:让你能创建一系列相关的对象,而无需指定其具体类。 生成器模式:使你能够分步骤创建复杂对象。该模式允许你使用相同的创建代码生成不同类型和形式的对象。 原型模式:使你能够复制已有对象,而又无需使代码依赖它们所属的类。 单例模式:让你能够保证一个类只有一个实例,并提供一个访问该实例的全局节点。 结构型模式 适配器模式:使接口不兼容的对象能够相互合作。 桥接模式:可将一个大类或一系列紧密相关的类拆分为抽象和实现两个独立的层次结构,从而能在开发时分别使用。 组合模式:可以使用它将对象组合成树状结构,并且能像使用独立对象一样使用它... </summary> </entry> <entry><title>Swift设计模式之「模板方法模式」</title><link href="https://kokohuang.github.io/posts/swift-design-patterns-template-method/" rel="alternate" type="text/html" title="Swift设计模式之「模板方法模式」" /><published>2020-04-22T12:00:00+08:00</published> <updated>2020-04-22T12:00:00+08:00</updated> <id>https://kokohuang.github.io/posts/swift-design-patterns-template-method/</id> <content src="https://kokohuang.github.io/posts/swift-design-patterns-template-method/" /> <author> <name>kokohuang</name> </author> <category term="代码人生" /> <category term="设计模式" /> <summary> 在父类中提供一个创建对象的方法,允许子类决定实例化对象的类型。 </summary> </entry> <entry><title>Swift设计模式之「策略模式」</title><link href="https://kokohuang.github.io/posts/swift-design-patterns-strategy/" rel="alternate" type="text/html" title="Swift设计模式之「策略模式」" /><published>2020-04-21T12:00:00+08:00</published> <updated>2020-04-21T12:00:00+08:00</updated> <id>https://kokohuang.github.io/posts/swift-design-patterns-strategy/</id> <content src="https://kokohuang.github.io/posts/swift-design-patterns-strategy/" /> <author> <name>kokohuang</name> </author> <category term="代码人生" /> <category term="设计模式" /> <summary> 在父类中提供一个创建对象的方法,允许子类决定实例化对象的类型。 </summary> </entry> <entry><title>Swift设计模式之「状态模式」</title><link href="https://kokohuang.github.io/posts/swift-design-patterns-state/" rel="alternate" type="text/html" title="Swift设计模式之「状态模式」" /><published>2020-04-20T12:00:00+08:00</published> <updated>2020-04-20T12:00:00+08:00</updated> <id>https://kokohuang.github.io/posts/swift-design-patterns-state/</id> <content src="https://kokohuang.github.io/posts/swift-design-patterns-state/" /> <author> <name>kokohuang</name> </author> <category term="代码人生" /> <category term="设计模式" /> <summary> 在父类中提供一个创建对象的方法,允许子类决定实例化对象的类型。 </summary> </entry> </feed>
1+
<feed xmlns="http://www.w3.org/2005/Atom"> <id>https://kokohuang.github.io/</id><title>kokohuang's blog</title><subtitle>kokohuang's blog.</subtitle> <updated>2021-04-08T17:23:18+08:00</updated> <author> <name>kokohuang</name> <uri>https://kokohuang.github.io/</uri> </author><link rel="self" type="application/atom+xml" href="https://kokohuang.github.io/feed.xml"/><link rel="alternate" type="text/html" hreflang="zh-CN" href="https://kokohuang.github.io/"/> <generator uri="https://jekyllrb.com/" version="4.2.0">Jekyll</generator> <rights> © 2021 kokohuang </rights> <icon>/assets/img/favicons/favicon.ico</icon> <logo>/assets/img/favicons/favicon-96x96.png</logo> <entry><title>LeetCode-Swift 刷题笔记</title><link href="https://kokohuang.github.io/posts/swift-leetcode/" rel="alternate" type="text/html" title="LeetCode-Swift 刷题笔记" /><published>2021-04-08T12:00:00+08:00</published> <updated>2021-04-08T16:36:54+08:00</updated> <id>https://kokohuang.github.io/posts/swift-leetcode/</id> <content src="https://kokohuang.github.io/posts/swift-leetcode/" /> <author> <name>kokohuang</name> </author> <category term="代码人生" /> <category term="LeetCode" /> <summary> LeetCode 刷题笔记,持续更新ing… 序号 标题 难度 时间复杂度 空间复杂度 题解 1 两数之和 简单 O(n) O(n) Swift 26 删除有序数组中的重复项 简单 O(n) O(1) Swift </summary> </entry> <entry><title>LeetCode - 删除有序数组中的重复项</title><link href="https://kokohuang.github.io/posts/swift-leetcode-0026/" rel="alternate" type="text/html" title="LeetCode - 删除有序数组中的重复项" /><published>2021-04-08T12:00:00+08:00</published> <updated>2021-04-08T16:36:54+08:00</updated> <id>https://kokohuang.github.io/posts/swift-leetcode-0026/</id> <content src="https://kokohuang.github.io/posts/swift-leetcode-0026/" /> <author> <name>kokohuang</name> </author> <category term="代码人生" /> <category term="LeetCode" /> <summary> 题目链接:删除有序数组中的重复项 主要思路 定义一个索引index, 遍历数组过程中与该索引的值进行对比,如果不一致,则修改对应索引的值 代码实现 /** * 时间复杂度: O(n), 空间复杂度: O(1) */ class Solution { func removeDuplicates(_ nums: inout [Int]) -&amp;gt; Int { guard nums.count &amp;gt; 0 else { return 0 } var index = 0 for num in nums where num != nums[index] { index += 1 nums[index] = num } ... </summary> </entry> <entry><title>LeetCode - 两数之和</title><link href="https://kokohuang.github.io/posts/swift-leetcode-0001/" rel="alternate" type="text/html" title="LeetCode - 两数之和" /><published>2021-04-08T12:00:00+08:00</published> <updated>2021-04-08T12:00:00+08:00</updated> <id>https://kokohuang.github.io/posts/swift-leetcode-0001/</id> <content src="https://kokohuang.github.io/posts/swift-leetcode-0001/" /> <author> <name>kokohuang</name> </author> <category term="代码人生" /> <category term="LeetCode" /> <summary> 题目链接:两数之和 主要思路 遍历数组,并且使用 map 存储 target - nums[i] 的值 代码实现 /** * 时间复杂度: O(n), 空间复杂度: O(n) */ class Solution { func twoSum(_ nums: [Int], _ target: Int) -&amp;gt; [Int] { var dict = [Int: Int]() for (i, num) in nums.enumerated() { if let lastIndex = dict[target - num] { return [lastIndex, i] } dict[num] = i } ... </summary> </entry> <entry><title>Swift设计模式概览</title><link href="https://kokohuang.github.io/posts/swift-design-patterns/" rel="alternate" type="text/html" title="Swift设计模式概览" /><published>2020-05-01T12:00:00+08:00</published> <updated>2020-05-01T12:00:00+08:00</updated> <id>https://kokohuang.github.io/posts/swift-design-patterns/</id> <content src="https://kokohuang.github.io/posts/swift-design-patterns/" /> <author> <name>kokohuang</name> </author> <category term="代码人生" /> <category term="设计模式" /> <summary> 创建型模式 工厂方法模式:在父类中提供一个创建对象的方法,允许子类决定实例化对象的类型。 抽象工厂模式:让你能创建一系列相关的对象,而无需指定其具体类。 生成器模式:使你能够分步骤创建复杂对象。该模式允许你使用相同的创建代码生成不同类型和形式的对象。 原型模式:使你能够复制已有对象,而又无需使代码依赖它们所属的类。 单例模式:让你能够保证一个类只有一个实例,并提供一个访问该实例的全局节点。 结构型模式 适配器模式:使接口不兼容的对象能够相互合作。 桥接模式:可将一个大类或一系列紧密相关的类拆分为抽象和实现两个独立的层次结构,从而能在开发时分别使用。 组合模式:可以使用它将对象组合成树状结构,并且能像使用独立对象一样使用它... </summary> </entry> <entry><title>Swift设计模式之「模板方法模式」</title><link href="https://kokohuang.github.io/posts/swift-design-patterns-template-method/" rel="alternate" type="text/html" title="Swift设计模式之「模板方法模式」" /><published>2020-04-22T12:00:00+08:00</published> <updated>2020-04-22T12:00:00+08:00</updated> <id>https://kokohuang.github.io/posts/swift-design-patterns-template-method/</id> <content src="https://kokohuang.github.io/posts/swift-design-patterns-template-method/" /> <author> <name>kokohuang</name> </author> <category term="代码人生" /> <category term="设计模式" /> <summary> 在父类中提供一个创建对象的方法,允许子类决定实例化对象的类型。 </summary> </entry> </feed>

_site/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/page2/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/page3/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

_site/page4/index.html

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)