This code cannot pass the case "abcdefghij" 9, but it's accepted, why?

  • 0
    // Forward declaration of the read4 API.
    int read4(char *buf);
    class Solution {
         * @param buf Destination buffer
         * @param n   Maximum number of characters to read
         * @return    The number of characters read
        int read(char *buf, int n) {
            char * tmpBuffer=new char[4];
            int numEach=4;
            int i=0;
            while( numEach == 4 && i < n ) {
                numEach = read4(tmpBuffer);
                memcpy(buf+i, tmpBuffer, numEach);
                i += numEach;
            return i>=n ? n:i;

    My question is, if the test case is "abcdefghij" 9, by running this code,
    the final results in the destination buffer is "abcdefghij" instead of "abcdefghi", which is a wrong answer.

    But why the code is still accepted?

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.