5

在一次入门级 C# 开发人员角色的面试中,我有 30 分钟的时间来完成以下任务,我能做到的最接近的任务是找出当前索引两边的字符是否相互匹配。

构造一个数组,该数组接受一个字符串并确定索引 (i) 处的子字符串是否为

(i) 的左侧反转时,等于 (i) 右侧的子字符串。

例如:“赛车”

在 index(3) 处,左子字符串为“rac”,反转时等于右子字符串“car”。

返回 (i) 如果满足这样的条件,否则返回 -1。如果字符串长度小于 3,则返回 0;

  if (str.Length < 3)
            return -1;

        for (int i = 0; i < str.Length - 1; i++)
        {
            if(str[i-1] == str [i+1])
               return i;
        }
                return -1;
4

9 回答 9

3

如果i != n/2你应该返回 false,那么只需检查i==n/2

int n = str.Length;
for (int i=0;i<=n/2;i++)
{
   if (str[i] != str[n-i-1])
     return -1;
}
return n/2;
于 2012-04-30T07:09:59.823 回答
1

我想出了这个,但我真的希望当他们问这个时你正坐在 Visual Studio 前面......

using System.Linq;

class Program {

    // New version: in fact, we are only looking for palindromes
    // of odd length
    static int FancyQuestion2(string value) {
        if (value.Length % 2 == 0) {
            return -1;
        }
        string reversed = new string(value.ToCharArray().Reverse().ToArray());
        if (reversed.Equals(value,  StringComparison.InvariantCultureIgnoreCase)) {
            return (int)(value.Length / 2);
        }
        return -1;
    }

    static void Main(string[] args) {
        int i1 = FancyQuestion2("noon"); // -1 (even length)
        int i2 = FancyQuestion2("racecar"); // 3
        int i3 = FancyQuestion2("WasItACatISaw"); // 6
    }
}
于 2012-04-30T07:14:17.493 回答
0
 public static int check(string str)
    {
        if(str.Length < 3)
            return 0;

        int n = str.Length;
        int right = str.Length-1;
        int left = 0;

        while (left < right)
        {
            if (str[left] != str[right])
                return -1;

            left++;
            right--;
        }
        return n / 2;
    }
于 2012-04-30T07:06:42.067 回答
0
  public static bool check(string s, int index)
        {

            if (s.Length < 3)
                return false;

            string left = s.Substring(0, index);
            Char[] rightChars = s.Substring(index + 1).ToCharArray();
            Array.Reverse(rightChars);
                string right =new string (rightChars);
                return left.Equals(right);
        }
于 2012-04-30T07:16:20.830 回答
0

试试这个

 static void Main(string[] args)
    {
        string theword = Console.ReadLine();
        char[] arrSplit = theword.ToCharArray();
        bool status = false;
        for (int i = 0; i < arrSplit.Length; i++)
        {
            if (i < theword.Length)
            {
                string leftWord = theword.Substring(0, i);
                char[] arrLeft = leftWord.ToCharArray();
                Array.Reverse(arrLeft);
                leftWord = new string(arrLeft);
                string rightWord = theword.Substring(i + 1, theword.Length - (i + 1));
                if (leftWord == rightWord)
                {
                    status = true;
                    break;
                }
            }
        }
        Console.Write(status);
        Console.ReadLine();
    }
于 2012-04-30T07:27:07.267 回答
0

纯 C,但希望工作流程可以帮助您实现目标。谢谢你分享这个。

int is_palindrome (const char str[], unsigned int index)
{
    int len = strlen(str);
    int result = index;

    //index not valid?
    if (index >= len)
        return -1;

    int i;
    for (i = 0; ((index+i) < len && (index - i) >= 0); ++i) {
        if(str[index-i] != str[index+i])
            return -1;
        else
            continue;
    }

    return result;
}
于 2012-04-30T07:31:22.387 回答
0

您的方法是正确的,但实施是错误的。您需要一个与 不同的循环变量i,因为它包含字符串中(假定)中心字符的索引。

此外,您不能从循环内部返回索引,那么您只会检查一对字符。您必须遍历字符串,然后检查结果。

int checkPalindrome(string str, int i) {
  // check that the index is at the center of the string 
  if (i != str.Length - i - 1) {
    return -1;
  }
  // a variable to keep track of the state
  bool cont = true;
  // loop from 1 until you reach the first and last characters
  for (int j = 1; cont && i - j >= 0; j++) {
    // update the status
    cont &= str[i - j] == str[i + j];
  }
  // check if the status is still true
  if (cont) {
    return i;
  } else {
    return -1;
  }
}
于 2012-04-30T07:33:11.917 回答
0

这是我能想到的最短的:

using System;
public class Test
{
    public static void Main()
    {            
        string example = "racecar";
        bool isPal = IsBothEndsPalindrome(example, 3);
        Console.WriteLine(isPal);
    }

    static bool IsBothEndsPalindrome(string s, int i) {
        while(i-- > 0) {
            if(s[i] != s[s.Length - i - 1]) return false;
         }
         return true;
    }
}

它是如何运作的:http: //ideone.com/2ae3j

于 2012-04-30T07:46:22.707 回答
0

另一种方法,返回时测试 -1,我能想到的最短:

using System;
public class Test
{
    public static void Main()
    {       
            TestPal( "Michael", 3 );                
            TestPal( "racecar", 3 );
            TestPal( "xacecar", 3 );
            TestPal( "katutak", 3 );
            TestPal( "able was i ere i saw elba", 7 );
            TestPal( "radar", 2 );
            TestPal( "radars", 2 );
            // This is false, space is not ignored ;-)
            TestPal( "a man a plan a canal panama", 9 );
    }

    static void TestPal(string s, int count) {
        Console.WriteLine( "{0} : {1}", s, IsBothEndsPalindrome(s, count) );
    }

    static bool IsBothEndsPalindrome(string s, int count) {           
        while(--count >= 0 && s[count] == s[s.Length - count - 1]);        
        return count == -1;
    }
}

输出:

Michael : False
racecar : True
xacecar : False
katutak : True
able was i ere i saw elba : True
radar : True
radars : False
a man a plan a canal panama : False

注意:最后一个为False,空格不被代码忽略

于 2012-04-30T08:27:08.630 回答