|
4 | 4 | import java.util.Arrays;
|
5 | 5 | import java.util.List;
|
6 | 6 |
|
7 |
| -/** |
8 |
| - * 800. Similar RGB Color |
9 |
| -
|
10 |
| - In the following, every capital letter represents some hexadecimal digit from 0 to f. |
11 |
| -
|
12 |
| - The red-green-blue color "#AABBCC" can be written as "#ABC" in shorthand. For example, "#15c" is shorthand for the color "#1155cc". |
13 |
| -
|
14 |
| - Now, say the similarity between two colors "#ABCDEF" and "#UVWXYZ" is -(AB - UV)^2 - (CD - WX)^2 - (EF - YZ)^2. |
15 |
| -
|
16 |
| - Given the color "#ABCDEF", return a 7 character color that is most similar to #ABCDEF, and has a shorthand (that is, it can be represented as some "#XYZ" |
17 |
| -
|
18 |
| - Example 1: |
19 |
| - Input: color = "#09f166" |
20 |
| - Output: "#11ee66" |
21 |
| - Explanation: |
22 |
| - The similarity is -(0x09 - 0x11)^2 -(0xf1 - 0xee)^2 - (0x66 - 0x66)^2 = -64 -9 -0 = -73. |
23 |
| - This is the highest among any shorthand color. |
24 |
| -
|
25 |
| - Note: |
26 |
| -
|
27 |
| - color is a string of length 7. |
28 |
| - color is a valid RGB color: for i > 0, color[i] is a hexadecimal digit from 0 to f |
29 |
| - Any answer which has the same (highest) similarity as the best answer will be accepted. |
30 |
| - All inputs and outputs should use lowercase letters, and the output is 7 characters. |
31 |
| -
|
32 |
| - */ |
33 | 7 | public class _800 {
|
34 |
| - public static class Solution1 { |
35 |
| - public String similarRGB(String color) { |
36 |
| - List<String> allShortHandCombinations = computeAllShorthandCombinations(); |
37 |
| - int minSimilarity = Integer.MIN_VALUE; |
38 |
| - String result = ""; |
39 |
| - for (String candidate : allShortHandCombinations) { |
40 |
| - int similarity = computeSimilarity(candidate, color); |
41 |
| - if (similarity > minSimilarity) { |
42 |
| - result = candidate; |
43 |
| - minSimilarity = similarity; |
| 8 | + public static class Solution1 { |
| 9 | + public String similarRGB(String color) { |
| 10 | + List<String> allShortHandCombinations = computeAllShorthandCombinations(); |
| 11 | + int minSimilarity = Integer.MIN_VALUE; |
| 12 | + String result = ""; |
| 13 | + for (String candidate : allShortHandCombinations) { |
| 14 | + int similarity = computeSimilarity(candidate, color); |
| 15 | + if (similarity > minSimilarity) { |
| 16 | + result = candidate; |
| 17 | + minSimilarity = similarity; |
| 18 | + } |
| 19 | + } |
| 20 | + return result; |
44 | 21 | }
|
45 |
| - } |
46 |
| - return result; |
47 |
| - } |
48 | 22 |
|
49 |
| - private int computeSimilarity(String candidate, String color) { |
50 |
| - return -(Integer.parseInt(candidate.substring(1, 3), 16) - Integer.parseInt( |
51 |
| - color.substring(1, 3), 16)) * (Integer.parseInt(candidate.substring(1, 3), 16) |
52 |
| - - Integer.parseInt(color.substring(1, 3), 16)) |
53 |
| - - (Integer.parseInt(candidate.substring(3, 5), 16) - Integer.parseInt( |
54 |
| - color.substring(3, 5), 16)) * (Integer.parseInt(candidate.substring(3, 5), 16) |
55 |
| - - Integer.parseInt(color.substring(3, 5), 16)) |
56 |
| - - (Integer.parseInt(candidate.substring(5, 7), 16) - Integer.parseInt( |
57 |
| - color.substring(5, 7), 16)) * (Integer.parseInt(candidate.substring(5, 7), 16) |
58 |
| - - Integer.parseInt(color.substring(5, 7), 16)); |
59 |
| - } |
| 23 | + private int computeSimilarity(String candidate, String color) { |
| 24 | + return -(Integer.parseInt(candidate.substring(1, 3), 16) - Integer.parseInt( |
| 25 | + color.substring(1, 3), 16)) * (Integer.parseInt(candidate.substring(1, 3), 16) |
| 26 | + - Integer.parseInt(color.substring(1, 3), 16)) |
| 27 | + - (Integer.parseInt(candidate.substring(3, 5), 16) - Integer.parseInt( |
| 28 | + color.substring(3, 5), 16)) * (Integer.parseInt(candidate.substring(3, 5), 16) |
| 29 | + - Integer.parseInt(color.substring(3, 5), 16)) |
| 30 | + - (Integer.parseInt(candidate.substring(5, 7), 16) - Integer.parseInt( |
| 31 | + color.substring(5, 7), 16)) * (Integer.parseInt(candidate.substring(5, 7), 16) |
| 32 | + - Integer.parseInt(color.substring(5, 7), 16)); |
| 33 | + } |
60 | 34 |
|
61 |
| - private List<String> computeAllShorthandCombinations() { |
62 |
| - List<String> result = new ArrayList<>(); |
63 |
| - List<Character> hexNumber = new ArrayList<>( |
64 |
| - Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', |
65 |
| - 'f')); |
66 |
| - for (int i = 0; i < hexNumber.size(); i++) { |
67 |
| - for (int j = 0; j < hexNumber.size(); j++) { |
68 |
| - for (int k = 0; k < hexNumber.size(); k++) { |
69 |
| - StringBuilder sb = new StringBuilder(); |
70 |
| - sb.append("#"); |
71 |
| - sb.append(hexNumber.get(i)); |
72 |
| - sb.append(hexNumber.get(i)); |
73 |
| - sb.append(hexNumber.get(j)); |
74 |
| - sb.append(hexNumber.get(j)); |
75 |
| - sb.append(hexNumber.get(k)); |
76 |
| - sb.append(hexNumber.get(k)); |
77 |
| - result.add(sb.toString()); |
78 |
| - } |
| 35 | + private List<String> computeAllShorthandCombinations() { |
| 36 | + List<String> result = new ArrayList<>(); |
| 37 | + List<Character> hexNumber = new ArrayList<>( |
| 38 | + Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', |
| 39 | + 'f')); |
| 40 | + for (int i = 0; i < hexNumber.size(); i++) { |
| 41 | + for (int j = 0; j < hexNumber.size(); j++) { |
| 42 | + for (int k = 0; k < hexNumber.size(); k++) { |
| 43 | + StringBuilder sb = new StringBuilder(); |
| 44 | + sb.append("#"); |
| 45 | + sb.append(hexNumber.get(i)); |
| 46 | + sb.append(hexNumber.get(i)); |
| 47 | + sb.append(hexNumber.get(j)); |
| 48 | + sb.append(hexNumber.get(j)); |
| 49 | + sb.append(hexNumber.get(k)); |
| 50 | + sb.append(hexNumber.get(k)); |
| 51 | + result.add(sb.toString()); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + return result; |
79 | 56 | }
|
80 |
| - } |
81 |
| - return result; |
82 | 57 | }
|
83 |
| - } |
84 | 58 | }
|
0 commit comments