代码随想录算法刷题训练营

代码随想录算法刷题训练营day08:LeetCode(344)反转字符串、LeetCode(541)反转字符串II、卡码网(54)替换数字、LeetCode(151)翻转字符串里的单词、卡码网(55)右旋字符串

LeetCode(344)反转字符串
题目
代码随想录算法刷题训练营_第1张图片代码

class Solution {
    public void reverseString(char[] s) {
        for (int i = 0; i < s.length/2; i++) {
            char temp=s[i];
            s[i]=s[s.length-1-i];
            s[s.length-1-i]=temp;    
        }

    }
}

LeetCode(541)反转字符串II
题目
代码随想录算法刷题训练营_第2张图片
代码

class Solution {
    public String reverseStr(String s, int k) {
        
        //逐步分析每一种情况
        char[] data=s.toCharArray();//将字符串转化为数组
        //字符串长度小于k
        if(data.length<k){
            for (int i = 0; i < data.length/2; i++) {
                char temp3=data[i];
                data[i]=data[data.length-1-i];
                data[data.length-1-i]=temp3;
            }
        }else if (data.length>=k&&data.length<2*k) {
            //字符串长度大于等于k小于2k
            for (int j = 0; j < k/2; j++) {
                char temp4=data[j];
                data[j]=data[k-1-j];
                data[k-1-j]=temp4;    
            }    
        }else{
            //字符串长度大于2k
            int lengthS=data.length;
            int flag=0;
            while(lengthS>=2*k){
                for(int i=flag,j=0;i<flag+k/2;i++,j++){
                    char temp=data[i];
                    data[i]=data[flag+k-1-j];
                    data[flag+k-1-j]=temp;
                }
                lengthS=lengthS-2*k;
                flag=flag+2*k;
            }
            if(lengthS>=k&&lengthS<2*k){
                for(int i=flag,j=0;i<flag+k/2;i++,j++){
                    char temp1=data[i];
                    data[i]=data[flag+k-1-j];
                    data[flag+k-1-j]=temp1;
                }
            }else if(lengthS<k&&lengthS>0){
                for(int i=flag,j=0;i<flag+(data.length-flag+1)/2;i++,j++){
                    char temp2=data[i];
                    data[i]=data[data.length-1-j];
                    data[data.length-1-j]=temp2;

                }
            }else{}
            
        } 
        String s1 = String.valueOf(data);
        return s1; 
    }
}

卡码网(54)替换数字
题目
代码随想录算法刷题训练营_第3张图片
代码

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s=scanner.next();
        StringBuilder data=new StringBuilder();//处理字符串
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)>='a'&&s.charAt(i)<='z'){
                data.append(s.charAt(i));
            }else{
                data.append("number");
            }
        }
        String s1=data.toString();
        System.out.println(s1);

    }
}

LeetCode(151)翻转字符串里的单词
题目
代码随想录算法刷题训练营_第4张图片
代码

class Solution {
    public String reverseWords(String s) {
        String s1=s.trim();//去除首尾空格
        String[] data=s1.split(" ");//转成字符串数组
        StringBuilder s2=new StringBuilder();
        for (int i = data.length-1; i>=0; i--) {
            if(data[i]!=""){
                s2.append(data[i]);//拼接并进行判断
                if(i!=0){
                    s2.append(" ");
                }
            }   
        }
        return s2.toString();

    }
}

卡码网(55)右旋字符串
题目
代码随想录算法刷题训练营_第5张图片
代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder s1=new StringBuilder();
        int target = scanner.nextInt();
        String data = scanner.next();
        for (int i = 0; i < target; i++) {
            s1.append(data.charAt(data.length()-target+i));
        }
        for (int j = 0; j < data.length()-target; j++) {
            s1.append(data.charAt(j));
        }
        String result=s1.toString();
        System.out.println(result);
    }
}

你可能感兴趣的:(代码随想录算法训练营,算法,java,开发语言,leetcode,数据结构,vscode)