0ms c solution,easy to understand


  • 0
    S
    static 	char prefix[500] = "";
    char* longestCommonPrefix(char** strs, int strsSize) {
        int i = 0;
    	int k = 0;
    	char tmp[500] = "";
    	if (strsSize == 1) return *strs;
    	char *cb = strs[0];
    	for (i = 1; i < strsSize; i++) {
    		char *cp = strs[i];
    		while (*cb){
    			if (*cb == *cp){
    				tmp[k++] = *cb;
    				cb++;
    				cp++;
    			}
    			else  break;
    		}
    		tmp[k] = '\0';
    		cb = tmp;
    		k = 0;	
    	}
    	strcpy(prefix, tmp);
    	return prefix;
    }
    

Log in to reply
 

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