zoukankan      html  css  js  c++  java
  • 玩转回文

    什么是回文:就是正着读和逆着读都一样,如:abcba

    上一个图加深认识

    判断是一个字符串是否是回文直接上题了

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                /*
                    莺啼岸柳弄春晴,
                    柳弄春晴夜月明。
                    明月夜晴春弄柳,
                    晴春弄柳岸啼莺
                 */
    
                string strTest = @"莺啼岸柳弄春晴柳弄春晴夜月明明月夜晴春弄柳晴春弄柳岸啼莺";
                string strTest2 = @"abcdeedcba";
                string strTest3 = @"efgcgfe"
    ;            //长度:奇数和偶数
                if (isPalindrome(strTest3))
                {
                    Console.WriteLine("yes");
                }
                else
                {
                    Console.WriteLine("no");
                }
            }
    
            public static bool isPalindrome(string str)
            {
                if (str == null || str.Length <= 1)
                {
                    return true;
                }
    
                //重点 int i=0, j= str.length-1; i<j;i++,j--
                for (int i = 0, j = str.Length - 1; i < j; i++, j--)
                {
                    if (str.Substring(i,1) != str.Substring(j,1))
                    {
                        return false;
                    }
                }
    
                return true;
            }
        }
    }
  • 相关阅读:
    codeforces 980A Links and Pearls
    zoj 3640 Help Me Escape
    sgu 495 Kids and Prizes
    poj 3071 Football
    hdu 3853 LOOPS
    hdu 4035 Maze
    hdu 4405 Aeroplane chess
    poj 2096 Collecting Bugs
    scu 4444 Travel
    zoj 3870 Team Formation
  • 原文地址:https://www.cnblogs.com/yzenet/p/5090041.html
Copyright © 2011-2022 走看看