simple golang,python,ruby,javascript,java Solution


  • 0
    S

    golang:

    func reverseString(s string) string {
        r := []rune(s)
        length := len(r)
        for i := 0; i <length/2; i++ {
            r[i],r[length-1-i] = r[length-1-i],r[i]
        }
    	return string(r)
    }
    

    python3:

    class Solution:
        def reverseString(self, s):
            """
            :type s: str
            :rtype: str
            """
            return s[::-1]
    

    ruby:

    def reverse_string(s)
        s.reverse
    end
    

    javascript:

    var reverseString = function(s) {
        return s.split('').reverse().join('');
    };
    

    java:

    class Solution {
        public String reverseString(String s) {
            char[] cs = s.toCharArray();
            char t;
            int length = cs.length;
            for(int i=0;i < length / 2; i++){
                t = cs[i];
                cs[i] = cs[length-1-i];
                cs[length-1-i] = t;
            }
            return String.valueOf(cs);
        }
    }
    

Log in to reply
 

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