字符串篇一

字符串

520. 检测大写字母 - 力扣(LeetCode)

1
2
3
4
5
6
char a='中';
if(Character.isLowerCase(a)||Character.isUpperCase(a)) {
System.out.println("字符a是字母!");
}else {
System.out.println("字符a不是字母!");
}
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public boolean detectCapitalUse(String word) {
int num = 0;
int n = word.length();
for(char c : word.toCharArray()){
if(Character.isUpperCase(c)) num++;
}
return (num == n || num == 0 || (num == 1 && Character.isUpperCase(word.charAt(0))));

}
}

125. 验证回文串 - 力扣(LeetCode)

很难想得到Character.isLetterOrDight()这个API,所以自己写一个函数进行判断,只需要记得

Character.isLowerCase() Character.toLowerCase() Character.isUpperCase() Character.toUpperCase()

判断及筛选

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public boolean isPalindrome(String s) {
StringBuffer sb1 = new StringBuffer();
for(char c : s.toCharArray()){
if(isValid(c)) sb1.append(Character.toLowerCase(c));
}
StringBuffer sb2 = new StringBuffer(sb1).reverse();
System.out.println(sb1.toString());
return sb1.toString().equals(sb2.toString());
}
public boolean isValid(char b){
char c = Character.toLowerCase(b);
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ;
}
}

双指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public boolean isPalindrome(String s) {
StringBuffer bs1 = new StringBuffer();
for(char c : s.toCharArray()){
if(Character.isLetterOrDigit(c)){
bs1.append(c);
}
}
int n = bs1.length();
int left = 0 ,right = n - 1;
while(left < right){
if(Character.toLowerCase(bs1.charAt(left)) != Character.toLowerCase(bs1.charAt(right))){
return false;
}
left++;
right--;
}
return true;
}
}

在原字符串上直接判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public boolean isPalindrome(String s) {
char[] c = s.toLowerCase().toCharArray();
int n = s.length();
int l = 0, r = n - 1;
while(l < r){
while(!isValid(c[l]) && l < r){
++l;
}
while(!isValid(c[r]) && l < r){
--r;
}
if(c[l] != c[r]) return false;
++l;
--r;
}
return true;
}
private boolean isValid(char c){
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
}
}

14. 最长公共前缀 - 力扣(LeetCode)

横向扫描

横向扫描是一种惯性思维

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public String longestCommonPrefix(String[] strs) {
if (strs == null || Objects.equals(strs[0], "")) return "";
int n = strs.length;
String prev = strs[0];
for (int i = 1; i < n; i++) {
prev = LCP(prev, strs[i]);
}
return prev;
}

private String LCP(String str1, String str2) {
int len = Math.min(str2.length(), str1.length());
int count = 0;
while (count < len && str1.charAt(count) == str2.charAt(count)) {
count++;
}
return str1.substring(0, count);
}

纵向扫描

纵向只有两种异常的情况会停止扫描

1、如果扫描的位置是数组中元素的最后一个字符

2、扫描的字符和当前**strs[0].charAt(i) **的字符不匹配。

有一种特殊情况就是**strs[0]**扫描到了最后,那就直接返回strs[0]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs[0].length() == 0) return "";
//纵向扫描
int m = strs.length;
int n = strs[0].length();
for(int i = 0; i < n; i++){
char c = strs[0].charAt(i);
for(int j = 1; j < m; j++){
if(i == strs[j].length() || c != strs[j].charAt(i)){
return strs[0].substring(0, i);
}
}
}
return strs[0];
}
}

二分查找(暂时不会)

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
32
33
34
35
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
int minLength = Integer.MAX_VALUE;
for (String str : strs) {
minLength = Math.min(minLength, str.length());
}
int low = 0, high = minLength;
while (low < high) {
int mid = (high - low + 1) / 2 + low;
if (isCommonPrefix(strs, mid)) {
low = mid;
} else {
high = mid - 1;
}
}
return strs[0].substring(0, low);
}

public boolean isCommonPrefix(String[] strs, int length) {
String str0 = strs[0].substring(0, length);
int count = strs.length;
for (int i = 1; i < count; i++) {
String str = strs[i];
for (int j = 0; j < length; j++) {
if (str0.charAt(j) != str.charAt(j)) {
return false;
}
}
}
return true;
}
}

434. 字符串中的单词数 - 力扣(LeetCode)

1
2
3
4
5
6
7
8
9
10
class Solution {
public int countSegments(String s) {
int n = s.length();
int ans = 0;
for(int i = 0; i < n ; i++){
if((i == 0 || s.charAt(i - 1) == ' ') && s.charAt(i) != ' ') ans++;
}
return ans;
}
}

58. 最后一个单词的长度 - 力扣(LeetCode)

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int lengthOfLastWord(String s) {
int n = s.length() - 1;
//这一步是为了防止类似"Hello World "的情况的出现
while(s.charAt(n) == ' ') n--;
int ans = 0;
while(n >= 0 && s.charAt(n) != ' '){
n--;
ans++;
}
return ans;
}
}

字符串篇一
http://example.com/2024/02/29/str1/
作者
nianjx
发布于
2024年2月29日
许可协议