Skip to content

Commit 43dea4d

Browse files
author
Michael Ho
committed
Reorganize folders
1 parent 843eaa6 commit 43dea4d

File tree

211 files changed

+661
-261
lines changed

Some content is hidden

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

211 files changed

+661
-261
lines changed

Easy/EasyProblems.playground/Contents.swift

Lines changed: 0 additions & 14 deletions
This file was deleted.

Medium/0043-MultiplyStrings.playground/Contents.swift

Lines changed: 0 additions & 28 deletions
This file was deleted.

Medium/0114-FlattenBinaryTreeToLinkedList.playground/Contents.swift

Lines changed: 0 additions & 53 deletions
This file was deleted.

Medium/0152-MaximumProductSubarray.playground/Contents.swift

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// LeetCode: https://leetcode.com/problems/broken-calculator/
2+
// Hint: Operate from Y is easier
3+
4+
5+
import XCTest
6+
7+
class Solution {
8+
func brokenCalc(_ X: Int, _ Y: Int) -> Int {
9+
if X >= Y {
10+
return X-Y
11+
} else {
12+
// Reverse the operation from X to Y, available operations:
13+
// 1. plus 1: +1
14+
// 2. divide by 2: /2
15+
return Y%2 == 0 ? 1+brokenCalc(X, Y/2) : 2+brokenCalc(X, (Y+1)/2)
16+
}
17+
}
18+
}
19+
20+
class Tests: XCTestCase {
21+
let s = Solution()
22+
23+
func testSample1() {
24+
let input = (2, 3)
25+
let expected = 2
26+
XCTAssertEqual(expected, s.brokenCalc(input.0, input.1))
27+
}
28+
29+
func testSample2() {
30+
let input = (5, 8)
31+
let expected = 2
32+
XCTAssertEqual(expected, s.brokenCalc(input.0, input.1))
33+
}
34+
35+
func testSample3() {
36+
let input = (3, 10)
37+
let expected = 3
38+
XCTAssertEqual(expected, s.brokenCalc(input.0, input.1))
39+
}
40+
41+
func testSample4() {
42+
let input = (1, 1000000000)
43+
let expected = 39
44+
XCTAssertEqual(expected, s.brokenCalc(input.0, input.1))
45+
}
46+
47+
func testSample5() {
48+
let input = (1024, 1)
49+
let expected = 1023
50+
XCTAssertEqual(expected, s.brokenCalc(input.0, input.1))
51+
}
52+
}
53+
54+
Tests.defaultTestSuite.run()

Easy/0007-ReverseInteger+0069-Sqrtx.playground/contents.xcplayground renamed to Medium/0991-BrokenCalculator.playground/contents.xcplayground

File renamed without changes.

Medium/0002-AddTwoNumbers.playground/Contents.swift renamed to Problems/1-100/0002-AddTwoNumbers.playground/Contents.swift

File renamed without changes.

Easy/0021-MergeTwoSortedLists.playground/contents.xcplayground renamed to Problems/1-100/0002-AddTwoNumbers.playground/contents.xcplayground

File renamed without changes.

Medium/0003-LongestSubstringWithoutRepeatingCharacters.playground/Contents.swift renamed to Problems/1-100/0003-LongestSubstringWithoutRepeatingCharacters.playground/Contents.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,6 @@
44
import XCTest
55

66
class Solution {
7-
// func lengthOfLongestSubstring(_ s: String) -> Int {
8-
// guard s.count > 0 else {
9-
// return 0
10-
// }
11-
//
12-
// var maxCount = 1
13-
// if s.count == 1 {
14-
// return maxCount
15-
// }
16-
// let strArr = Array(s) // Key: convert string to array
17-
// for (index, char) in strArr.enumerated() {
18-
// var dict: [Character : Int] = [:]
19-
// var count = 1
20-
// dict[char] = 1
21-
// var movingIdx = index + 1
22-
// while movingIdx < strArr.count, dict[strArr[movingIdx]] ?? 0 < 1 {
23-
// dict[strArr[movingIdx]] = 1
24-
// count += 1
25-
// movingIdx += 1
26-
// }
27-
// maxCount = count > maxCount ? count : maxCount
28-
// }
29-
// return maxCount
30-
// }
31-
// 14
327
func lengthOfLongestSubstring(_ s: String) -> Int {
338
let arr = Array(s)
349
var dict = [Character : Int]() // [char : idx]

Easy/0009-PalindromeNumber.playground/contents.xcplayground renamed to Problems/1-100/0003-LongestSubstringWithoutRepeatingCharacters.playground/contents.xcplayground

File renamed without changes.

0 commit comments

Comments
 (0)