*LeetCode--Reverse String II (自己的做法很笨)

Reverse String II

自己的解法:

就是用一个StringBuffer来进行字符串的接收,利用一个指针来指示当前是哪一个k部分,当 i + k < s.length() 时,证明从 i 到 i + k - 1部分是可以进行反转的,而从i + k 部分到 i + 2 * k部分是直接进行拼接,利用条件 j < s.length()即可进行限制,不过对于最后剩余的一小部分如果是需要反转的,那么就不能使用上面反转时使用的条件 : 从j = i + k - 1 ,而是从 j = s.length() - 1开始到  i 结束

class Solution {
    public String reverseStr(String s, int k) {
        if(s == null || s.length() == 0 || k == 0) return s;
        StringBuffer str = new StringBuffer();
        int i = 0;
        while(i + k < s.length()){
            for(int j = i + k - 1; j >= i && j < s.length(); j--){
                str.append(s.charAt(j));
            }
            i += k;
            for(int j = i; j < i + k && j < s.length(); j++){
                str.append(s.charAt(j));
            }
            i += k;
        }

        if(i + k >= s.length()){
            for(int j = s.length() - 1; j >= i; j--){
                str.append(s.charAt(j));
            }
        }

        return str.toString();
    }
}

  

 public String reverseStr(String s, int k) {
        if(s == null || s.isEmpty()){return s;}
        StringBuilder sb = new StringBuilder();
        boolean reverse = true;
        for(int i = 0; i < s.length(); i = i + k, reverse = !reverse){
            int end = (i + k) < s.length() ? (i + k - 1) : s.length()-1;
            if(reverse)
                for(int j = end; j >= i; j--){sb.append(s.charAt(j));} // Copy in reverse
            else
                for(int j = i; j <= end; j++){sb.append(s.charAt(j));} // Copy as it is
        }
        return sb.toString();
    }

  

public String reverseStr(String s, int k) {
        char[] ch = s.toCharArray();
        for (int i = 0; i < ch.length; i += 2 * k) {
            rev(ch, i, i + k);
        }
        return String.valueOf(ch);
    }

    private void rev(char[] ch, int i, int j) {
        j = Math.min(ch.length, j) - 1;
        for (; i < j; i++, j--) {
            char tmp = ch[i];
            ch[i] = ch[j];
            ch[j] = tmp;
        }
    }

  

原文地址:https://www.cnblogs.com/SkyeAngel/p/9060718.html

时间: 2024-10-30 16:18:08

*LeetCode--Reverse String II (自己的做法很笨)的相关文章

[LeetCode] Reverse String II

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or eq

[LeetCode] 344 Reverse String &amp; 541 Reverse String II

原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse String II: https://leetcode.com/problems/reverse-string-ii/description/ 题目&解法: 1.Reverse String: Write a function that takes a string as input and returns

[LeetCode] Reverse String 翻转字符串

Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 这道题没什么难度,直接从两头往中间走,同时交换两边的字符即可,参见代码如下: 解法一: class Solution { public: string reverseString(string s) { int left =

541. Reverse String II

题目 : Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than

[Algorithm] Reverse String II

给定一个字符串,要求把字符串前面的若干字符移动到字符串尾部. 解法一:蛮力法 首先想考虑将一个字符移到尾部的方法.代码如下: void LeftShiftOne(char* s, int n) { char t = s[0]; for (int i = 1; i != n; i++) s[i - 1] = s[i]; s[n - 1] = t; } 如果要移动m个字符串,则依次对函数LeftShiftOne执行m次即可.代码如下: void LeftRotateString(char* s, i

LeetCode Reverse Words in a String III

原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Inpu

[LeetCode] Reverse Words in a String II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain leading or trailing spaces and the words are always separated by a single space.For example,Given s = "t

Leetcode:Reverse Linked List II 反转链表区间

Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given   1->2->3->4->5->NULL,  m = 2 and n = 4, return  1->4->3->2->5->NULL. Note:Given m, n satisfy the following

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2&q