Huawei interview hand-torn real questions [string compression]

        Use the number of repeated occurrences of characters to realize the basic string compression function. For example, the string aabcccccaaa becomes a2b1c5a3. If the "compressed" string is not shortened, return the original string. Enter only lowercase characters.

The topic is relatively simple, just use a variable to count, save the current count when encountering different characters, and then count again.

But for this question, you need to know what time complexity and space complexity are . After reading the code, ask you how much space complexity is, don't say it is O(n) stupidly, this will expose your programming literacy.

import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;

class Main {
    public static void main(String[] args) {
        // 处理输入
        Scanner in = new Scanner(System.in);
        String input_str = in.nextLine();
        
        System.out.println(compressString(input_str));
        return;
    }

    public static String compressString(String S) {
        if (S.length() == 0) { // 空串处理
            return S;
        }
        StringBuffer ans = new StringBuffer();
        int cnt = 1;
        char ch = S.charAt(0);
        boolean flag = true;
        for (int i = 1; i < S.length(); ++i) {
            if (ch == S.charAt(i)) {
                cnt++;
            } else {
                ans.append(ch);
                if (cnt != 1){
                    flag = false;
                }
                ans.append(cnt);
                ch = S.charAt(i);
                cnt = 1;
            }
        }
        ans.append(ch);
        ans.append(cnt);
        return flag ? S : ans.toString();
    }

}

 HUAWEI OD 2023/2022 latest machine test questions and explanations, 100% pass rate, high probability of getting the original questions!
Java: https://renjie.blog.csdn.net/article/details/127947829 
Python: https:// renjie.blog.csdn.net/article/details/127946125 
C++: https://renjie.blog.csdn.net/article/details/126965954 
Js: https://renjie.blog.csdn.net/article/details/ 128974467 
C: https://renjie.blog.csdn.net/article/details/129190260

HUAWEI OD 2023/2022 latest machine test questions and explanations, 100% pass rate, high probability of getting the original questions!
Java: https://renjie.blog.csdn.net/article/details/127947829 
Python: https:// renjie.blog.csdn.net/article/details/127946125 
C++: https://renjie.blog.csdn.net/article/details/126965954 
Js: https://renjie.blog.csdn.net/article/details/ 128974467 
C: https://renjie.blog.csdn.net/article/details/129190260

HUAWEI OD 2023/2022 latest machine test questions and explanations, 100% pass rate, high probability of getting the original questions!
Java: https://renjie.blog.csdn.net/article/details/127947829 
Python: https:// renjie.blog.csdn.net/article/details/127946125 
C++: https://renjie.blog.csdn.net/article/details/126965954 
Js: https://renjie.blog.csdn.net/article/details/ 128974467 
C: https://renjie.blog.csdn.net/article/details/129190260

おすすめ

転載: blog.csdn.net/misayaaaaa/article/details/130440383