Leetcode解題筆記(5)

仍然是String相關

383. Ransom Note

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.

Each letter in the magazine string can only be used once in your ransom note.

You may assume that both strings contain only lowercase letters.

題目大意就是給出兩個字符串,判斷第一個字符串能否由第二個字符串構成,每個字符只能使用一次,假設都是小寫

一開始好幾次wrong answer,因爲對題目理解的不到位。

最開始的想法是將兩個字符串都轉換成數組,並用array.sort()方法按照ASCII碼排一下,然後就簡單粗暴的對比一下。但是不對,比如"bg","eawfawebatfewagaerfga",是返回true的,但是之前的排序對他來說就沒有什麼意義,所以換了一種思路。

利用string.indexOf(),遍歷字符串,如果有,則將第二個字符串中對應的地方改成0,,因爲題目要求不能重複利用;如果沒有就return false。

/**
 * @param {string} ransomNote
 * @param {string} magazine
 * @return {boolean}
 */
var canConstruct = function(ransomNote, magazine) {
    ransomNote = ransomNote.split("");
    magazine = magazine.split("");
    if(ransomNote.length > magazine.length){
        return false;
    }else{
        var i=0;
        while(i < ransomNote.length){
            var tem = magazine.indexOf(ransomNote[i]);
            if(tem == -1){
                return false;
            }else{
                magazine[tem] = 0;
            }
            i++;
        }
        return true;
    }
};

58. Length of Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

題目大意就是判斷一個字符串裏最後一個單詞的長度。

最開始一個思路是找到最後一個空格的位置,然後獲取從這個位置到最後的子串的長度。一個漏洞就是 “a        ”

重新調整了一下,利用正則匹配結尾開始的空白字符並用空白替換掉,然後轉換成數組,利用array.pop()方法再求其長度即可。

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function(s) {
    var str = s.replace(/\s+$/,"").split(" ").pop();
    return str.length;
};

一開始還小小激動了一下 但是wronganswer好幾次 因爲replace()方法我用的不對,正則的時候不用加引號了!


520. Detect Capital

Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. Only the first letter in this word is capital if it has more than one letter, like "Google".
Otherwise, we define that this word doesn't use capitals in a right way.

題目大意爲判斷大寫是否正確。都大寫、都小寫、首字母大寫纔可以

思路一是結合正則進行判斷。

先看第一個字母,如果小寫則之後就都應該是小寫;如果大寫則要看第二個字母,如果大寫則之後都應該大寫,如果小寫則之後都應該小寫。

/**
 * @param {string} word
 * @return {boolean}
 */
var detectCapitalUse = function(word) {
    var reg1 = /[A-Z]/;
    var reg2 = /[a-z]/g;
    var reg3 = /[A-Z]/g;


    if(reg1.test(word[0])){
        if(reg1.test(word[1])){
            if(reg2.test(word.substring(2))){
                return false;
            }else{
                return true;
            }
        }else{
            if(reg3.test(word.substring(1))){
                return false;
            }else{
                return true;
            }
        }
    }else{
        if(reg3.test(word)){
            return false;
        }else{
            return true;
        }
    }
};
代碼冗餘。


思路二是想到toUppercase() toLowercase()

遍歷字符串 看當前字母大寫/小寫之後是否仍和當前字母一樣,同時要注意是否是首字母。

/**
 * @param {string} word
 * @return {boolean}
 */
var detectCapitalUse = function(word) {
    var lower=0,upper=0;
    for(var i=0;i < word.length;i++){
        if(word[i].toUpperCase() == word[i] && lower > 0){
            return false;
        }else if(word[i].toLowerCase() == word[i] && upper > 1){
            return false;
        }else if(word[i].toUpperCase() == word[i]){
            upper++;
        }else if(word[i].toLowerCase() == word[i]){
            lower++;
        }
    }
    return true;
};

感覺字符串和正則配合的地方還是很多 順便安利一個可以圖形化的正則網站!

https://regexper.com


LeetCode上還有一些字符串的easy題 比如字符串匹配 括號對應 之前數據結構課上老師講過 主要是利用棧 具體的比如字符串匹配用到KMP算法 還沒有動手實踐過。。還有一些涉及到動態規劃的

要學習的還是很多!



發佈了38 篇原創文章 · 獲贊 10 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章