Interview algorithm questions for Baidu, Ali, Tencent, JD, etc.

Today I will share with you the string-related algorithm interview questions. Now when you enter a big factory, you will have algorithm interview questions, but because algorithms and data structures have a certain threshold, it is a feasible strategy to cross this threshold, accumulate slowly and then look at it over and over again.


This question is the 151st question on Leetcode: Reverse words in a string
image

 

The idea of ​​solving this problem:

  • Clear the useless spaces in the string, 【are you ok  organize "   " into " are you ok”

  • Then reverse the processed string, [organize "are you ok" into "ko uoy era"]

  • Flip each word separately, [change ko to ok, uoy to you, and era to are]

  • The final result is "ok you are"  sorting out: 

             1. Clear spaces

             2. The entire string is reversed

             3. Then flip each word

 

Goal 1. Eliminate useless spaces in strings swift 输入:s = " are you ok "

 输出:"are you ok" Requirement: The useless spaces include the spaces in the first, middle, and last positions

image    

Ideas: 

        1. Traverse the string array, if it is not " ", add it to the array
        2. If it is " ", judge whether it is also empty before, if it is, skip it, if it is not, it will be " "added to the array. [Because there may be multiple spaces between two words, but after processing, only one space can appear, so it is necessary to judge whether it is also empty before  ]

 func clearSpace(str: String) -> [Character] {
        var chars = [Character]()
        var space = true
        for c in str {
            if c != " " {
                chars += "\(c)"
                space = false
            } else if space == false {
                chars += " "
                space = true
            }
        }
        return chars
    }

 

 The function of the space in the code is to realize the judgment logic of [Idea 2]. We just use

s = "  are   you ok "

Let's go through the code as an example.

  • Start is  " ", so do nothing, continue to execute until it traverses to'a'

  • c = a, so it is added to the chars array, and the space is marked as false, (the meaning of this mark is: when traversing to r, we know that the last one is not a space at the end according to the value of space = false). Continue execution until traversal to'e'

  • Next, c =  " ", but space = false, so chars +=  " ", then space is marked as true; continue execution, at this time c =  " ", according to the code, no operation is performed. Continue execution until c = y, and return to the first step.

 

The more important point here is that the initial value of  space is true.

The following is the implementation code of java:

public String clearSpace(String s) {
      if (s == null) return "";
        char[] chars = s.toCharArray();
    boolean space = true;
    for (int i = 0; i < chars.length; i++) {
        if (chars[i] != ' ') { // chars[i]是非空格字符
            chars[cur++] = chars[i];
            space = false;
        } else if (space == false) { // chars[i]是空格字符,chars[i - 1]是非空格字符
            chars[cur++] = ' ';
            space = true;
        }
    }
}

 

Goal 2. Flip

This is very simple, the code is as follows:

func reverseString(_ chars: inout [Character], _ l: inout Int, _ r: inout Int) {
        r -= 1
        while l < r {
            let c = chars[l]
            chars[l] = chars[r]
            chars[r] = c
            l += 1
        }
    }

The meaning of this code is to organize ["are you ok" into "ko uoy era"]

 

Overall realization:

class Solution {
    func reverseWords(_ s: String) -> String {
        var chars = [Character]()
        var space = true 
        for c in s {
            if c != " " {
                chars += "\(c)"
                space = false 
            } else if space == false {
                chars += " " 
                space = true 
            }
        }
        if space == true {
            chars.removeLast()
        }
        let count = chars.count
        reverseString(&chars, 0, count)
        var preIdx = -1
        for i in 0..<count {
            if chars[i] == " " {
                reverseString(&chars, preIdx+1, i)
                preIdx = i 
            }
        }
        reverseString(&chars, preIdx+1, count)
        var str = ""
        for c in chars {
            str += "\(c)"
        }
        return str
    }

    func reverseString(_ chars: inout [Character], _ l: Int, _ r: Int) {
        var b = l 
        var e = r - 1
        while b < e {
            let c = chars[b]
            chars[b] = chars[e]
            chars[e] = c
            b += 1
            e -= 1
        }
    }
}

 

to sum up:

The problem-solving idea I learned today is that an algorithm problem can be decomposed into sub-problems, and then assembled after processing the sub-problems, the problem can be solved. If you want to pass the interview, you still have to do the questions and accumulate solutions to some problems. Well, I will share so much with you today, hoping to help you all.

Welcome to pay attention to [The Way of Infinite Testing] public account , reply [receive resources],
Python programming learning resources dry goods,
Python+Appium framework APP UI automation,
Python+Selenium framework Web UI automation,
Python+Unittest framework API automation,

Resources and codes are sent for free~
There is a QR code of the official account at the bottom of the article, you can just scan it on WeChat and follow it.

Remarks: My personal public account has been officially opened, dedicated to the sharing of test technology, including: big data testing, functional testing, test development, API interface automation, test operation and maintenance, UI automation testing, etc., WeChat search public account: "Infinite The Way of Testing", or scan the QR code below:

 Add attention and let us grow together!

 

Guess you like

Origin blog.csdn.net/weixin_41754309/article/details/113093783
Recommended