Skip to content

Commit 3166f4d

Browse files
author
EternalChildren
committed
resolve two problem with swift
0 parents  commit 3166f4d

File tree

8 files changed

+89
-0
lines changed

8 files changed

+89
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# OSX
2+
.DS_Store
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// You're given strings J representing the types of stones that are jewels, and S representing the stones you have.
2+
// Each character in S is a type of stone you have.
3+
// You want to know how many of the stones you have are also jewels.
4+
// The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive,
5+
// so "a" is considered a different type of stone from "A".
6+
7+
8+
var J = "aA"
9+
var S = "aAAbbbb"
10+
11+
// 12ms
12+
func numJewelsInStones(_ J: String, _ S: String) -> Int {
13+
var dic = [Character: Int]()
14+
var res = 0
15+
for (_, v) in S.enumerated() {
16+
if let count = dic[v] {
17+
dic[v] = count + 1
18+
} else {
19+
dic[v] = 1
20+
}
21+
}
22+
for (_, j) in J.enumerated() {
23+
if let count = dic[j] {
24+
res += count
25+
}
26+
}
27+
return res
28+
}
29+
30+
31+
numJewelsInStones(J, S)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
var num = [Int]()
3+
4+
// 80ms
5+
func removeDuplicates1(_ nums: inout [Int]) -> Int {
6+
guard nums.count > 0 else {
7+
return 0
8+
}
9+
var i = 0
10+
for j in 1..<nums.count {
11+
if nums[i] != nums[j] {
12+
i += 1
13+
nums[i] = nums[j]
14+
}
15+
}
16+
return i + 1
17+
}
18+
19+
// 100ms
20+
func removeDuplicates2(_ nums: inout [Int]) -> Int {
21+
var dic = [Int: Int]()
22+
for i in 0..<nums.count {
23+
dic[nums[i]] = i
24+
}
25+
nums = Array(dic.keys)
26+
nums.sort()
27+
return dic.keys.count
28+
}
29+
30+
31+
removeDuplicates1(&num)
32+
removeDuplicates2(&num)
33+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

removeDuplicates/removeDuplicates.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

0 commit comments

Comments
 (0)