复习数据结构(2023.3.8)

leetcode 151. 反转字符串中的单词

题目描述:

给你一个字符串 s ,请你反转字符串中 单词 的顺序。

单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。

返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。

注意:输入字符串 s中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。

class Solution {
    public String reverseWords(String s) {

        //1.去除多余空格,包括字符串开头、结尾和字符串中间的连续空格
        StringBuilder sb = removeSpace(s);
        //2.对整个字符串进行反转
        reverseString(sb,0,sb.length()-1);
        //3.对字符串中的每个单次进行反转
        reverseEachWord(sb);
        return sb.toString();
    }
    public StringBuilder removeSpace(String s){
        int start = 0;
        int end = s.length() - 1;
        while(s.charAt(start) == ' ')start++;//去重开头的空格
        while(s.charAt(end) == ' ') end--;//去除末尾的空格
        StringBuilder sb = new StringBuilder();
        //去除字符串中间连续空格
        while(start <= end){
            if(s.charAt(start) != ' ' || sb.charAt(sb.length() - 1) != ' '){
                sb.append(s.charAt(start));
            }
            start++;

        }
        return sb;
    }
    //反转字符串
    public  void reverseString(StringBuilder sb,int start,int end){
        while(start < end){
            char temp = sb.charAt(start);
            sb.setCharAt(start,sb.charAt(end));
            sb.setCharAt(end,temp);
            start++;
            end--;
        }
        
    }
    //反转字符串中的每个单词
    public void reverseEachWord(StringBuilder sb){
        int start = 0;
        int end = 1;
        int n = sb.length();
        while(start < n){
            while(end < n && sb.charAt(end) != ' '){
                end++;
            }
            reverseString(sb,start,end-1);
            start = end + 1;
            end = start + 1;
        }


    }
}

leetcode 剑指 Offer 58 - II. 左旋转字符串

题目描述:

字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。

class Solution {
    public String reverseLeftWords(String s, int n) {
        StringBuilder sb = new StringBuilder();
        int start = n;
        //先拼接后面的
        while(start < s.length()){
            sb.append(s.charAt(start));
            start++;
        }
        //再拼接前面的
        int index = 0;
        while(index < n){
            sb.append(s.charAt(index));
            index++;
        }
        return sb.toString();
    }
}

leetcode 28. 找出字符串中第一个匹配项的下标

题目描述:

给你两个字符串 haystackneedle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1

class Solution {
    public int strStr(String haystack, String needle) {
        int start = 0;
        // haystack ='a' needle = 'a'
        while(start < haystack.length()-needle.length()+1){
            int index = 0;
            while(index < needle.length()){
                if(haystack.charAt(start+index) != needle.charAt(index)){
                    break;
                }
                if(index == needle.length() - 1){
                    return start;
                }
                index++;
            }
            start++;
        }
        return -1;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何必,去在意

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值