成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

[LeetCode] Reverse String

Karrdy / 1591人閱讀

Problem

Write a function that takes a string as input and returns the string reversed.

Example

Given s = "hello", return "olleh".

Solution

1. Two-Pointer --3ms

public class Solution {
    public String reverseString(String s) {
        char[] str = s.toCharArray();
        int left = 0, right = str.length-1;
        while (left < right) {
            char temp = str[left];
            str[left] = str[right];
            str[right] = temp;
            left++;
            right--;
        }
        return new String(str);
    }
}

2. StringBuilder --5ms

public class Solution {
    public String reverseString(String s) {
        return  new StringBuilder(s).reverse().toString();
    }
}

3. Recursion --22ms

public class Solution {
    public String reverseString(String s) {
        int len = s.length();
        if (len <= 1) return s;
        String leftStr = s.substring(0, len / 2);
        String rightStr = s.substring(len / 2, len);
        return reverseString(rightStr) + reverseString(leftStr);
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65992.html

相關(guān)文章

  • 翻轉(zhuǎn)字符串的相關(guān)題目

    摘要:一題目描述空格分隔,逐個(gè)反轉(zhuǎn)二題目描述三題目描述當(dāng)然也可以用的做,不過(guò)用雙指針更快。 LeetCode: 557. Reverse Words in a String III 一、LeetCode: 557. Reverse Words in a String III 題目描述 Given a string, you need to reverse the order of chara...

    lykops 評(píng)論0 收藏0
  • [LeetCode] 557. Reverse Words in a String III

    Problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1:Input: Lets take LeetCode contest...

    104828720 評(píng)論0 收藏0
  • [Leetcode] Reverse Words in a String 反轉(zhuǎn)單詞順序

    摘要:代碼先反轉(zhuǎn)整個(gè)數(shù)組反轉(zhuǎn)每個(gè)單詞雙指針交換法復(fù)雜度時(shí)間空間思路這題就是版的做法了,先反轉(zhuǎn)整個(gè)數(shù)組,再對(duì)每個(gè)詞反轉(zhuǎn)。 Reverse Words in a String Given an input string, reverse the string word by word. For example, Given s = the sky is blue, return blue is...

    codeKK 評(píng)論0 收藏0
  • [LeetCode] Reverse Words in a String II

    Problem Reverse Words in a String IIGiven an input string , reverse the string word by word. Example Input: [t,h,e, ,s,k,y, ,i,s, ,b,l,u,e]Output: [b,l,u,e, ,i,s, ,s,k,y, ,t,h,e] Note A word is defin...

    浠ラ箍 評(píng)論0 收藏0
  • LeetCode 345. Reverse Vowels of a String

    摘要:描述編寫(xiě)一個(gè)函數(shù),以字符串作為輸入,反轉(zhuǎn)該字符串中的元音字母。示例輸入輸出示例輸入輸出說(shuō)明元音字母不包含字母。找到所有的元音字母索引,第一個(gè)索引對(duì)應(yīng)的元素和最后一個(gè)索引對(duì)應(yīng)的元素交換,第二個(gè)和倒數(shù)第二個(gè)交換,第三個(gè)和倒數(shù)第三個(gè)交換。 Description Write a function that takes a string as input and reverse only th...

    archieyang 評(píng)論0 收藏0
  • [LeetCode] Reverse Vowels of a String

    摘要:第二種解法相同的思路,一頭一尾兩個(gè)指針向中間夾逼。注意只有當(dāng)頭指針為元音字母時(shí),才會(huì)操作尾指針。判斷尾指針?lè)窃糇帜傅臈l件 Problem Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = hello, return hol...

    dabai 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<