Design algorithms and write code to remove repeated characters in strings. No extra cache space is available. Note: one or two additional variables can be used, but an additional array copy is not allowed.
Simple question:
# Include <stdio. h> # include <string. h> void remove_duplicate (char vstr []) {int Len = strlen (vstr); If (! Len) {printf ("the string is null \ n"); return;} int COUNT = 0; For (INT I = 0; I <Len; ++ I) {If (vstr [I]! = '\ 0') {vstr [count ++] = vstr [I]; for (int K = I + 1; k <Len; ++ K) {If (vstr [I] = vstr [k]) {vstr [k] = '\ 0' ;}}} vstr [count] =' \ 0 ';} void remove_duplicate2 (char vstr []) {int Len = strlen (vstr); If (! Len) {printf ("the string is null \ n"); return;} bool visited [256]; memset (visited, 0, sizeof (visited )); int COUNT = 0; For (INT I = 0; I <Len; ++ I) {If (! Visited [vstr [I]) {vstr [count ++] = vstr [I]; visited [vstr [I] = true ;}} vstr [count] = '\ 0';} void test () {char STR [] = "13434343435568889 hhhhhfdcvbb"; remove_duplicate (STR); printf ("% s \ n ", str );}
Keep it up writes several algorithm questions every week, entertainment!