|
31 | 31 | There is only one optimal division for each test case.
|
32 | 32 | */
|
33 | 33 | public class _553 {
|
| 34 | + public static class Solution1 { |
| 35 | + /** |
| 36 | + * Credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_553.java |
| 37 | + */ |
| 38 | + public String optimalDivision(int[] nums) { |
| 39 | + /**https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html: |
| 40 | + * StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. |
| 41 | + * The String "[George:Sally:Fred]" may be constructed as follows: |
| 42 | + StringJoiner sj = new StringJoiner(":", "[", "]"); |
| 43 | + sj.add("George").add("Sally").add("Fred"); |
| 44 | + String desiredString = sj.toString();*/ |
34 | 45 |
|
35 |
| - /**Credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_553.java*/ |
36 |
| - public String optimalDivision(int[] nums) { |
37 |
| - /**https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html: |
38 |
| - * StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. |
39 |
| - * The String "[George:Sally:Fred]" may be constructed as follows: |
40 |
| - StringJoiner sj = new StringJoiner(":", "[", "]"); |
41 |
| - sj.add("George").add("Sally").add("Fred"); |
42 |
| - String desiredString = sj.toString();*/ |
43 |
| - |
44 |
| - if (nums.length == 1) { |
45 |
| - return "" + nums[0]; |
46 |
| - } |
47 |
| - if (nums.length == 2) { |
48 |
| - return nums[0] + "/" + nums[1]; |
49 |
| - } |
| 46 | + if (nums.length == 1) { |
| 47 | + return "" + nums[0]; |
| 48 | + } |
| 49 | + if (nums.length == 2) { |
| 50 | + return nums[0] + "/" + nums[1]; |
| 51 | + } |
50 | 52 |
|
51 |
| - /**Tricky one: the solution is fixed: always wrap the one from the second until the last. |
52 |
| - * Another important thing to note that such way could work is that: |
53 |
| - * the prerequisite is: Elements will be in range [2,1000], so no elements are smaller than 1.*/ |
54 |
| - StringJoiner stringJoiner = new StringJoiner("/"); |
55 |
| - for (int i = 1; i < nums.length; i++) { |
56 |
| - stringJoiner.add("" + nums[i]); |
| 53 | + /**Tricky one: the solution is fixed: always wrap the one from the second until the last. |
| 54 | + * Another important thing to note that such way could work is that: |
| 55 | + * the prerequisite is: Elements will be in range [2,1000], so no elements are smaller than 1.*/ |
| 56 | + StringJoiner stringJoiner = new StringJoiner("/"); |
| 57 | + for (int i = 1; i < nums.length; i++) { |
| 58 | + stringJoiner.add("" + nums[i]); |
| 59 | + } |
| 60 | + return String.format("%d/(%s)", nums[0], stringJoiner.toString()); |
57 | 61 | }
|
58 |
| - return String.format("%d/(%s)", nums[0], stringJoiner.toString()); |
59 | 62 | }
|
60 | 63 |
|
61 | 64 | }
|
0 commit comments