c# Submission Result: Wrong Answer but it is right when I run in vs .


  • 0
    X

    public class Solution {
    public string ReverseVowels(string s) {

      char[] ss=s.ToCharArray();
      char temp=' ',temps=' ';
      int k=0,n=0;
      for(int i=0,j=ss.Length-1;i<=j;i++,j--)
      {
          if(ss[i]=='a'||ss[i]=='e'||ss[i]=='i'||ss[i]=='o'||ss[i]=='u'||ss[i]=='A'||ss[i]=='E'||ss[i]=='I'||ss[i]=='O'||ss[i]=='U')
          {
             temp=ss[i];
             k=i;
          }
           if(ss[j]=='a'||ss[j]=='e'||ss[j]=='i'||ss[j]=='o'||ss[j]=='u'||ss[j]=='A'||ss[j]=='E'||ss[j]=='I'||ss[j]=='O'||ss[j]=='U')
           {
             temps=ss[j];
             n=j;
           }
          if(temp!=' '&&temps!=' ')
          {
             ss[k]=temps;
             ss[n]=temp;
             temps=' ';
             temp=' ';
          }
      }
      return new string(ss);
    
    }
    

    }


  • 0
    X

    The right answer;
    public class Solution
    {
    public string ReverseVowels(string s)
    {

                char[] ss = s.ToCharArray();
                char temp = ' ', temps = ' ';
                for (int i = 0, j = ss.Length - 1; i <= j;)
                {
                    if (ss[i] == 'a' || ss[i] == 'e' || ss[i] == 'i' || ss[i] == 'o' || ss[i] == 'u' || ss[i] == 'A' || ss[i] == 'E' || ss[i] == 'I' || ss[i] == 'O' || ss[i] == 'U')
                    {
                        temp = ss[i];
                    }
                    if (ss[j] == 'a' || ss[j] == 'e' || ss[j] == 'i' || ss[j] == 'o' || ss[j] == 'u' || ss[j] == 'A' || ss[j] == 'E' || ss[j] == 'I' || ss[j] == 'O' || ss[j] == 'U')
                    {
                        temps = ss[j];
                    }
                    if (temp == ' ')
                        i++;
                    if (temps == ' ')
                        j--;
                    if (temp != ' ' && temps != ' ')
                    {
                        ss[i] = temps;
                        ss[j] = temp;
                        temps = ' ';
                        temp = ' ';
                        i++;
                        j--;
                    }
                }
                return new string(ss);
            }
        }

  • 0
    E

    "aeiouAEIOU".Contains(ss[i])
    may make your code simple


  • 0
    X

    @edentidus Yes,good idea,thank you.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.