字符串篇四

数字与字符间的转换

Integer.parseInt(a);将a转换成int类型

Integer.toString(a);将a转换成String类型

int[] cp = score.clone();将数组score的值赋给cp

299. 猜数字游戏 - 力扣(LeetCode)

  • 这个题说白了还是统计字符类别的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public String getHint(String secret, String guess) {
int bulls = 0;
int[] cntS = new int[10];
int[] cntG = new int[10];
for(int i = 0; i < secret.length(); i++){
if(secret.charAt(i) == guess.charAt(i)) ++bulls;
else{
//这里相当于统计字符
++cntS[secret.charAt(i) - '0'];
++cntG[guess.charAt(i) - '0'];
}
}
int cows = 0;
for(int i = 0; i < 10; ++i){
//理解一个点cntG[i], cntS[i]在同一位置上的字符肯定是相等的
cows += Math.min(cntG[i], cntS[i]);
}
return Integer.toString(bulls) + "A" + Integer.toString(cows) + "B";
}
}

412. Fizz Buzz - 力扣(LeetCode)

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public List<String> fizzBuzz(int n) {
List<String> ans = new ArrayList<>();
for (int i = 1; i <= n; i++) {
String cur = "";
if (i % 3 == 0) cur += "Fizz";
if (i % 5 == 0) cur += "Buzz";
if (cur.length() == 0) cur = i + "";
ans.add(cur);
}
return ans;
}
}

506. 相对名次 - 力扣(LeetCode)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
String[] ss = new String[]{"Gold Medal", "Silver Medal", "Bronze Medal"};
public String[] findRelativeRanks(int[] score) {
int n = score.length;
String[] ret = new String[n];
int[] clone = score.clone();
Arrays.sort(clone);
Map<Integer, Integer> map = new HashMap<>();
for(int i = n - 1; i >= 0; i--) map.put(clone[i], n - 1 - i);
for(int i = 0; i < n; i++){
int rank = map.get(score[i]);
ret[i] = rank < 3 ? ss[rank] : String.valueOf(rank + 1);
}
return ret;
}
}

539. 最小时间差 - 力扣(LeetCode)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int findMinDifference(List<String> timePoints) {
int n = timePoints.size() * 2;
int[] nums = new int[n];
for (int i = 0, idx = 0; i < n / 2; i++, idx += 2) {
String[] ss = timePoints.get(i).split(":");
int h = Integer.parseInt(ss[0]), m = Integer.parseInt(ss[1]);
nums[idx] = h * 60 + m;
nums[idx + 1] = nums[idx] + 1440;
}
Arrays.sort(nums);
int ans = nums[1] - nums[0];
for (int i = 0; i < n - 1; i++) ans = Math.min(ans, nums[i + 1] - nums[i]);
return ans;
}
}

substring()当有两个参数的时候左闭右开区间,当只有一个参数的时候从这个参数开始(闭区间)一直到字符结束位置。

1
2
3
4
5
String ss = "abcdefg";
String e= ss.substring(0,3);
String c = ss.substring(5);
System.out.println("两个参数:" +e); \\两个参数:abc
System.out.println("一个参数:" + c); \\一个参数:fg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int findMinDifference(List<String> timePoints) {
int[] arr = new int[timePoints.size()];
for(int i = 0; i < arr.length; i++){
// System.out.println(timePoints.get(i).substring(0, 2));
// System.out.println(timePoints.get(i).substring(3));
arr[i] = Integer.parseInt(timePoints.get(i).substring(0, 2))*60 + Integer.parseInt(timePoints.get(i).substring(3));
}
// System.out.println(Arrays.toString(arr));
Arrays.sort(arr);
int min = Integer.MAX_VALUE;
for(int i = 1; i < arr.length; i++) min = Math.min(min, arr[i] - arr[i - 1]);

return Math.min(min, arr[0] + 1440 - arr[arr.length - 1]);

}
}

553. 最优除法 - 力扣(LeetCode)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public String optimalDivision(int[] nums) {
int n = nums.length;
StringBuilder bs = new StringBuilder();
for(int i = 0; i < n; i++){
bs.append(nums[i]);
if(i + 1 < n) bs.append("/");
}
if(n > 2){
bs.insert(bs.indexOf("/") + 1, "(");
bs.append(")");
}
return bs.toString();
}
}

537. 复数乘法 - 力扣(LeetCode)

.split()方法的解释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
String str1 = "Welcome-To-Shanxi-XiAn";
String[] ss1 = str1.split("-");
//只是限定了分隔符号-所以返回了:[Welcome, To, Shanxi, XiAn]
System.out.println(Arrays.toString(ss1));

String[] sss1 = str1.split("-", 2);
//分隔符设置分割份数返回值,因为限定了两个所以返回:[Welcome, To-Shanxi-XiAn]
System.out.println(Arrays.toString(sss1));

String str2 = "www.baidu.com";
String[] ss2 = str2.split("\\.");
//转义字符的返回值:[www, baidu, com],如果不进行转义就会返回[]。
System.out.println(Arrays.toString(ss2));

String str3 = "acount=? and uu =? or n=?";
String[] ss3 = str3.split("and|or");
//多个分隔符返回值:[acount=? , uu =? , n=?]
System.out.println(Arrays.toString(ss3));

String str4 = "1+1i";
//通过\\进行转义并通过|进行分隔返回值:[1, 1]
String[] ss4 = str4.split("\\+|i");
System.out.println(Arrays.toString(ss4));
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public String complexNumberMultiply(String num1, String num2) {
String[] ss1 = num1.split("\\+|i"), ss2 = num2.split("\\+|i");
int a = parse(ss1[0]), b = parse(ss1[1]);
int c = parse(ss2[0]), d = parse(ss2[1]);
int A = a * c - b * d, B = b * c + a * d;
return A + "+" + B + "i";
}
int parse(String s) {
return Integer.parseInt(s);
}
}

443. 压缩字符串 - 力扣(LeetCode)

代码整的没太懂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public int compress(char[] cs) {
int n = cs.length;
int i = 0, j = 0;
while (i < n) {
int idx = i;
while (idx < n && cs[idx] == cs[i]) idx++;
int cnt = idx - i;
cs[j++] = cs[i];
if (cnt > 1) {
int start = j, end = start;
while (cnt != 0) {
cs[end++] = (char)((cnt % 10) + '0');
cnt /= 10;
}
reverse(cs, start, end - 1);
j = end;
}
i = idx;
}
return j;
}
void reverse(char[] cs, int start, int end) {
while (start < end) {
char t = cs[start];
cs[start] = cs[end];
cs[end] = t;
start++; end--;
}
}
}

13. 罗马数字转整数 - 力扣(LeetCode)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int romanToInt(String s) {
int[] gra1 = new int[] {1, 5, 10, 50,100,500,1000};
char[] gra2 = new char[]{'I', 'V', 'X', 'L', 'C', 'D', 'M'};
Map<Character, Integer> map = new HashMap<>();
for(int i = 0; i < 7; i++) map.put(gra2[i], gra1[i]);
int n = s.length();
int max = Integer.MIN_VALUE;
int ans = 0;
for(int i = n - 1; i > -1; i--){
int v = map.get(s.charAt(i));
if(v >= max){
ans += v;
max = v;
}else{
ans -= v;
}
}
return ans;
}
}

12. 整数转罗马数字 - 力扣(LeetCode)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public String intToRoman(int num) {
int[] value = new int[] {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900,1000};
String[] key = new String[]{"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C","CD","D", "CM","M"};
StringBuilder ret = new StringBuilder();
for(int i =value.length - 1; i > -1; i--){
int val = value[i];
String symbol = key[i];
while(num >= val){
num -= val;
ret.append(symbol);
}
if(num == 0) break;
}
return ret.toString();
}
}

481. 神奇字符串 - 力扣(LeetCode)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int magicalString(int n) {
//这里定义n + 2大小的目的是防止while循环里j++加了两次而造成数组越界
int[] cnt = new int[n + 2];
cnt[0] = 1; cnt[1] = cnt[2] = 2;
int j = 3, i = 2, c = 2;
while(j < n){
c ^= 3;
cnt[j++] = c;
if(cnt[i++] == 2) cnt[j++] = c;
}
int ans = 0;
for(int b = 0; b < n; b++) ans += 2 - cnt[b];
return ans;
}
}

上面的这种方法太过于难以像到了,尤其是 C ^= 3 这一句。使用下面进行模拟就比较容易理解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public int magicalString(int n) {
StringBuilder bs = new StringBuilder("122");
String BLX = "1";
//这里注意一定是从2开始的,脑子又不灵光了
int x = 2;
while (x < n){
if (bs.charAt(x) == '1') bs.append(BLX.repeat(1));
else bs.append(BLX.repeat(2));
//这一句这样写感觉有一点丑陋
BLX = BLX == "1" ? "2" : "1";
//BLX = BLX.equals("1") ? "2" : "1";
x++;
}
System.out.println(bs.toString());
int ans = 0;
for (int i = 0; i < n; i++) ans += '2' - bs.charAt(i) ;
return ans;
}
}

上面的这种方法对于方法的使用和利用很好


字符串篇四
http://example.com/2024/03/10/str4/
作者
nianjx
发布于
2024年3月10日
许可协议