|
1 | 1 | packagecom.blankj.hard._044;
|
2 | 2 |
|
| 3 | +importjava.util.ArrayList; |
| 4 | +importjava.util.Collections; |
| 5 | +importjava.util.List; |
| 6 | + |
3 | 7 | /**
|
4 | 8 | * <pre>
|
5 | 9 | * author: Blankj
|
@@ -30,35 +34,74 @@ public class Solution {
|
30 | 34 | // return pi == pl;
|
31 | 35 | // }
|
32 | 36 |
|
33 |
| -publicbooleanisMatch(Strings,Stringp) { |
34 |
| -if (p.length() ==0)returns.length() ==0; |
35 |
| -intsl =s.length(),pl =p.length(); |
36 |
| -boolean[][]dp =newboolean[sl +1][pl +1]; |
37 |
| -char[]sc =s.toCharArray(),pc =p.toCharArray(); |
38 |
| -dp[0][0] =true; |
39 |
| -for (inti =1;i <=pl; ++i) { |
40 |
| -if (pc[i -1] =='*')dp[0][i] =dp[0][i -1]; |
| 37 | +// public boolean isMatch(String s, String p) { |
| 38 | +// if (p.length() == 0) return s.length() == 0; |
| 39 | +// int sl = s.length(), pl = p.length(); |
| 40 | +// boolean[][] dp = new boolean[sl + 1][pl + 1]; |
| 41 | +// char[] sc = s.toCharArray(), pc = p.toCharArray(); |
| 42 | +// dp[0][0] = true; |
| 43 | +// for (int i = 1; i <= pl; ++i) { |
| 44 | +// if (pc[i - 1] == '*') dp[0][i] = dp[0][i - 1]; |
| 45 | +// } |
| 46 | +// for (int i = 1; i <= sl; ++i) { |
| 47 | +// for (int j = 1; j <= pl; ++j) { |
| 48 | +// if (pc[j - 1] != '*') { |
| 49 | +// dp[i][j] = dp[i - 1][j - 1] && (sc[i - 1] == pc[j - 1] || pc[j - 1] == '?'); |
| 50 | +// } else { |
| 51 | +// dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; |
| 52 | +// } |
| 53 | +// } |
| 54 | +// } |
| 55 | +// return dp[sl][pl]; |
| 56 | +// } |
| 57 | + |
| 58 | +publicList<String>fullJustify(String[]words,intmaxWidth) { |
| 59 | +intlen =words.length; |
| 60 | +if (len ==0)returnCollections.emptyList(); |
| 61 | +List<String>ans =newArrayList<>(); |
| 62 | +StringBuilderspaces =newStringBuilder(); |
| 63 | +for (inti =0;i <maxWidth; ++i) { |
| 64 | +spaces.append(" "); |
41 | 65 | }
|
42 |
| -for (inti =1;i <=sl; ++i) { |
43 |
| -for (intj =1;j <=pl; ++j) { |
44 |
| -if (pc[j -1] !='*') { |
45 |
| -dp[i][j] =dp[i -1][j -1] && (sc[i -1] ==pc[j -1] ||pc[j -1] =='?'); |
| 66 | +intsLen = -1,left =0; |
| 67 | +for (inti =0;i <len; ++i) { |
| 68 | +if (sLen +words[i].length() +1 <=maxWidth) { |
| 69 | +sLen +=words[i].length() +1; |
| 70 | + }else { |
| 71 | +StringBuildersub =newStringBuilder(words[left]); |
| 72 | +intrest =maxWidth -sLen; |
| 73 | +intseg =i -left; |
| 74 | +if (seg ==0) { |
| 75 | +sub.append(spaces.substring(0,rest)); |
46 | 76 | }else {
|
47 |
| -dp[i][j] =dp[i][j -1] ||dp[i -1][j]; |
| 77 | +intleastSpace =rest /seg +1; |
| 78 | +intrestSpace =rest %seg; |
| 79 | +for (intj =left +1;j <i; ++j) { |
| 80 | +if (restSpace-- >0) { |
| 81 | +sub.append(spaces.substring(0,leastSpace +1)).append(words[j]); |
| 82 | + }else { |
| 83 | +sub.append(spaces.substring(0,leastSpace)).append(words[j]); |
| 84 | + } |
| 85 | + } |
48 | 86 | }
|
| 87 | +ans.add(sub.toString()); |
| 88 | +left =i; |
| 89 | +sLen =words[i].length(); |
49 | 90 | }
|
50 | 91 | }
|
51 |
| -returndp[sl][pl]; |
| 92 | +StringBuildersub =newStringBuilder(words[left]); |
| 93 | +for (inti =left +1;i <len; ++i) { |
| 94 | +sub.append(" ").append(words[i]); |
| 95 | + } |
| 96 | +ans.add(sub +spaces.substring(0,maxWidth -sub.length())); |
| 97 | +returnans; |
52 | 98 | }
|
53 | 99 |
|
| 100 | + |
54 | 101 | publicstaticvoidmain(String[]args) {
|
55 | 102 | Solutionsolution =newSolution();
|
56 |
| -System.out.println(solution.isMatch("aa","a"));// false |
57 |
| -System.out.println(solution.isMatch("aa","aa"));// true |
58 |
| -System.out.println(solution.isMatch("aaa","aa"));// false |
59 |
| -System.out.println(solution.isMatch("aa","*"));// true |
60 |
| -System.out.println(solution.isMatch("aa","a*"));// true |
61 |
| -System.out.println(solution.isMatch("ab","?*"));// true |
62 |
| -System.out.println(solution.isMatch("aab","c*a*b"));// false |
| 103 | +System.out.println(solution.fullJustify(newString[]{"",""},0)); |
| 104 | +System.out.println(solution.fullJustify(newString[]{"a"},1)); |
| 105 | +System.out.println(solution.fullJustify(newString[]{"This","is","an","example","of","text","justification."},16)); |
63 | 106 | }
|
64 | 107 | }
|