Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0fb599f

Browse files
committed
add 28, 73, 2094, 3024 at swift
1 parent f7f3566 commit 0fb599f

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 28. Find the Index of the First Occurrence in a String
3+
* https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/
4+
**/
5+
6+
class Solution {
7+
func strStr(_ haystack: String, _ needle: String) -> Int {
8+
let source = Array(haystack), find = Array(needle)
9+
var i = 0, j = 0
10+
while i < source.count {
11+
while i+j < source.count && j < find.count && source[i+j] == find[j] {
12+
j += 1
13+
}
14+
if j == find.count {
15+
return i
16+
}
17+
i += 1
18+
j = 0
19+
}
20+
21+
return -1
22+
}
23+
}

‎swift/0073-set-matrix-zeroes.swift‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 73. Set Matrix Zeroes
3+
* https://leetcode.com/problems/set-matrix-zeroes/
4+
**/
5+
6+
class Solution {
7+
func setZeroes(_ matrix: inout [[Int]]) {
8+
var rows: Set<Int> = [], cols: Set<Int> = []
9+
10+
for i in 0 ..< matrix.count {
11+
for j in 0 ..< matrix[i].count where matrix[i][j] == 0 {
12+
rows.insert(i)
13+
cols.insert(j)
14+
}
15+
}
16+
17+
for row in rows {
18+
for j in 0 ..< matrix[row].count {
19+
matrix[row][j] = 0
20+
}
21+
}
22+
23+
for i in 0 ..< matrix.count {
24+
for col in cols {
25+
matrix[i][col] = 0
26+
}
27+
}
28+
}
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 2094. Finding 3-Digit Even Numbers
3+
* https://leetcode.com/problems/finding-3-digit-even-numbers/
4+
**/
5+
6+
class Solution {
7+
func findEvenNumbers(_ digits: [Int]) -> [Int] {
8+
var ans: Set<Int> = []
9+
10+
for (i, a) in digits.enumerated() where a != 0 {
11+
for (j, b) in digits.enumerated() where i != j {
12+
for (k, c) in digits.enumerated() where k != j && k != i && c % 2 == 0 {
13+
ans.insert( a*100 + b*10 + c )
14+
}
15+
}
16+
}
17+
18+
return ans.sorted()
19+
}
20+
}

‎swift/3024-type-of-triangle.swift‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* 3024. Type of Triangle
3+
* https://leetcode.com/problems/type-of-triangle/
4+
**/
5+
6+
class Solution {
7+
func triangleType(_ nums: [Int]) -> String {
8+
let nums = nums.sorted()
9+
let (a, b, c) = (nums[0], nums[1], nums[2])
10+
guard a + b > c else { return "none" }
11+
if a == b && b == c { return "equilateral" }
12+
if a == b || b == c { return "isosceles" }
13+
return "scalene"
14+
}
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /