C# accepted solution, not using built in functions


  • 0
    P
    public string ReverseStr(string s, int k) {
        int n = s.Length;
        if(n>=k)
        {
        for(int i=0;i<n;i=i+2*k)
        {
            s = ReverseString(s,i,i+k-1<=n-1?i+k-1:n-1);
        }
        }
        else 
        {
            s = ReverseString(s,0,n-1);
        }
        return s;
    }
    
    public string ReverseString(string str, int i, int j) {
        char []s = str.ToCharArray();
        while(i<j)
        {
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
        return new String(s);
    }

Log in to reply
 

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