Java实现Leetcode题目(字符串)

2023-12-13 19:21:32

LeetCode344(翻转字符串)

package str;

public class LeetCode344 {
	public static void main(String[] args) {
		char[] nums = {'h','e','l','l','o'};
		nums = reverse(nums);
		System.out.print(nums);
	}
	
	public static char[] reverse(char[] charArrays) {
		int x = 0;
		int j = charArrays.length-1;
		while(x<j) {
			char temp = charArrays[x];
			charArrays[x] = charArrays[j];
			charArrays[j]= temp;
			x++;
			j--;
		}
		return charArrays;
	}

}

?LeetCode541(翻转字符串||)

package str;

public class LeetCode541 {
	public static void main(String[] args) {
		System.out.print(reverse(2,"abcdefg"));
	}
	
	public static String reverse(int k,String c) {
		char[] nums = c.toCharArray();
		
		for(int i=0;i<nums.length;i+=(2*k)) {
			int start = i;
			int end = Math.min(nums.length-1, start+k-1);
			while(end > start) {
				char temp = nums[start];
				nums[start] = nums[end];
				nums[end] = temp;
				end--;
				start++;
			}
		}
		return  new String(nums);
	}
}

替换数字 (题目页面 (kamacoder.com)

package str;

import java.util.Scanner;

public class cardCode {
	public static void main(String[] agrs) {
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine();
		
		String newStr = insertNumber(str);
		System.out.print(newStr);
		scanner.close();
	}
	public static String insertNumber(String str) {
		StringBuilder newStr = new StringBuilder();
		
		for(int i =0;i<str.length();i++) {
			if(Character.isDigit(str.charAt(i))) {
				newStr.append("number");
			}else {
				newStr.append(str.charAt(i));
			}
		}
		return newStr.toString();
		
	}
}

LeetCode15(反转字符串里面的单词)

package str;

import java.util.Arrays;

public class Leetcode151 {

	public static void main(String[] args) {
		String  str = "the sky is blue";
		//String newStr = reverseStr(str);
		//System.out.print(newStr);
		System.out.print(finalWay(str));
		
	}
	public static String finalWay(String str) {
		char[] c = str.toCharArray();
		c = removeSpace(c);
		reverse(c,0,c.length-1);
		reverseWord(c);
		return new String(c);
	}
	//去除多余空格
	public static char[] removeSpace(char[] chars) {
		int slow=0;
		for(int i=0;i<chars.length;i++) {
			if(chars[i]!=' ') {
				if(slow!=0) {
					chars[slow++]=' ';
				} 
				while(i<chars.length&&chars[i]!=' ') {
					chars[slow++]=chars[i++];
				}
			}
		}
		
		char[] newChars = new char[slow];
		newChars = Arrays.copyOf(chars, slow);
		return newChars;
	}
	//字符串全翻转
    //双指针实现指定范围内字符串反转,可参考字符串反转题解
    public static void reverse(char[] chars, int left, int right) {
        if (right >= chars.length) {
            System.out.println("set a wrong right");
            return;
        }
        while (left < right) {
            chars[left] ^= chars[right];
            chars[right] ^= chars[left];
            chars[left] ^= chars[right];
            left++;
            right--;
        }
    }
	//反转单词
	public static void reverseWord(char[] chars) {
		int start = 0;
		for(int end =0;end<chars.length;end++) {
			if(end == chars.length-1) {
				reverse(chars,start,end);
				start = end+1;
			}
			
			if(chars[end]==' ') {
				
				reverse(chars,start,end-1);
				start = end+1;
			}
		}
	}

	
	//自己的方法(比较笨的)
	public static String reverseStr(String str) {
		String[] strArrays = str.split("\\s+");//\\s表示匹配任何空白字符,+表示匹配一次或多次
		int right = 0;
		int left = strArrays.length-1;
		while(left>right) {
			String temp = strArrays[right];
			strArrays[right] = strArrays[left];
			strArrays[left] = temp;
			right++;
			left--;
		}
		
		StringBuilder newStr = new StringBuilder();
		for(String i:strArrays) {
			newStr.append(i);
			newStr.append(" ");
		}
		return new String(newStr).trim();
	}
	
	

}

?右旋转字符串(题目页面 (kamacoder.com)

package str;

public class cardCode01 {
	public static void main(String[] args) {
		String str = reverse("abcdefg",2);
		System.out.print(str);
	}
	
	public static String reverse(String str,int n ) {
		char[] chars = str.toCharArray();
		reverseArrays(chars,0,chars.length-1);
		reverseArrays(chars,0,n-1);
		reverseArrays(chars,n,chars.length-1);
		return new String(chars);
		
	}
	
	public static void reverseArrays(char[] nums,int left,int right) {
		if(right>nums.length-1||left<0) {
			System.out.print("输入错误");
			return; 
		}
		while(right>left) {
			char temp = nums[right];
			nums[right] = nums[left];
			nums[left] = temp;
			left++;
			right--;
		}
	}
}

Leetcode28(?实现 strStr())?

package str;

public class LeetCode28 {

	public static void main(String[] args) {
		
	}
	
	public static void getNext(int[] next,String str) {
		int j = -1;
		next[0] = j;
		for(int i = 1;i<str.length();i++) {
			while(j>=0&&str.charAt(i)!=str.charAt(j+1)) {
				j = next[j];
			}
			if(str.charAt(i)==str.charAt(j+1)) {
				j++;
			}
			next[i]=j;
		}
	}
	public static int strStr(String haystack,String needle) {
		if(needle.length()==0) {
			return 0;
		}
		int[] next = new int[needle.length()];
		getNext(next,needle);
		int j =-1;
		for(int i =0;i<haystack.length();i++) {
			while(j>=0&&haystack.charAt(i)!=needle.charAt(j+1)) {
				j = next[j];
			}
			if(haystack.charAt(i)==needle.charAt(j+1)) {
				j++;
			}
			if(j==needle.length()-1) {
				return (i-needle.length()+1);
			}
		}
		return -1;
		
	}

}

Leetcode459(重复的子字符串)

package str;

public class Leetcode459 {
	public static void main(String[] args) {
		System.out.print(findWay("aba"));
	}
	
	//移动匹配
	public static boolean findWay(String str) {
		StringBuffer newStr = new StringBuffer(str);
		newStr.append(str);
		newStr.deleteCharAt(0);
		newStr.deleteCharAt(newStr.length()-1);
		if(newStr.indexOf(str)!=-1) {
			return true;
		}else {
			return false;
		}
	}
	//KMP
	public static void getNext(int[] next,String str) {
		int j=-1;
		next[0] = j;
		for(int i =1;i<str.length();i++) {
			while(j>=0&&str.charAt(j+1)!=str.charAt(i)) {
				j = next[j];
			}
			if(str.charAt(j+1)==str.charAt(i)) {
				j++;
			}
			next[i]=j;
		}
	}
	
	public static boolean finfWay02(String str) {
		if(str.length()==0) {
			return false;
		}
		int[] next = new int[str.length()];
		getNext(next,str);
		int length = str.length();
		if(next[length-1]!=-1&&(length%(length-(next[length-1]+1))==0)) { //因为都统一减一了,所以要加回来
			return true;
		}
		return false;
		
	}
}

Leetcode03(无重复子串)

package str;

import java.util.HashSet;
import java.util.Set;

public class LeetCode03 {
	public static void main(String[] args) {
		System.out.print(findWay("abcdbc"));
	}
	
	public static int findWay(String str) {
		Set set = new HashSet();
		int n = str.length();
		int Max = 0;
		int i=0,j=0;
		while(i<n&&j<n) {
			if(!(set.contains(str.charAt(j)))) {
				set.add(str.charAt(j));
				j++;
				Max = Math.max(Max, j-i);
			}else {
				set.remove(str.charAt(i));
				i++;
			}
		}
		return Max;
		
	}
}

文章来源:https://blog.csdn.net/weixin_47059164/article/details/134895424
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。