Skip to content

Commit ed9963b

Browse files
华为od的一二面真题
1 parent 9a2bba9 commit ed9963b

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
```java
2+
package main.java;
3+
4+
/**
5+
* Lixiaomin l00495964对所有人说说: 08:14 PM
6+
* 有一种速记方式,针对重复内容有一套独特的缩写规则:
7+
* 1.重复的部分会被以 "(重复内容)<重复次数>" 形式记录,并且可能存在嵌套缩写关系。不重复的部分按照原样记录。
8+
* <p>
9+
* 现给一个符合此速记方式的字符串 records,请以字符串形式返回复原后的内容。
10+
* 注: records 仅由小写字母、数字及<, >, (, )组成。
11+
* <p>
12+
* 示例 1:
13+
* <p>
14+
* 输入:records = "abc(d)<2>e"
15+
* <p>
16+
* 输出:"abcdde"
17+
* <p>
18+
* 解释:字符串中出现 "(d)<2>",表示 "d" 重复出现 2 次,因此返回复原后的内容 "abcdde"。
19+
* <p>
20+
* 示例 2:
21+
* <p>
22+
* 输入:records = "a(b(c)<3>d)<2>e"
23+
* <p>
24+
* 输出:"abcccdbcccde"
25+
* <p>
26+
* 解释:字符串中出现 "a(b(c)<3>d)<2>",其中 "(c)<3>" 表示 "c" 出现 3 次,复原为 "ccc";"(bcccd)<2>" 表示 "bcccd" 重复出现 2 次,复原为 "bcccdbcccd"。最终返回复原后内容 "abcccdbcccde"
27+
* <p>
28+
* 提示:
29+
* •1 <= records.length <= 200
30+
* •2 <= 重复次数 <= 10
31+
* •题目保证返回结果字符串长度小于等于 10^4
32+
* •输入保证合法,确保括号与尖括号成对出现
33+
* •嵌套深度不超过 13
34+
*/
35+
public class Main {
36+
37+
public static void main(String[] args) {
38+
// String s = "a(b(c)<3>d)<2>e";
39+
// String s3 = "abc(d)<2>e";
40+
// System.out.println(solv(s));
41+
// System.out.println(solv(s3));
42+
String a = "a";
43+
String b = new String("a");
44+
}
45+
46+
private static String solv(String s) {
47+
int length = s.length();
48+
StringBuilder sb = new StringBuilder();
49+
int firstIndex = s.indexOf("<");
50+
int nextIndex = s.indexOf(">");
51+
int num = Integer.parseInt(s.substring(firstIndex + 1, nextIndex));
52+
int firstCharIndex = firstIndex - 1;
53+
int nextCharIndex = 0;
54+
String leftStr = s.substring(0, firstCharIndex);
55+
nextCharIndex = leftStr.lastIndexOf('(');
56+
String charRes = s.substring(nextCharIndex + 1, firstCharIndex);
57+
StringBuilder tempSb = new StringBuilder();
58+
for (int i = 0; i < num; i++) {
59+
tempSb.append(charRes);
60+
}
61+
charRes = tempSb.toString();
62+
for (int i = 0; i < length; ) {
63+
if ((i < nextCharIndex || i > firstCharIndex) && (i < firstIndex || i > nextIndex)) {
64+
sb.append(s.charAt(i));
65+
} else if (i == firstIndex) {
66+
i = nextIndex;
67+
sb.append(charRes);
68+
}
69+
i++;
70+
}
71+
if (sb.toString().contains("<")) return solv(sb.toString());
72+
else return sb.toString();
73+
}
74+
}
75+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
```java
2+
package main.java;
3+
4+
/**
5+
* 在一个由 '0' 和 '1' 组成的二维矩阵内,找到只包含 '1' 的最大正方形,并返回其面积。
6+
* 1 0 1 1 1
7+
* 1 0 1 1 1
8+
* 1 0 1 1 1
9+
*
10+
* 转换后
11+
* 1 0 1 1 1
12+
* 1 0 1 2 2
13+
* 1 0 1 2 2
14+
*/
15+
public class Main {
16+
public static void main(String[] args) {
17+
int[][] arr = new int[][]{{1, 0, 1, 1, 1}, {1, 0, 1, 1, 1}, {1, 0, 1, 1, 1}};
18+
int l1 = arr.length;
19+
int l2 = arr[0].length;
20+
int[][] arr2 = new int[l1][l2];
21+
int max = 1;
22+
for (int i = 0; i < l1; i++) {
23+
for (int j = 0; j < l2; j++) {
24+
if (arr[i][j] == 1) {
25+
if (i > 0 && j > 0) {
26+
arr2[i][j] = Math.min(Math.min(arr2[i - 1][j], arr2[i][j - 1]), arr2[i - 1][j - 1]) + 1;
27+
} else {
28+
arr2[i][j] = 1;
29+
}
30+
}
31+
max = Math.max(max, arr2[i][j]);
32+
}
33+
}
34+
System.out.println(max * max);
35+
}
36+
}
37+
```

0 commit comments

Comments
 (0)