1、无重复字符的最长子串
class Solution {
public int lengthOfLongestSubstring(String s) {
int []hash = new int [500];
int max = 0;
int i = 0, j = 0;
while (i < s.length() && j <s.length() ) {
if(hash[s.charAt(j)] == 0) {
hash[s.charAt(j)] = 1;
j++;
max = (j - i) > max ? (j - i) : max;
} else {
hash[s.charAt(i)] = 0;
i++;
}
}
return max;
}
}
参考:https://blog.csdn.net/liuguangqiang/article/details/79931473
2、最长公共前缀
public static String longestCommonPrefix(String[] strs) {
int count = strs.length;
String prefix = "";
if(count != 0){
prefix = strs[0];
}
for(int i=0; i<count; i++){
//关键代码,不断的从后往前截取字符串,然后与之相比,直到startsWith()返回true
while(!strs[i].startsWith(prefix)){
prefix = prefix.substring(0, prefix.length()-1);
}
}
return prefix;
}
参考:https://blog.csdn.net/fengpojian/article/details/81326781
3、字符串的排列
class Solution {
public boolean checkInclusion(String s1, String s2) {
int l1 = s1.length();
int l2 = s2.length();
int [] count = new int [128];
if(l1 > l2)
return false;
for(int i = 0; i<l1; i++){
count[s1.charAt(i) - 'a']++;
count[s2.charAt(i) - 'a']--;
}
if(allZero(count))
return true;
for(int i = l1; i<l2; i++){
count[s2.charAt(i) - 'a']--;
count[s2.charAt(i-l1) - 'a']++;
if(allZero(count))
return true;
}
return false;
}
public boolean allZero(int [] count){
int l = count.length;
for(int i = 0; i < l; i++){
if(count[i] != 0)
return false;
}
return true;
}
}
参考连接:https://www.colabug.com/4388406.html
4、字符串相乘
public class Solution {
public String multiply(String num1, String num2) {
num1 = new StringBuilder(num1).reverse().toString();
num2 = new StringBuilder(num2).reverse().toString();
int[] d = new int[num1.length()+num2.length()];
for(int i=0;i<num1.length();i++){
int a = num1.charAt(i)-'0';
for(int j=0;j<num2.length();j++){
int b = num2.charAt(j)-'0';
d[i+j] +=a*b;
}
}
StringBuilder sb = new StringBuilder();
for(int i=0;i<d.length;i++){
int digit = d[i]%10;
int carry = d[i]/10;
sb.insert(0,digit);
if(i<d.length-1){
d[i+1] +=carry;
}
}
while(sb.length()>0 && sb.charAt(0)=='0'){
sb.deleteCharAt(0);
}
return sb.length()==0? "0" :sb.toString();
}
}
原文:https://blog.csdn.net/Lynn_Baby/article/details/80757305
5、翻转字符串里的单词
答案:https://blog.csdn.net/Regemc/article/details/79781586
6、简化路径
答案:https://www.cnblogs.com/grandyang/p/4347125.html
7、复原IP地址
答案:https://www.cnblogs.com/grandyang/p/4305572.html