当前位置:网站首页>Leecode brush question record sword finger offer 58 - ii Rotate string left

Leecode brush question record sword finger offer 58 - ii Rotate string left

2022-07-07 00:12:00 Why is there a bug list

List of articles

topic

The left rotation operation of string is to transfer several characters in front of string to the end of string . Please define a function to implement the left rotation operation of string . such as , Input string "abcdefg" And number 2, This function will return the result of rotating two bits to the left "cdefgab".

Example 1:

Input : s = “abcdefg”, k = 2
Output : “cdefgab”
Example 2:

Input : s = “lrloseumgh”, k = 6
Output : “umghlrlose”

Limit :

1 <= k < s.length <= 10000

answer

class Solution {
    
     public String reverseLeftWords(String s, int n) {
    
        char[] chars=s.toCharArray();
        char[] tmpchars=new char[n];
        int i=0;
        for(; i< tmpchars.length; i++)
        {
    
            tmpchars[i]=chars[i];
        }
        for(int j=n;j<s.length();j++)
        {
    
            chars[j-n]=chars[j];
        }
        i=0;
        for(int k=s.length()-n;k<s.length();k++)
        {
    
            chars[k]=tmpchars[i];
            i++;
        }
        return new String(chars);
    }
}
原网站

版权声明
本文为[Why is there a bug list]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131017364992.html