# # # introduction

Today, I read the news in the group and suddenly saw a buddy sent a topic about the algorithm. At first glance, it seems pretty simple. Some friends in the group said to use map to do, and then I tried to use map to write, found that fell into the trap of thinking, just can’t get out. Feel their logical thinking there is lack of, so summed up a wave.

# # #

  • Stroll the test questions square of bovine guest net, also discovered the existence of this one problem.

  • When browsing Zhihu, I found a thread around the topic of IT companies to apply for fresh graduates of what is the standard. See a big guy’s answer to have the existence of this problem, visible the importance of this problem. Crap doesn’t say much, stickers.

To tell the truth, my foundation is not particularly good, do this problem is still a little difficult, mainly fell into the trap of thinking, although some Java theory can be full of train ran out, but really encountered a little flexible algorithm, estimate GG.


###

  • Start by defining a variable element, which defaults to the first character of the string. Use element as the base element to determine if a character is repeated. Then define a variable count to count the number of repetitions. The initial value should be 1.

  • We then iterate through the string, starting with the character in position 1. If the current character is equal to element, that character is repeated in the current string. So count should increment itself.

  • If the current character is not equal to element, it indicates that the number of consecutive repeats of the previous character has been calculated and can be concatenated. Now we can manipulate the current character and restore count to its initial value of 1.

  • At the end of traversal, a splicing operation is performed. Why not do the concatenation in step 2? Because the number of repetitions in the entire string has not been counted, if the number of repetitions is recorded, the operation of concatenation will not achieve the desired effect. If the string is “PPPPP”, if it does, the output will be “p2p3p4p5”. Therefore, the number of repetitions of a character must be counted before the operation can be performed. We only need to count the number of consecutive repeats of a character. When we iterate over a character and find that it is not equal to element, we have counted the number of repetitions of the previous character. Then we can concatenate strings.

### What should be paid attention to

  • First general, then special. For boundary control, we need to determine whether the string is null or “”, and then determine accordingly.

  • Each time the value of the variable involved in the preceding loop invariant must be advanced.

  • For example, if a character a occurs once in the string, it should print a, not A1.

  • Do not concatenate data in the form of String concatenation. Strings are immutable, and each concatenation generates a new String. Using StringBuffers is thread-safe or mutable. Each call to append() does not generate a new string.


### Handwritten code

public class Demo {

    public static void main(String[] args) {
        encode("aaabbcaa");
        encode("a");
        encode("abcabc");
        encode("ppppp");
        encode("");
        encode(null);
    }

    public static void encode(String str) {
        if (str == null || str.equals("")) {
            System.out.println("");
            return;
        }

        StringBuffer sb = new StringBuffer();
        int count = 1;
        char element = str.charAt(0);

        for (int i = 1; i < str.length(); i++) {
            if (str.charAt(i) == element) {
                count++;
            } else {
                sb.append(element);
                sb.append(count == 1 ? "" : count);
                element = str.charAt(i);
                count = 1;
            }
        }

        sb.append(element);
        sb.append(count == 1 ? "": count); System.out.println(sb.toString()); }}Copy the code
  • When writing test data, you should consider null values, special values, and boundary values. rundemoLook at the output.


# # # tail

I’m still too bad at it. Java small white, still need to work hard.