首页 > 其他分享 >Leetcode刷题第三周

Leetcode刷题第三周

时间:2022-11-13 19:44:06浏览次数:75  
标签:String charAt int 第三周 next length str Leetcode 刷题

字符串:

反转字符串

344

class Solution {
    public void reverseString(char[] s) {
        // 左右指针
        int leftNode = 0;
        int rifhtNode = s.length - 1;
        char temp;
        while(leftNode <= rifhtNode){
            temp = s[rifhtNode];
            s[rifhtNode] = s[leftNode];
            s[leftNode] = temp;
            leftNode++;
            rifhtNode--;
        }
    }
}

反转字符串 II

541

class Solution {
    public String reverseStr(String s, int k) {
        int count = 0;
        StringBuffer target = new StringBuffer();
        for(int i = 0; i < s.length(); i++){
            count++;
            if(count == 2*k){
                target.append(reverse(s.substring(i+1-2*k,i+1-k)));
                target.append(s.substring(i+1-k,i+1));
                count = 0;
            }
        }
        if(count < k){
            target.append(reverse(s.substring(s.length() - count,s.length())));
        }else if(count >= k && count < 2*k){
            target.append(reverse(s.substring(s.length() - count,s.length()- count + k)));
            target.append(s.substring(s.length() - count+k,s.length()));
        }
        return new String(target);
    }
    public String reverse(String s){
        char[] t = s.toCharArray();
        int leftNode = 0;
        int rifhtNode = t.length - 1;
        char temp;
        while(leftNode <= rifhtNode){
            temp = t[rifhtNode];
            t[rifhtNode] = t[leftNode];
            t[leftNode] = temp;
            leftNode++;
            rifhtNode--;
        }
        return new String(t);
    }
}

替换空格

offer 05

class Solution {
    public String replaceSpace(String s) {
        StringBuffer target = new StringBuffer();
        char temp;
        for(int i = 0; i < s.length(); i++){
            temp = s.charAt(i);
            if(temp == ' '){
                target.append("%20");
            }else{
                target.append(temp);
            }
        }
        return new String(target);
    }
}

反转字符串中的单词

151

class Solution {
    public String reverseWords(String s) {
        StringBuffer str = new StringBuffer();
        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i) != ' '){
                int index = i;
                while(i < s.length() && s.charAt(i) != ' '){
                    i++;
                }
                str.insert(0,s.substring(index, i));
                if(i <  s.length() - 1){
                    str.insert(0," ");
                }
            }
        }
        if(str.charAt(0) == ' '){
            str.delete(0,1);
        }
        return new String(str);
    }
}

左旋转字符串

Offer 58 - II

class Solution {
    public String reverseLeftWords(String s, int n) {
// 先整体反转,在根据k进行部分反转
        char[] str = s.toCharArray();
        reverse(str, 0, str.length - 1);
        reverse(str, 0, str.length - 1 - n);
        reverse(str, str.length - n, str.length - 1);
        return new String(str);
    }
    public void reverse(char[] str, int start, int end){
        while(start < end){
            str[start] ^= str[end];
            str[end] ^= str[start];
            str[start] ^= str[end];
            start++;
            end--;
        }
    }
}

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

28

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

重复的子字符串

459

class Solution {
    public boolean repeatedSubstringPattern(String s) {
        int[] next = new int[s.length()];
        next[0] = 0;
        for(int i = 1, j = 0; i < s.length(); i++){
            while(j > 0 && s.charAt(i) != s.charAt(j)){
                j = next[j - 1];
            }
            if(s.charAt(i) == s.charAt(j)){
                j++;
            }
            next[i] = j;
        }
        if(next[next.length - 1] != 0 && next.length%(next.length - next[next.length - 1]) == 0){
            return true;
        }
        return false;
    }
}

标签:String,charAt,int,第三周,next,length,str,Leetcode,刷题
From: https://www.cnblogs.com/noviceprogrammeroo/p/16866350.html

相关文章

  • #yyds干货盘点# 前端歌谣的刷题之路-第一百六十六题-instanceOf
     前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了......
  • #yyds干货盘点# 前端歌谣的刷题之路-第一百六十七题-Array.map
     前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了......
  • leetcode 5. 最长回文子串
    给你一个字符串 s,找到 s 中最长的回文子串。 示例1:输入:s="babad"输出:"bab"解释:"aba"同样是符合题意的答案。示例2:输入:s="cbbd"输出:"bb" 提示:1<......
  • 剑指 Offer 41. 数据流中的中位数 - 力扣(Leetcode)
    剑指Offer41.数据流中的中位数-力扣(Leetcode)分析维护两个堆,一个大根堆,一个小根堆。插入操作:当进行插入时,先判断大根堆中是否有元素,如果没有直接插入大根堆,若有......
  • java刷题中常见api记忆
      转自:https://blog.csdn.net/lijiaming_99/article/details/117738526......
  • 剑指 Offer 59 - I. 滑动窗口的最大值 - 力扣(Leetcode)
    剑指Offer59-I.滑动窗口的最大值-力扣(Leetcode)一.分析方法一:数组长度为1e5,k的大小为1e4,因此直接暴力计算会TLE。我们可以思考一个更复杂的问题:询问任意区间中的......
  • [回溯算法]leetcode40. 组合总和 II(c实现)
    题目给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的每个数字在每个组合中......
  • leetcode 647. 回文子串 js实现
    给你一个字符串 s ,请你统计并返回这个字符串中 回文子串 的数目。回文字符串 是正着读和倒过来读一样的字符串。子字符串 是字符串中的由连续字符组成的一个序列。......
  • LeetCode 665. 非递减数列
    classSolution{public:boolcheckPossibility(vector<int>&nums){intn=nums.size();for(inti=0;i<n-1;i++){intx=nums[i......
  • LeetCode刷题记录.Day13
    四数之和18.四数之和-力扣(LeetCode)classSolution{public:vector<vector<int>>fourSum(vector<int>&nums,inttarget){vector<vector<int>>res......