3

golang 递归判断回文字符串

 2 years ago
source link: https://studygolang.com/articles/3147?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

golang 递归判断回文字符串

guonaihong · 2015-05-25 23:00:01 · 3501 次点击 · 预计阅读时间 2 分钟 · 大约8小时之前 开始浏览    
这是一个创建于 2015-05-25 23:00:01 的文章,其中的信息可能已经有所发展或是发生改变。

判断回文字符串是个比较经典的问题。

思路就是拿第一个字符和最一个字符比较,如果不等退出,相同的话继续刚刚的过程,直到第一个字符和最后一个字符相遇或者他们的距离为1时。说明他们是回文字符串。

下面的代码会忽略空白字符 如"1   1  2 1"会让为是回文字符串。

golang

package main

import (
    "fmt"
    "os"
    "strings"
    "unicode/utf8"
)

func doPalindrome(s string) bool {
    if utf8.RuneCountInString(s) <= 1 { 
        return true
    }   

    word := strings.Trim(s, "\t \r\n\v")
    first, sizeOfFirst := utf8.DecodeRuneInString(word)
    last, sizeOfLast := utf8.DecodeLastRuneInString(word)

    if first != last {
        return false
    }   
    return doPalindrome(word[sizeOfFirst : len(word)-sizeOfLast])
}

func IsPalindrome(word string) bool {
    s := ""
    s = strings.Trim(word, "\t \r\n\v")
    if len(s) == 0 || len(s) == 1 { 
        return false
    }   
    return doPalindrome(s)
}

func main() {
    args := os.Args[1:]
    for _, v := range args {
        ok := IsPalindrome(v)
        if ok {
            fmt.Printf("%s\n", v)
        }   
    }   

}

clang  递归版:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>

int do_palind(char *first, char *last) {
    /*跳过头部的空白字符*/
    while(*first && isspace(*first)) {
        first++;
    }   
    /*跳过尾部的空白字符*/
    while (first < last && isspace(*last)) {
        last--;
    }   

    if (last - first <= 0)
        return 1;

    if (*first != *last)
        return 0;

    return do_palind(++first, --last);
}

int ispalindrome(const char *str) {
    if (str[0] == '\0' || str[1] == '\0')
        return 0;
    //printf("---->%ld\n", strlen(str));
    return do_palind((char *)str, (char *)str + strlen(str) - 1); 
}

int main(int argc, char **argv) {
    int is; 
    while (*++argv) {
        is = ispalindrome(*argv);
        if (is)
            printf("%s\n", *argv);
    }   
}

clang 循环版:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int ispalindrome(const char *str) {
    char *last;

    if (str[0] == '\0' || str[1] == '\0')
        return 0;

    last = (char *)str + strlen(str) - 1;

    while (str < last) {
        while (str < last && isspace(*str)) {
            str++;
        }   

        while (str < last && isspace(*last)) {
            last--;
        }   

        if (*str != *last)
            return 0;
        str++;
        last--;
    }   
    return 1;
}

int main(int argc, char **argv) {
    int is; 
    while (*++argv) {
        is = ispalindrome(*argv);
        if (is) {
            printf("%s\n", *argv);
        }   
    }   
}



有疑问加站长微信联系(非本文作者)

280

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK