-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
800 Similar RGB Color.swift
38 lines (32 loc) 路 1.03 KB
/
800 Similar RGB Color.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// 800 Similar RGB Color.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 17/02/2021.
// Copyright 漏 2021 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/similar-rgb-color/
class Solution {
/// Finds a 7 character color that is most similar to a given color, and has a
/// shorthand.
///
/// - Parameter color: The color.
/// - Returns: A 7 character color.
///
/// - Complexity:
/// - time: O(1), only constant time is used.
/// - space: O(1), only constant space is used.
func similarRGB(_ color: String) -> String {
let color = Array(color)
var ans = "#"
for i in 0..<3 {
guard let tmp = Int(String(color[(i*2+1)..<(i*2+3)]), radix: 16) else {
fatalError("Invalid color")
}
let round = ((tmp + 8) / 17) * 17
ans = round > 0 ? "\(ans)\(String(round, radix: 16))" : "\(ans)00"
}
return ans
}
}