继续在力扣网用java练题。主要作为自己的笔记吧,后面再把算法运行速度优化下。
下面是题目链接:
力扣网 探索初级算法 字符串 跳转链接
字符串
- 反转字符串
public static void reverseString(char[] s) {
int len=s.length;
for (int i = 0; i < len/2; i++) {
char a=s[len-i-1];
s[len-i-1]=s[i];
s[i]=a;
}
}
- 整数反转
public static int reverse(int x) {
Long num=0L;
int symbol=0; //保存符号
if(x<0){
symbol=1;
x*=-1;
}
while (x>0){
int a=x%10;
num=num*10+a;
x=x/10;
}
//加上符号
num=symbol==0?num:num*-1;
if(num>Integer.MIN_VALUE&&num< Integer.MAX_VALUE){
return num.intValue();
}
return 0;
}
- 字符串中的第一个唯一字符
public int firstUniqChar(String s) {
char[] str=s.toCharArray();
for (int i = 0; i <str.length ; i++) {
int same=0;
for (int j = 0; j < str.length; j++) {
if(str[i]==str[j]&&i!=j){
same=1;
j=str.length;
}
}
if(same==0){
return i;
}
}
return -1;
}
- 有效的字母异位词
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()){
return false;
}
StringBuffer strs=new StringBuffer();
StringBuffer strt=new StringBuffer();
for (char i = 'a'; i < 'z'; i++) {
for (int j = 0; j < s.length(); j++) {
if (i==s.charAt(j)){
strs.append(i);
}
}
for (int j = 0; j < t.length(); j++) {
if (i==t.charAt(j)){
strt.append(i);
}
}
}
if (strs.toString().equals(strt.toString())){
return true;
}else {
return false;
}
}
- 验证回文字符串
public boolean isPalindrome(String s) {
s=s.trim();
return huiwen(s,0,s.length()-1,false);
}
public static boolean huiwen(String s,int start,int end,Boolean has){
Boolean bStart=false;
Boolean bEnd=false;
while (!bStart||!bEnd){
if(start>=end){
return true;
}
char startC=s.charAt(start);
bStart= ('a'<=startC&&startC<='z')||('A'<=startC&&startC<='Z'||'0'<=startC&&startC<='9');
char endC=s.charAt(end);
bEnd=('a'<=endC&&endC<='z')||('A'<=endC&&endC<='Z'||'0'<=endC&&endC<='9');
if(!bStart){
start+=1;
}
if(!bEnd){
end-=1;
}
}
if(!String.valueOf(s.charAt(start)).equalsIgnoreCase(String.valueOf(s.charAt(end)))){
return false;
}
return huiwen(s,start+1,end-1,true);
}
- 字符串转换整数 (atoi)
public int myAtoi(String str) {
str=str.trim();
int symbol=0; //0 无符号 1='-' 2='+'
int start=0;
int size=0;
if(str.length()>0){
int index=0;
if(str.charAt(0)=='-'){
symbol=1;
index++;
}else if(str.charAt(0)=='+'){
symbol=2;
index++;
}
for (int i = index; i <str.length() ; i++) {
if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
if(str.charAt(i)=='0'&&size==0){
start+=1;
}else {
size+=1;
}
}else {
break;
}
}
String s=str.substring(symbol==0?start:start+1,symbol==0?start+size:start+size+1);
if(s.length()<=0){
return 0;
} if(s.length()>10){
return symbol==1?Integer.MIN_VALUE:Integer.MAX_VALUE;
}else {
Long num=symbol==1?Long.parseLong(s)*-1:Long.parseLong(s);
if(num>Integer.MIN_VALUE&&num<Integer.MAX_VALUE){
return num.intValue();
}
return symbol==1?Integer.MIN_VALUE:Integer.MAX_VALUE;
}
}
return 0;
}
- 实现strStr()
public int strStr(String haystack, String needle) {
if (haystack.contains(needle)){
String newHayStack=haystack.replace(needle,"-");
return newHayStack.indexOf("-");
}
return -1;
}
- 报数
public String countAndSay(int n) {
if(n==1){
return "1";
}
String count=countAndSay(n-1);
StringBuffer s=new StringBuffer();
for (int i = 0; i <count.length() ;) {
int num=1;
for (int j = i+1; j < count.length(); j++) {
if(count.charAt(i)==count.charAt(j)){
num++;
}else {
break;
}
}
s.append(num);
s.append(count.charAt(i));
i+=num;
}
return s.toString();
}
- 最长公共前缀
public String longestCommonPrefix(String[] strs) {
if(strs.length==0){
return "";
}else if(strs.length==1){
return strs[0];
}
String same="";
same=strs[0];
for (int i = 1; i <strs.length ; i++) {
int size=0;
for (int j = 0; j < strs[i].length(); j++) {
if(j<same.length()&&same.charAt(j)==strs[i].charAt(j)){
size++;
}else {
break;
}
}
same=same.substring(0,size);
}
return same;
}