WIith Google search, I find there is a website (http://crack5e.info/) to list the C++ solutions to the problems listed in book Cracking the Coding Interview (5th edition). It is a great website. Thanks to the author! In the following days, I will also try to post my C solutions on my blog, if the C solution is possible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | // Author: Sheng Yu // Time: 02/02/2013 // // C solution for problem 1.1 in Cracking the Coding Interview 5th eidtion. // For this problem, it's critical to know whether the string is an ASCII // string or an Unicode string. In the solution, we assume it is an ASCII // string // // Test environment: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-52) // #include <stdio.h> #include <stdbool.h> #include <string.h> bool isUnique(char *str){ int index = -1; char mask[32]; if(str == NULL){ // A null string should be unique return true; } memset(mask,0,32); // clear all the masks to 0 // Scan the string and check whether it is unique while(str[++index] != ' '){ if((mask[str[index]/8] & (1<<(str[index]%8))) != 0) return false; else mask[str[index]/8] |= 1<<(str[index]%8); } return true; } int main(int argc, char *argv[]){ // Firstly check the arguments if(argc != 2){ printf("Invalid arguments.n"); printf("Usage: %s test_stringn",argv[0]); return 1; } // Use the argument as test string bool testret = isUnique(argv[1]); if(testret == true) printf("%s: is unique.n",argv[1]); else printf("%s: is not unique.n",argv[1]); return 0; } |