算法Day8 | Leetcode151.翻转字符串里的单词、右旋字符串(acm模式)、实现 strStr()

151.翻转字符串里的单词

力扣链接
在这里插入图片描述

思路

去掉多余的空白字符,拆分字符串,逆序拼接。还有种写法就是去掉多余空白字符,全反转,按每个空白字符划分再反转一次。

代码

class Solution {
    public String reverseWords(String s) {
        int i=0;
        String[] directory=new String[s.length()];
        int count=0;
        int left=0;
        while(i<s.length()){
            if(s.charAt(i)==' '){
                String temp=s.substring(left,i);
                if(left!=i){
                    directory[count]=temp;
                    left=i+1;
                    count++;
                }else{
                    while(i+1<s.length()&&s.charAt(i+1)==' ') i++;
                    left=i+1;
                }
            }
            i++;
        }
        if(left<s.length()&&i==s.length()){
            String temp=s.substring(left,i);
            directory[count]=temp;
            left=i+1;
            count++;
        }
        String result="";
        for(int j=count-1;j>=0;j--){
            result+=directory[j];
            if(j!=0){
                result+=" ";
            }
        }
        return result;
    }
}

右旋字符串(acm模式)

力扣链接
在这里插入图片描述

思路

跟链表的删除倒数第K个元素一个思想。先让fast指针走k步,slow和fast再一起走。记得输入的时候吞掉一个回车符。

代码

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int k=in.nextInt();
        in.nextLine();
        String s=in.nextLine();
        String result=change(s,k);
        System.out.println(result);
    }
    
    public static String change(String s,int k){
        String result="";
        int slow=0;
        int fast=0;
        while(fast<k){
            fast++;
        }
        while(fast<s.length()){
            fast++;
            slow++;
        }
        while(slow<s.length()){
            result+=s.charAt(slow);
            slow++;
        }
        if(k<s.length()){
            result+=s.substring(0,s.length()-k);
        }
        return result;
    }
}

实现 strStr()

思路

kmp

代码

class Solution {
    public int strStr(String haystack, String needle) {
        int[] next=new int[needle.length()];
        int j=0;
        next[0]=0;
        for(int i=1;i<needle.length();i++){
            while(j>0&&needle.charAt(i)!=needle.charAt(j)){
                j=next[j-1];
            }
            if(needle.charAt(i)==needle.charAt(j)){
                j++;
                next[i]=j;
            }
        }

        int k=0;
        for(int i=0;i<haystack.length();i++){
            while(k>0&&haystack.charAt(i)!=needle.charAt(k)){
                k=next[k-1];
            }
            if(haystack.charAt(i)==needle.charAt(k)){
                k++;
            }
            if(k==needle.length()){
                return i-k+1;
            }
        }

        return -1;
    }
}
//a a b a a a b a a a c
//a a b a a a c
//0 1 0 1 2 2 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值