[Point] Given two strings code, using the second string to the first division, the realization String split () function method. Request does not use subString () and native split () method

A few days ago a friend threw me such a coding problem, and asked me if I had any idea I was first thought was that this question is not leetcode, this stock will not want to show off my loaded to force it, then embarrassing, as a not-depth contact with leetcode programmer (ps: it is ashamed), how could a short time both accurate and efficient code of his training and preparation, and a serious blow to his face yet. Ha ha ha, joke, get down to business.

Speaking of this segmentation of the string, I first thought the student exposed to the concept of a cursor, called a cursor, in fact, from a certain end to the other end, traversing pointers, so I was wondering, with the cursor is not possible as a strategy to solve this question yet. Based on the above ideas, quick comb, care must be taken to achieve the following key points:

  1. The second string as a cursor, sequentially from the back than the first string subscript 0;
  2. Left and right to compare the two are likely to be multi-character, since there was no use subString () method, then they would avoid about two contrasting string as a logical whole, so I can think of is to use the array;
  3. How a matching portion does not match with the distinguished portion, Oh, packaging may be used, it can be used as a special value reaches a null value to distinguish the effect;
  4. After the end of the cursor traversal, a null value or a null value will be continuous as a whole division boundaries, as a subsequent determination logic to assemble the result set and the completion of process control;

Man of few words said, directly on the code (the code may bug some point: special characters, null pointers, etc., are currently not considered).

public class SplitDemo {

    public List<String> split(String str, String reg) {
        List<String> result = new ArrayList<>();
        Character[] strArr = toCharacterArray(str);
        Character[] regArr = toCharacterArray(reg);
        //游标逻辑
        for (int i = 0; i < strArr.length; i++) {
            //标识多字符是否完全匹配
            boolean flag = false;
            for (int j = 0; j < regArr.length; j++) {
                flag = regArr[j].equals(strArr[i + j]);
                if (!flag) {
                    break;
                }
            }
            if (!flag) {
                continue;
            }
            //特殊null值赋值
            for (int j = 0; j < regArr.length; j++) {
                strArr[i + j] = null;
            }
        }
        //结果集封装
        String item = "";
        for (Character character : strArr) {
            if (null != character) {
                item = item + character;
            } else {
                if (!"".equals(item)) {
                    result.add(item);
                    item = "";
                }
            }
        }
        if (!"".equals(item)) {
            result.add(item);
        }
        return result;
    }
    /**
    将字符串转为字符包装类数组
    */
    private Character[] toCharacterArray(String str) {
        Character[] result = new Character[str.length()];
        for (int i = 0; i < result.length; i++) {
            result[i] = str.charAt(i);
        }
        return result;
    }
}
复制代码

Test Methods

    public static void main(String[] args) {
        List<String> result = new SplitDemo().split("asasdecdsdxa", "sd");
        for (String str : result) {
            System.out.println(str);
        }
    }
复制代码

Output

Initially completed, but felt a little tedious after a friend posted the exchange, has another approach. A friend direct code screenshots.

This code has several key points:

  1. indexOf (String str, int fromIndex), returns the first matching string to the first full header of the second character string, if there is no match to -1;
  2. To control the position of the first string match the initial determination, i.e. indexOf (String str, int fromIndex) into the Parameter formIndex, taken sequentially repeated to achieve the effect;

Overall logic is very clear, and less the amount of code, more efficient preliminary judgment, here I can not help but to String class indexOf (String str, int formIndex) method full of curiosity, let us pursue what JDK source code (version JDK8) .

    /**
     * Returns the index within this string of the first occurrence of the
     * specified substring, starting at the specified index.
     *
     * <p>The returned index is the smallest value <i>k</i> for which:
     * <blockquote><pre>
     * <i>k</i> &gt;= fromIndex {@code &&} this.startsWith(str, <i>k</i>)
     * </pre></blockquote>
     * If no such value of <i>k</i> exists, then {@code -1} is returned.
     *
     * @param   str         the substring to search for.
     * @param   fromIndex   the index from which to start the search.
     * @return  the index of the first occurrence of the specified substring,
     *          starting at the specified index,
     *          or {@code -1} if there is no such occurrence.
     */
    public int indexOf(String str, int fromIndex) {
        return indexOf(value, 0, value.length,
                str.value, 0, str.value.length, fromIndex);
    }
    
    /**
     * Code shared by String and StringBuffer to do searches. The
     * source is the character array being searched, and the target
     * is the string being searched for.
     *
     * @param   source       the characters being searched.
     * @param   sourceOffset offset of the source string.
     * @param   sourceCount  count of the source string.
     * @param   target       the characters being searched for.
     * @param   targetOffset offset of the target string.
     * @param   targetCount  count of the target string.
     * @param   fromIndex    the index to begin searching from.
     */
    static int indexOf(char[] source, int sourceOffset, int sourceCount,
            char[] target, int targetOffset, int targetCount,
            int fromIndex) {
        if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (targetCount == 0) {
            return fromIndex;
        }

        char first = target[targetOffset];
        int max = sourceOffset + (sourceCount - targetCount);

        for (int i = sourceOffset + fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (source[i] != first) {
                while (++i <= max && source[i] != first);
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;
                int end = j + targetCount - 1;
                for (int k = targetOffset + 1; j < end && source[j]
                        == target[k]; j++, k++);

                if (j == end) {
                    /* Found whole string. */
                    return i - sourceOffset;
                }
            }
        }
        return -1;
    }
复制代码

We can see, it calls a static method indexOf internally (char [] source, int sourceOffset, int sourceCount, char [] target, int targetOffset, int targetCount, int fromIndex), passing parameters from left to right can be understood as source byte array (first string title), a source byte array offset (default 0 incoming internal call), the length of the source array of bytes, the byte array to look for (a second header string), the byte offset of the array to find (still 0), the length of the byte array to look for, and the index matched initial position.

The method proceeds to the inside, can be seen generally the process is:

  1. Illegal into the reference filter
  2. Obtaining a first element of the byte array to find, may be calculated to find the first matching element of the array a source maximum byte array index (expressed herein together about ~ Comparative)
  3. Open cycle, the initial position is actually passed in when calling this method last parameter, the end of the cycle symbol i is more than the max value in step 2
  4. Quickly find the location of the first character to match
  5. Open inner loop, the position obtained in step 4 as the starting point, rapid matching to find the remaining elements of the array of characters, exact match is returned i, does not match the end of this inner loop, the outer loop, the first layer into the cycle;
  6. The final was not found or -1

Investigate its internal principle, in fact, beginning with the author of the cursor program would be similar, but more subtle, especially the flow control statements, the use of very sophisticated critical value, it is worth learning!

That's all for this share of it, there is something wrong place to welcome the students correct me!

Guess you like

Origin juejin.im/post/5e8c37d551882573a509a40d