国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] Reverse String

Karrdy / 1586人閱讀

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);
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65992.html

相關文章

  • 翻轉字符串的相關題目

    摘要:一題目描述空格分隔,逐個反轉二題目描述三題目描述當然也可以用的做,不過用雙指針更快。 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 評論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 評論0 收藏0
  • [Leetcode] Reverse Words in a String 反轉單詞順序

    摘要:代碼先反轉整個數組反轉每個單詞雙指針交換法復雜度時間空間思路這題就是版的做法了,先反轉整個數組,再對每個詞反轉。 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 評論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...

    浠ラ箍 評論0 收藏0
  • LeetCode 345. Reverse Vowels of a String

    摘要:描述編寫一個函數,以字符串作為輸入,反轉該字符串中的元音字母。示例輸入輸出示例輸入輸出說明元音字母不包含字母。找到所有的元音字母索引,第一個索引對應的元素和最后一個索引對應的元素交換,第二個和倒數第二個交換,第三個和倒數第三個交換。 Description Write a function that takes a string as input and reverse only th...

    archieyang 評論0 收藏0
  • [LeetCode] Reverse Vowels of a String

    摘要:第二種解法相同的思路,一頭一尾兩個指針向中間夾逼。注意只有當頭指針為元音字母時,才會操作尾指針。判斷尾指針非元音字母的條件 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 評論0 收藏0

發表評論

0條評論

Karrdy

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<