首页 > 其他分享 >leetcode刷题day9|字符串部分(151.翻转字符串里的单词、卡码网:55.右旋转字符串)

leetcode刷题day9|字符串部分(151.翻转字符串里的单词、卡码网:55.右旋转字符串)

时间:2024-09-05 22:53:09浏览次数:7  
标签:151 卡码 ch end int StringBuilder start 字符串 sb

前言:字符串章节的第二部分,复习第一部分的知识加提升。

151.翻转字符串里的单词

链接:https://leetcode.cn/problems/reverse-words-in-a-string/
前言:不使用Java内部方法实现
思路:先去除字符串中的空格;将整个字符串反转;将单词再一次反转。
代码如下:

class Solution {
    public String reverseWords(String s) {
        //去除空格
        StringBuilder sb=removeSpace(s);
        //反转字符串
        sb=stringReverse(sb,0,sb.length()-1);
        //反转单词
        sb=wordReverse(sb);
        return sb.toString();
    }

    //去除空格
    public StringBuilder removeSpace(String s){
        int start=0,end=s.length()-1;
        while(s.charAt(start)==' '){
            start++;
        }
        while(s.charAt(end)==' '){
            end--;
        }
        StringBuilder sb =new StringBuilder();
        int i=0;
        while(start<=end){
            if(s.charAt(start)!=' ' || sb.charAt(sb.length()-1)!=' '){
                sb.append(s.charAt(start));
            }
            start++;
        }
        return sb;
    }

    //反转字符串
    public StringBuilder stringReverse(StringBuilder sb,int start,int end){
        while(start<end){
            char temp=sb.charAt(start);
            //setCharAt()第一个位置是索引,第二个是字符
            sb.setCharAt(start,sb.charAt(end));
            sb.setCharAt(end,temp);
            start++;
            end--;
        }
        return sb;
    }

    //反转单词
    public StringBuilder wordReverse(StringBuilder sb){
        int start=0,end=1;
        while(end<sb.length()){
        	//注意控制end不要越界
            while(end<sb.length() && sb.charAt(end)!=' '){
                end++;
            }
            stringReverse(sb,start,end-1);
            start=end+1;
            end=start+1;
        }
        return sb;
    }
}

知识积累:
setCharAt(int index, char ch)方法:StringBuilder 类提供了一个 setCharAt 方法,允许直接修改 StringBuilder 中的某个字符,适用于需要修改特定位置字符的情况。

卡码网:55.右旋转字符串

思路:将字符串分为两部分,先整体反转;再两两部分分别反转过来。
代码如下:

import java.util.Scanner;
public class Main{
    public static void main (String[] args) {
        Scanner in =new Scanner(System.in);
        int n=Integer.parseInt(in.nextLine());
        String s=in.nextLine();
        char[] ch=s.toCharArray();
        reverseString(ch,0,s.length()-1);
        reverseString(ch,0,n-1);
        reverseString(ch,n,s.length()-1);
        System.out.println(ch);
        
    }
    public static void reverseString(char[] ch,int start,int end){
        while(start<end){
            char temp=ch[start];
            ch[start]=ch[end];
            ch[end]=temp;
            start++;
            end--;
        }
        
    }
}

关于KMP的内容明天再补充。

标签:151,卡码,ch,end,int,StringBuilder,start,字符串,sb
From: https://blog.csdn.net/m0_51007517/article/details/141929881

相关文章

  • leetcode刷题day8|字符串部分(344.反转字符串、541. 反转字符串II)
    前言:字符串部分还是比较简单的344.反转字符串链接:https://leetcode.cn/problems/reverse-string/description/思路:这个比较简单,因为字符串也是数组类型的,用两个指针进行交换即可。代码如下:classSolution{publicvoidreverseString(char[]s){inti=0......
  • 2024.9.5 leetcode 3174 清除数字(字符串)
    题面3174.清除数字-力扣(LeetCode)题解:今天的每日一题比较简单,思路是遍历字符串,遇到第一个数字x的时候,把数字x和前面的字母y删除,也就是删除yx。1、为什么前面一定是字母,因为遇到的是第一个数字,前面不可能再有数字。2、如何实现删除yx,重新定义一个字符串,每一次遍历将y前面的字......
  • AT_arc151 题解 & 数组字典序大小比较求方案数
    很好的一题,做的时候没有一点思路,看了题解。看来做过的题目还是太少了,记录一下经验。注意到$1\leN\le2\times10^5$和$1\leM\le10^9$,如此庞大的数据,dp是肯定不行的。当字典序$A<B$时,当且仅当存在$i$,使得$\forallx\in[1,i)$,$A_x=B_x$且$A_i<B_i$。那么我们对于$......
  • 字符串拼接的几种形式
    字符串拼接的几种形式##一.算术运算符1.//+-*/%(取余)2.     intnum=10+10;//20      intnum1=10-10;//0      intnum2=10*10;//100      intnum3=10/10;//1      intnum4=10%......
  • 洛谷 P1516 青蛙的约会 题解
    一道简单的数学题~首先分析题意。精简得出:假设跳了\(t\)次,那么青蛙A的坐标是\((x+mt)\modL\),青蛙B的坐标是\((y+nt)\modL\),列出方程:\[x+mt\equivy+nt\pmodL\]由于余数具有可减性,所以把\(y+nt\)移到左边,得出:\[x-y+mt-nt\equiv0\pmodL\]写成人话:\[(x-y+mt-nt)\mod......
  • LeetCode 3174. 清除数字(字符串、模拟)
    题目:3174.清除数字思路:用字符串t模拟操作要求,当x是数字时,删除t的最后一个字符。不是的话,直接插入xclassSolution{public:stringclearDigits(strings){stringt="";for(autox:s){if('0'<=x&&x<='9'){......
  • 20240905_154516 python 填空题 字符串方法2
    有字符串列表li=["a","b","c"],让列表成员用+拼接,保存给变量rr="+".join(li)有字符串s,把它的内容变成小写,保存给变量rr=s.lower()有字符串s,把它内部的java全替换为python,保存结果给变量rr=s.replace("java","python")有字符串s="abc",请把它按空符号进行分割,得......
  • MySQL(四)日期函数 NULL函数 字符串函数
    日期函数函数描述NOW()返回当前的日期和时间CURDATE()返回当前的日期CURTIME()返回当前的时间DATE()提取日期或日期/时间表达式的日期部分EXTRACT()返回日期/时间的单独部分DATE_ADD()向日期添加指定的时间间隔DATE_SUB()从日期减去指定的时间......
  • 【时时三省】c语言例题----华为机试题<截取字符串>
    山不在高,有仙则名。水不在深,有龙则灵。                                    ----CSDN时时三省1,题目HJ46截取字符串描述输入一个字符串和一个整数k,截取字符串的前k个字符并输出数据范......
  • 20240905_144516 python 填空题 字符串方法1
    字符串s="hi",希望它占30个位置,居中,其它位置用!占位,结果用变量r记录r=s.center(30,"!")字符串s="abaac",需要统计s中有多少个a,把结果保存在变量i中i=s.count("a")有字符串s,需要把它转换为utf8的字节数据,把结果保存在变量b中b=s.encode()有字符串s,需要把它转换为gbk的字节......