Skip to content

Commit 0429a48

Browse files
committed
Valid sudoku & fraction to decimal leetcode problem solved
1 parent a991725 commit 0429a48

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/fraction-to-recurring-decimal/
4+
5+
6+
7+
func fractionToDecimal(_ numerator: Int, _ denominator: Int) -> String {
8+
var result = ""
9+
var remainderMap : [Int : Int] = [:]
10+
if numerator == 0 {
11+
return "0"
12+
}
13+
14+
if numerator < 0 || denominator < 0 {
15+
result.append("-")
16+
}
17+
18+
var quotient = abs(numerator/denominator)
19+
result += "\(quotient)"
20+
var remainder = (numerator % denominator) * 10
21+
if remainder == 0 {
22+
return result
23+
} else {
24+
result += "."
25+
}
26+
27+
while remainder != 0 {
28+
if remainderMap[remainder] != nil {
29+
let beginning = remainderMap[remainder]
30+
let left = result[result.index(result.startIndex, offsetBy: 0)..<result.index(result.startIndex, offsetBy: beginning ?? 0)]
31+
let right = result[result.index(result.startIndex, offsetBy: beginning ?? 0)..<result.index(result.startIndex, offsetBy: result.count)]
32+
result = left + "(" + right + ")"
33+
return result
34+
} else {
35+
remainderMap[remainder] = result.count
36+
quotient = remainder/denominator
37+
result += "\(quotient)"
38+
remainder = (remainder % denominator) * 10
39+
}
40+
}
41+
return result
42+
}
43+
44+
print(fractionToDecimal(4, 333))
45+
print(fractionToDecimal(1, 2))
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='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/valid-sudoku/description/
4+
5+
func isValidSudoku(_ board: [[Character]]) -> Bool {
6+
var col = [Int : Set<Character>]()
7+
var row = [Int : Set<Character>]()
8+
var square = [Int : Set<Character>]() //keys r/3 * 3 + c/3
9+
10+
for r in 0..<9 {
11+
for c in 0..<9 {
12+
let value = board[r][c]
13+
if value == "." {
14+
continue
15+
}
16+
if row[r]?.contains(value) == true || col[c]?.contains(value) == true || square[(r/3)*3 + c/3]?.contains(value) == true {
17+
return false
18+
}
19+
row[r, default: Set<Character>()].insert(value)
20+
col[c, default: Set<Character>()].insert(value)
21+
square[(r/3)*3 + c/3, default: Set<Character>()].insert(value)
22+
}
23+
}
24+
return true
25+
}
26+
27+
print(isValidSudoku([["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]))
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='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)