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

資訊專欄INFORMATION COLUMN

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

lykops / 1088人閱讀

摘要:一題目描述空格分隔,逐個反轉(zhuǎn)二題目描述三題目描述當然也可以用的做,不過用雙指針更快。

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 characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let"s take LeetCode contest"

Output: "s"teL ekat edoCteeL tsetnoc"

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

空格分隔,逐個反轉(zhuǎn)

class Solution {
    public String reverseWords(String s) {
        
        String[] ss = s.split(" ");
        StringBuilder sb = new StringBuilder();
        int n = ss.length;
        
        for (int i = 0; i < n - 1; i++) {
            sb.append(reverse(ss[i]) + " ");
        }
        sb.append(reverse(ss[n - 1]));
        
        return sb.toString();
    }
    
    public String reverse(String str) {
        StringBuilder sb = new StringBuilder(str);
        return sb.reverse().toString();
    }
}
    

LeetCode: 541. Reverse String II

二、LeetCode: 541. Reverse String II 題目描述

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.

Example:

Input: s = "abcdefg", k = 2

Output: "bacdfeg"

Restrictions:

The string consists of lower English letters only.

Length of the given string and k will in the range [1, 10000]

class Solution {
    public String reverseStr(String s, int k) {
        
        int n = s.length();
        int i = 0, j = n - 1;
        char[] c = s.toCharArray();
        
        while (i < n) {
            j = Math.min(i + k - 1, n - 1);
            reverse(c, i, j);
            i += 2 * k;
        }
        
        return new String(c);
        
    }
    
    public void reverse(char[] c, int i, int j) {
        while (i < j) {
            char temp = c[i];
            c[i] = c[j];
            c[j] = temp;
            i++;
            j--;
        }
    }
}

LeetCode: 344. Reverse String

三、LeetCode: 344. Reverse String 題目描述

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

Example:

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

當然也可以用 StringBuilder 的 reverse 做,不過用雙指針更快。

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

LeetCode: 345. Reverse Vowels of a String

四、LeetCode: 345. Reverse Vowels of a String 題目描述

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:

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

Example 2:

Given s = "leetcode", return "leotcede".

Note:

The vowels does not include the letter "y".

同樣地,雙指針,注意考慮 vowel 的大小寫

class Solution {
    public String reverseVowels(String s) {
        // a e i o u
        int len = s.length();
        int left = 0, right = len - 1;
        char[] c = s.toCharArray();
        
        while(left < right) {
            if (isVowel(c[left]) && isVowel(c[right])) {
                char temp = c[left];
                c[left] = c[right];
                c[right] = temp;
                left++;
                right--;
            } else if (!isVowel(c[left]))
                left++;
            else right--;
        }
        
        return new String(c);
    }
    
    public boolean isVowel(char c) {
        return c == "a" || c == "A"
           || c == "e" || c == "E"
           || c == "i" || c == "I"
           || c == "o" || c == "O"
           || c == "u" || c == "U";
    }
}

LeetCode: 11. Container With Most Water

五、LeetCode: 11. Container With Most Water 題目描述

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

首先理解題意,就是找一個最大的容器容納最多的水,這個容器由兩個板子組成,每個 an 在數(shù)組中的值就是這個板子的高度,畫一下圖就更清晰了。

[2,1,2] 模擬畫圖 ≡ω≡

|   |
| | |     板子...
--------- x軸
0 1 2     數(shù)組下標

然后抽象出來就是求 (j - i) * Math.min(height[i], height[j]) 的最大值

暴力算法很容易想到,但是會超時

有一種 O(n) 的做法,但是不是很好理解,discuss 里有比較清楚的講解 Yet another way to see what happens in the O(n) algorithm-algorithm)

我理解的核心思想是突破那個短板,如果左邊的短,就移動左邊的板子,如果右邊的短,就移動右邊的板子,看能不能形成更大的容器。

class Solution {
    public int maxArea(int[] height) {
        
        int n = height.length;
        int max = 0;
        int i = 0, j = n - 1;
        
        while (i < j) {
            max = Math.max(max, Math.min(height[i], height[j]) * (j - i));
            if (height[i] < height[j])
                i++;
            else
                j--;
        }
        
        return max;
    }
}

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

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/69446.html

相關(guān)文章

  • LeetCode 第 267 場周賽

    摘要:第三組長度為,奇數(shù),沒有發(fā)生反轉(zhuǎn)。箭頭指示順序即為單元格填充順序。因此我們采用并查集處理朋友關(guān)系。如果沒有沖突,再把修改后的副本賦值給原并查集,添加成功否則就認為這個添加無法進行,原并查集對象不做修改,該請求為。 ...

    Dionysus_go 評論0 收藏0
  • 70道前端LeetCode題目集合及視頻講解(持續(xù)更新中...)

    前端LeetCode刷題 下面是已刷的題目的目錄。GitHub:https://github.com/cunzaizhuy...每日打卡更新中,歡迎關(guān)注。 數(shù)組類 26 刪除排序數(shù)組中的重復項 27 移除元素 35 搜索插入位置 66 加1 80 medium 刪除排序數(shù)組中的重復項2 88 合并兩個有序數(shù)組 167 兩數(shù)之和II - 輸入有序數(shù)組 118 楊輝三角 169 easy 求眾數(shù) 1...

    mayaohua 評論0 收藏0
  • PHP細節(jié):foreach、(漢子)符串反轉(zhuǎn)、isset,empty用法區(qū)別以及0、‘’、null

    摘要:規(guī)定要反轉(zhuǎn)的字符串。參考文檔實現(xiàn)字符串翻轉(zhuǎn)包含中文漢字參考處理漢字官方文檔函數(shù)最下面給出了支持的方案三用法區(qū)別以及之間關(guān)系用法如果是非空或非零的值,則返回。若想檢測常量是否已設置,可使用函數(shù)。 一、foreach 第一題: //1.for循環(huán)執(zhí)行幾次 //for($i=0;$i=1;$i++){ // echo $i; //} $i==1條件很成立,死循環(huán) for($i=0;$...

    孫淑建 評論0 收藏0

發(fā)表評論

0條評論

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