|
| 1 | + |
| 2 | +```java |
| 3 | + |
| 4 | +/*** |
| 5 | +Valid Word Abbreviation |
| 6 | +Input : s = "internationalization", abbr = "i12iz4n" |
| 7 | +Output : true |
| 8 | +***************************************************** |
| 9 | +
|
| 10 | +Merge Intervals |
| 11 | +Example 1: |
| 12 | +Input: [(1,3)] |
| 13 | +Output: [(1,3)] |
| 14 | +Example 2: |
| 15 | +Input: [(1,3),(2,6),(8,10),(15,18)] |
| 16 | +Output: [(1,6),(8,10),(15,18)] |
| 17 | +
|
| 18 | +***************************************************** |
| 19 | +Log Sorting |
| 20 | +Input: |
| 21 | + logs = [ |
| 22 | + "zo4 4 7", |
| 23 | + "a100 Act zoo", |
| 24 | + "a1 9 2 3 1", |
| 25 | + "g9 act car" |
| 26 | + ] |
| 27 | +Output: |
| 28 | + [ |
| 29 | + "a100 Act zoo", |
| 30 | + "g9 act car", |
| 31 | + "zo4 4 7", |
| 32 | + "a1 9 2 3 1" |
| 33 | + ] |
| 34 | +Explanation: "Act zoo" < "act car", so the output is as above. |
| 35 | +
|
| 36 | +
|
| 37 | +
|
| 38 | +***************************************************** |
| 39 | +Decode Ways |
| 40 | +Input: "12" |
| 41 | +Output: 2 |
| 42 | +Explanation: It could be decoded as AB (1 2) or L (12). |
| 43 | +*/ |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +public class Solution { |
| 48 | + /** |
| 49 | + * @param logs: the logs |
| 50 | + * @return: the log after sorting |
| 51 | + */ |
| 52 | + |
| 53 | + class MyCompartor implements Comparator<String> { |
| 54 | + public int compare(String s1, String s2){ |
| 55 | + int t1 = s1.indexOf(' '); |
| 56 | + int t2 = s2.indexOf(' '); |
| 57 | + String id1 = s1.substring(0, t1); |
| 58 | + String id2 = s2.substring(0, t2); |
| 59 | + String body1 = s1.substring(t1); |
| 60 | + String body2 = s2.substring(t2); |
| 61 | + |
| 62 | + if(body1.equals(body2)){ |
| 63 | + return id1.compareTo(id2); |
| 64 | + }else{ |
| 65 | + return body1.compareTo(body2); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public String[] logSort(String[] logs) { |
| 71 | + // Write your code here |
| 72 | + |
| 73 | + int size = logs.length; |
| 74 | + int idx = size -1; |
| 75 | + String[] res = new String[size]; |
| 76 | + |
| 77 | + List<String> logsStr = new LinkedList<>(); |
| 78 | + |
| 79 | + for(int i = size -1; i >= 0 ; i--){ |
| 80 | + |
| 81 | + int index = logs[i].indexOf(" "); |
| 82 | + String temp = logs[i].substring(index); |
| 83 | + |
| 84 | + if(Character.isDigit(temp.charAt(1))){ |
| 85 | + |
| 86 | + res[idx--] = logs[i]; |
| 87 | + }else{ |
| 88 | + logsStr.add(logs[i]); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + Collections.sort(logsStr, new MyCompartor()); |
| 93 | + |
| 94 | + int i = 0; |
| 95 | + for(String item : logsStr){ |
| 96 | + res[i++] = item; |
| 97 | + } |
| 98 | + |
| 99 | + return res; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +/*** |
| 105 | +Decode Ways |
| 106 | +*/ |
| 107 | + |
| 108 | +public class Solution { |
| 109 | + /** |
| 110 | + * @param s: a string, encoded message |
| 111 | + * @return: an integer, the number of ways decoding |
| 112 | + */ |
| 113 | + public int numDecodings(String s) { |
| 114 | + // write your code here |
| 115 | + |
| 116 | + if(s == null || s.length() == 0){ |
| 117 | + return 0; |
| 118 | + } |
| 119 | + |
| 120 | + int l = s.length(); |
| 121 | + if (l == 0) { |
| 122 | + return 0; // only for this problem, but the ans should be 1 |
| 123 | + } |
| 124 | + int[] f = new int[l + 1]; |
| 125 | + f[0] = 1; |
| 126 | + |
| 127 | + char chars[] = s.toCharArray(); |
| 128 | + |
| 129 | + |
| 130 | + for(int i = 1; i <= l; i++){ |
| 131 | + |
| 132 | + if(chars[i - 1] != '0'){ |
| 133 | + f[i] += f[i -1]; |
| 134 | + } |
| 135 | + |
| 136 | + if(i >= 2){ |
| 137 | + int val = (chars[i - 2] - '0') * 10 + chars[i - 1] - '0'; |
| 138 | + if(val >= 10 && val <= 26){ |
| 139 | + f[i] += f[i -2]; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return f[l]; |
| 145 | + |
| 146 | + |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
0 commit comments