Getting Started with the Blue Bridge Cup (Twenty-two) Reversed Characters (Unconventional Way)

Welcome ===Follow===Like===Comment, learn together, and make progress together!

------Continue to update the series of algorithm examples of the Lanqiao Cup entry-------

If you also like Java and algorithms, welcome to subscribe to the column to learn and communicate together!

Your likes, attention, and comments are the driving force for my creation!

-------I hope my article is helpful to you-------

Preface: Let's learn a simple string processing algorithm today, that is, a simple string reversal, and an advanced version! Easy to understand and easy to use! When writing technical articles, the most important thing is to let the audience understand, understand, and learn!

1. Topic description (1)

Write a function that reverses the input string. The input string is given as a character sarray .

Don't allocate extra space for another array, you have to modify the input array in-place , using O(1) extra space to solve this problem.

Example 1:

输入:s = ["h","e","l","l","o"]
输出:["o","l","l","e","h"]

Example 2:

输入:s = ["H","a","n","n","a","h"]
输出:["h","a","n","n","a","H"]

 2. Problem-solving ideas

1. Directly reverse the string, use double pointers, that is, you can exchange positions from the first left and the last right of the array, and then move towards each , and the two pointers can complete!

2. Therefore, the key lies in the exchange. C++ library functions have swap, but java does not, so we can only write a swap method by hand.

3. Bit operations are used here to implement swap!

3. Code implementation

  public void reverseString(char[] s) {
        int l = 0, r = s.length - 1;
        while (l < r) {
            s[l] ^= s[r];
            s[r] ^= s[l];
            s[l] ^= s[r];
            l++;
            r--;
        }
    }

 1. Topic description (2)

  Given a string sand an integer k, for 2kevery , reverse 2kthe kfirst the characters.

  • If kthere , reverse all remaining characters.
  • If the remaining characters are less than 2kbut greater than or equal to characters k, kthe first , and the remaining characters are left as is.

Example 1:

Input: s = "abcdefg", k = 2
 Output: "bacdfeg"

Example 2:

Input: s = "abcd", k = 2
 Output: "bacd"

2. Problem-solving ideas

1. This question is similar to question 1. It also needs to reverse the string, but it is a partial reversal .

2. This question is to reverse the first half of the first 2k characters , not all of them, pay attention!

3. Use the double-needle solution method to complete, that is, firstk is the position of the first k characters, and secondk is the position of the 2kth character.

4. We first reverse the first k strings , then add a new empty string , and then insert the second half of the string that does not need to be reversed .

5. When the remaining part is less than k, just add it to the end!

6. Note that the scope of the two pointers is less than length, and one moves k at a time , and the other moves 2k . When the two pointers meet, the loop ends!

3. Code implementation

  public  static String reverseStr(String s, int k) {
        int len=s.length();
        int start=0;//起始位置
        StringBuffer res=new StringBuffer();//目标动态字符串
        while (start<len){
            StringBuffer sb=new StringBuffer();//临时字符串,每次都会改变
            int first=(start+k)>len?len:start+k;//三目运算符判断当前指针位置
            int second=(start+2*k)>len?len:start+2*k;

            sb.append(s.substring(start,first));//前一部分加入
            res.append(sb.reverse());//反转后再加入目标字符串
            if (first<second){
                res.append(s.substring(first,second));//无需反转直接加入
            }
            start+=2*k;
        }
        return  res.toString();
    }

 

It's not easy to post, I implore the big guys to raise your hands!


Likes: Likes are a kind of virtue, and they are the recognition of my creation by the bosses!


Comments: There is no white contact, it is the beginning of communication between you and me!


Collection: May you pick more, it is the appreciation of the bosses to me!

Guess you like

Origin blog.csdn.net/m0_55278347/article/details/129231572