1
0
mirror of https://github.com/Snailclimb/JavaGuide synced 2025-06-20 22:17:09 +08:00

增加目录

This commit is contained in:
SnailClimb 2018-09-20 15:30:38 +08:00 committed by GitHub
parent 36196cec6f
commit dc21529432
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,20 @@
<!-- MarkdownTOC -->
- [说明](#说明)
- [1. KMP 算法](#1-kmp-算法)
- [2. 替换空格](#2-替换空格)
- [3. 最长公共前缀](#3-最长公共前缀)
- [4. 回文串](#4-回文串)
- [4.1. 最长回文串](#41-最长回文串)
- [4.2. 验证回文串](#42-验证回文串)
- [4.3. 最长回文子串](#43-最长回文子串)
- [4.4. 最长回文子序列](#44-最长回文子序列)
- [5. 括号匹配深度](#5-括号匹配深度)
- [6. 把字符串转换成整数](#6-把字符串转换成整数)
<!-- /MarkdownTOC -->
## 说明 ## 说明
- 本文作者wwwxmu - 本文作者wwwxmu
@ -36,34 +53,34 @@
//https://www.weiweiblog.cn/replacespace/ //https://www.weiweiblog.cn/replacespace/
public class Solution { public class Solution {
/** /**
* 第一种方法常规方法。利用String.charAt(i)以及String.valueOf(char).equals(" " * 第一种方法常规方法。利用String.charAt(i)以及String.valueOf(char).equals(" "
* )遍历字符串并判断元素是否为空格。是则替换为"%20",否则不替换 * )遍历字符串并判断元素是否为空格。是则替换为"%20",否则不替换
*/ */
public static String replaceSpace(StringBuffer str) { public static String replaceSpace(StringBuffer str) {
int length = str.length(); int length = str.length();
// System.out.println("length=" + length); // System.out.println("length=" + length);
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
char b = str.charAt(i); char b = str.charAt(i);
if (String.valueOf(b).equals(" ")) { if (String.valueOf(b).equals(" ")) {
result.append("%20"); result.append("%20");
} else { } else {
result.append(b); result.append(b);
} }
} }
return result.toString(); return result.toString();
} }
/** /**
* 第二种方法利用API替换掉所用空格一行代码解决问题 * 第二种方法利用API替换掉所用空格一行代码解决问题
*/ */
public static String replaceSpace2(StringBuffer str) { public static String replaceSpace2(StringBuffer str) {
return str.toString().replaceAll("\\s", "%20"); return str.toString().replaceAll("\\s", "%20");
} }
} }
``` ```
@ -93,36 +110,36 @@ public class Solution {
```java ```java
//https://leetcode-cn.com/problems/longest-common-prefix/description/ //https://leetcode-cn.com/problems/longest-common-prefix/description/
public class Main { public class Main {
public static String replaceSpace(String[] strs) { public static String replaceSpace(String[] strs) {
// 数组长度 // 数组长度
int len = strs.length; int len = strs.length;
// 用于保存结果 // 用于保存结果
StringBuffer res = new StringBuffer(); StringBuffer res = new StringBuffer();
// 注意:=是赋值,==是判断 // 注意:=是赋值,==是判断
if (strs == null || strs.length == 0) { if (strs == null || strs.length == 0) {
return ""; return "";
} }
// 给字符串数组的元素按照升序排序(包含数字的话,数字会排在前面) // 给字符串数组的元素按照升序排序(包含数字的话,数字会排在前面)
Arrays.sort(strs); Arrays.sort(strs);
int m = strs[0].length(); int m = strs[0].length();
int n = strs[len - 1].length(); int n = strs[len - 1].length();
int num = Math.min(m, n); int num = Math.min(m, n);
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
if (strs[0].charAt(i) == strs[len - 1].charAt(i)) { if (strs[0].charAt(i) == strs[len - 1].charAt(i)) {
res.append(strs[0].charAt(i)); res.append(strs[0].charAt(i));
} else } else
break; break;
} }
return res.toString(); return res.toString();
} }
//测试 //测试
public static void main(String[] args) { public static void main(String[] args) {
String[] strs = { "customer", "car", "cat" }; String[] strs = { "customer", "car", "cat" };
System.out.println(Main.replaceSpace(strs));//c System.out.println(Main.replaceSpace(strs));//c
} }
} }
``` ```
@ -161,23 +178,23 @@ public class Main {
```java ```java
//https://leetcode-cn.com/problems/longest-palindrome/description/ //https://leetcode-cn.com/problems/longest-palindrome/description/
class Solution { class Solution {
public int longestPalindrome(String s) { public int longestPalindrome(String s) {
if (s.length() == 0) if (s.length() == 0)
return 0; return 0;
// 用于存放字符 // 用于存放字符
HashSet<Character> hashset = new HashSet<Character>(); HashSet<Character> hashset = new HashSet<Character>();
char[] chars = s.toCharArray(); char[] chars = s.toCharArray();
int count = 0; int count = 0;
for (int i = 0; i < chars.length; i++) { for (int i = 0; i < chars.length; i++) {
if (!hashset.contains(chars[i])) {// 如果hashset没有该字符就保存进去 if (!hashset.contains(chars[i])) {// 如果hashset没有该字符就保存进去
hashset.add(chars[i]); hashset.add(chars[i]);
} else {// 如果有,就让count++(说明找到了一个成对的字符),然后把该字符移除 } else {// 如果有,就让count++(说明找到了一个成对的字符),然后把该字符移除
hashset.remove(chars[i]); hashset.remove(chars[i]);
count++; count++;
} }
} }
return hashset.isEmpty() ? count * 2 : count * 2 + 1; return hashset.isEmpty() ? count * 2 : count * 2 + 1;
} }
} }
``` ```
@ -203,26 +220,26 @@ class Solution {
```java ```java
//https://leetcode-cn.com/problems/valid-palindrome/description/ //https://leetcode-cn.com/problems/valid-palindrome/description/
class Solution { class Solution {
public boolean isPalindrome(String s) { public boolean isPalindrome(String s) {
if (s.length() == 0) if (s.length() == 0)
return true; return true;
int l = 0, r = s.length() - 1; int l = 0, r = s.length() - 1;
while (l < r) { while (l < r) {
// 从头和尾开始向中间遍历 // 从头和尾开始向中间遍历
if (!Character.isLetterOrDigit(s.charAt(l))) {// 字符不是字母和数字的情况 if (!Character.isLetterOrDigit(s.charAt(l))) {// 字符不是字母和数字的情况
l++; l++;
} else if (!Character.isLetterOrDigit(s.charAt(r))) {// 字符不是字母和数字的情况 } else if (!Character.isLetterOrDigit(s.charAt(r))) {// 字符不是字母和数字的情况
r--; r--;
} else { } else {
// 判断二者是否相等 // 判断二者是否相等
if (Character.toLowerCase(s.charAt(l)) != Character.toLowerCase(s.charAt(r))) if (Character.toLowerCase(s.charAt(l)) != Character.toLowerCase(s.charAt(r)))
return false; return false;
l++; l++;
r--; r--;
} }
} }
return true; return true;
} }
} }
``` ```
@ -254,28 +271,28 @@ class Solution {
```java ```java
//https://leetcode-cn.com/problems/longest-palindromic-substring/description/ //https://leetcode-cn.com/problems/longest-palindromic-substring/description/
class Solution { class Solution {
private int index, len; private int index, len;
public String longestPalindrome(String s) { public String longestPalindrome(String s) {
if (s.length() < 2) if (s.length() < 2)
return s; return s;
for (int i = 0; i < s.length() - 1; i++) { for (int i = 0; i < s.length() - 1; i++) {
PalindromeHelper(s, i, i); PalindromeHelper(s, i, i);
PalindromeHelper(s, i, i + 1); PalindromeHelper(s, i, i + 1);
} }
return s.substring(index, index + len); return s.substring(index, index + len);
} }
public void PalindromeHelper(String s, int l, int r) { public void PalindromeHelper(String s, int l, int r) {
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) { while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
l--; l--;
r++; r++;
} }
if (len < r - l - 1) { if (len < r - l - 1) {
index = l + 1; index = l + 1;
len = r - l - 1; len = r - l - 1;
} }
} }
} }
``` ```
@ -381,20 +398,20 @@ import java.util.Scanner;
* @Description: TODO 求给定合法括号序列的深度 * @Description: TODO 求给定合法括号序列的深度
*/ */
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
String s = sc.nextLine(); String s = sc.nextLine();
int cnt = 0, max = 0, i; int cnt = 0, max = 0, i;
for (i = 0; i < s.length(); ++i) { for (i = 0; i < s.length(); ++i) {
if (s.charAt(i) == '(') if (s.charAt(i) == '(')
cnt++; cnt++;
else else
cnt--; cnt--;
max = Math.max(max, cnt); max = Math.max(max, cnt);
} }
sc.close(); sc.close();
System.out.println(max); System.out.println(max);
} }
} }
``` ```
@ -407,38 +424,38 @@ public class Main {
//https://www.weiweiblog.cn/strtoint/ //https://www.weiweiblog.cn/strtoint/
public class Main { public class Main {
public static int StrToInt(String str) { public static int StrToInt(String str) {
if (str.length() == 0) if (str.length() == 0)
return 0; return 0;
char[] chars = str.toCharArray(); char[] chars = str.toCharArray();
// 判断是否存在符号位 // 判断是否存在符号位
int flag = 0; int flag = 0;
if (chars[0] == '+') if (chars[0] == '+')
flag = 1; flag = 1;
else if (chars[0] == '-') else if (chars[0] == '-')
flag = 2; flag = 2;
int start = flag > 0 ? 1 : 0; int start = flag > 0 ? 1 : 0;
int res = 0;// 保存结果 int res = 0;// 保存结果
for (int i = start; i < chars.length; i++) { for (int i = start; i < chars.length; i++) {
if (Character.isDigit(chars[i])) {// 调用Character.isDigit(char)方法判断是否是数字是返回True否则False if (Character.isDigit(chars[i])) {// 调用Character.isDigit(char)方法判断是否是数字是返回True否则False
int temp = chars[i] - '0'; int temp = chars[i] - '0';
res = res * 10 + temp; res = res * 10 + temp;
} else { } else {
return 0; return 0;
} }
} }
return flag == 1 ? res : -res; return flag == 1 ? res : -res;
} }
public static void main(String[] args) { public static void main(String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
String s = "-12312312"; String s = "-12312312";
System.out.println("使用库函数转换:" + Integer.valueOf(s)); System.out.println("使用库函数转换:" + Integer.valueOf(s));
int res = Main.StrToInt(s); int res = Main.StrToInt(s);
System.out.println("使用自己写的方法转换:" + res); System.out.println("使用自己写的方法转换:" + res);
} }
} }