Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit series activities – click on the task to see the details of the activities.

Hello everyone, I am quick-frozen fish 🐟, a water front 💦, like the garish 💐, continuous sand sculpture 🌲 Welcome friends to add my wechat: Sudongyuer pull you into the group of my public number: front-end quick-frozen fish progress together, looking forward to growing together with everyone

Preface 🌧 ️

Algorithms are unfamiliar and familiar to the front-end people, and often we don’t value them as much as the back-end engineers do. But in fact, algorithms have an unshakable position for every programmer.

Because the development process is to convert the actual problem into the computer can recognize the instructions, that is, “data structure” said, “design the data structure, in the application of algorithms on the line”.

The quality of writing instructions will directly affect the performance of the program, and instructions are composed of data structure and algorithm, so the design of data structure and algorithm basically determines the quality of the final program.

The title 🦀

541. Reverse string II

Difficulty is simple

Given a string s and an integer k, invert the first K characters of the 2k character for every 2k character count from the beginning of the string.

  • If there are less than k characters left, all the remaining characters are reversed.

  • If the remaining characters are less than 2K but greater than or equal to K, the first K characters are reversed and the remaining characters are left as is.

Example 1:

Input: s = "abcdefg", k = 2 Output: "bacdfeg"Copy the code

Example 2:

Input: s = "abcd", k = 2Copy the code

Tip:

  • 1 <= s.length <= 104
  • sConsists of lowercase English only
  • 1 <= k <= 104

🌵

  • This problem, first of all, the problem is complicated, it is literally translated into English, in a popular way, every k inversion k, not enough at the end of the k reversal

  • Split into arrays for easy operation

  • Before each exchange, determine the left and right Pointers for exchange

How to solve the problem 🐂

  • Split splits into arrays
  • Determine the left and right Pointers when switching
  • It is suggested to take a piece of paper and simply draw a picture to determine the boundary of the pointer
  • Finally join the return

Source 🔥

/ * * *@param {string} s
 * @param {number} k
 * @return {string}* /
var reverseStr = function(s, k) {
    s=s.split(' ')

    for(let i=0; i<s.length; i+=2*k){
        let l = i-1
        // Determine the right pointer when switching
        letr = i+k>s.length? s.length:i+kwhile(++l<--r){
            [s[l],s[r]]=[s[r],s[l]]
        }
    }

    return s.join("")};Copy the code

Time complexity :O(n)

Space complexity :O(1)

Conclusion 🌞

So fish fish LeetCode algorithm article “LeetCode” 541 – reverse string | | ⚡ ️ ended, algorithm this thing no shortcuts, only more writing practice more, more, actually very simple, the purpose of the article is to push myself to accomplish algorithm practice and summarize and output, food not food is not important, but love 🔥. I hope everyone can like my essay, and I also hope to know more like-minded friends through the article. If you also like to toss, welcome to add my friend, sand sculpture together, together progress.

Making 🤖 : sudongyu

Personal blog 👨💻: Frozen fish blog

Vx 👦 : sudongyuer

Write in the last

Guys, if you like my words, give 🐟🐟 a thumbs up 👍 or follow ➕ to support me the most.