004 string deduplication (keep it up)

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.