LC Two Pointers

[Uber]Trapping rain water

[基本功]將arr左側(cè)分為小于等于pv=arr[0]勾栗,右側(cè)為大于pv聋庵!

arr = [5,7,4,2,9,8]
pv = arr[0]
i = j = len(arr)-1

#j to the end are greater than pv

while i>0:
    if arr[i]>pv: 
        arr[i],arr[j] = arr[j],arr[i]
        i -= 1
        j -= 1
    else:
        i -= 1

arr[j],arr[0] = arr[0],arr[j]
print(arr) #[4, 2, 5, 7, 9, 8]

[蟈蟈電面]LC 31 Next Permutation
[1,3,2]-->[2,1,3]
[1,2,3] → [1,3,2]
[1,3,5,4,8,9]-->[1,3,5,4,9,8]

思路:從尾部掃内列,遇到第一個(gè)升序就發(fā)現(xiàn)了機(jī)會!比如[1,3]。但是以為是 lexicographically next greater permutation of numbers, 所以僅僅這兩個(gè)swap還不夠,比如[1,2,3,5,4,9,8]馋评,我們在[4,9]發(fā)現(xiàn)了機(jī)會,但是答案是[1,2,3,5,8,4,9]刺啦,因此要做兩個(gè)操作留特,第一,我們要找到之后大于4的最小的數(shù)來和swap洪燥,第二磕秤,4之后的數(shù)字需要reverse一下變成升序,因?yàn)楝F(xiàn)在它們是降序排列的捧韵。
No return, modify in place!
beat 100%

class Solution:
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        # find the first asending piece from tail
        pivot = -1
        for i in range(len(nums) - 2, -1, -1):
            if nums[i] < nums[i + 1]:
                pivot = i
                break
        
        if pivot == -1:
            nums.reverse()
            return
        
        # find the next larger number to replace pivot
        for j in range(len(nums) - 1, -1, -1):
            if nums[j] > nums[pivot]:
                nums[pivot], nums[j] = nums[j], nums[pivot]
                break
    
        nums[pivot+1:] = nums[pivot+1:][::-1]

[Uber電面] LC 3 Longest Substring Without Repeating Characters
小改動(dòng)市咆,要求輸出最長的substring,不是長度再来。最后問了一下復(fù)雜度蒙兰。

Easy:
LC 344 Reverse String
定義頭尾指針,調(diào)換對應(yīng)的字符

class Solution(object):
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        s = list(s)
        start = 0
        end = len(s)-1
        while(start<end):
            s[start],s[end] = s[end],s[start]
            start+=1
            end-=1
        return ''.join(s)

LC 345. Reverse Vowels of a String
逆轉(zhuǎn)字符串中的元音字母

class Solution(object):
    def reverseVowels(self,s):
        dic=['a','o','e','i','u','A','O','E','I','U']
        tmp=list(s)
        start,end = 0,len(tmp)-1
        while(start<end):
            while(start<end and tmp[start] not in dic):
                start+=1
            while(end>start and tmp[end] not in dic):
                end-=1
            if start<end:
                tmp[start],tmp[end] = tmp[end],tmp[start]
                start+=1
                end-=1
        return ''.join(tmp)

LC 26. Remove Duplicates from Sorted Array
刪除排序數(shù)組中的重復(fù)數(shù)字芒篷,返回剩余數(shù)組的長度.
Duplicates are guaranteed to be removed up to j-1. j is supposed to be filled with next new number.
Attention: it is sorted!

class Solution(object):
    def removeDuplicates(self, nums):
        if nums == []: return 0
        j = 1
        for i in range(1,len(nums)):
            if nums[i]!=nums[i-1]:
                nums[j] = nums[i]
                j+=1
        return j

LC 27 Remove Element
刪除數(shù)組中指定的數(shù)字搜变,返回剩余數(shù)組的長度, in place!

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        if nums == []: return 0
        j = 0
        for i in range(len(nums)):
            if nums[i]!=val:
                nums[j]=nums[i]
                j+=1
        return j

LC 283. Move Zeroes
將數(shù)組中的0移到最后. No zeros from 0 to j-1, so we will fill 0 from j onwards.

class Solution(object):
    def moveZeroes(self, nums):
        j=0
        for i in range(len(nums)):
            if nums[i]!=0:
                nums[j]=nums[i]
                j=j+1
        for i in range(j,len(nums)):
            nums[i]=0

Medium:
LC 763. Partition Labels (Two Pointers, Greedy)
Input: S = "ababcbacadefegdehijhklij"
Output: [9,7,8]
The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
S will consist of lowercase letters ('a' to 'z') only.

LC 524. Longest Word in Dictionary through Deleting
給定string s和list d,判斷s刪去一部分字符是否可以組成d中的字符串,如果可以求長度最長且字典序最小的字符串针炉。否則返回空串挠他。If there are more than one possible results, return the longest word with the smallest lexicographical order.

Input: s = "abpcplea", d = ["ale","apple","monkey","plea"]
Output: "apple"

Input: s = "abpcplea", d = ["a","b","c"]
Output: "a"

How to check if a needle (word) is a subsequence of a haystack (S)? This is a classic problem with the following solution: walk through haystack, keeping track of the position (i) of the needle. Whenever word[i] matches the current character in S, we only have to match word[i+1:], so we increment i. At the end of this process, i == len(word) if and only if we've matched every character in word to some character in S.

def findLongestWord(self, S, D):
    D.sort(key = lambda x: (-len(x), x))
    for word in D:
        i = 0
        for c in S:
            if i < len(word) and word[i] == c:
                i += 1
        if i == len(word):
            return word
    return ""
  1. Merge Intervals

reference:
https://blog.csdn.net/tinkle181129/article/details/79990668

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市篡帕,隨后出現(xiàn)的幾起案子殖侵,更是在濱河造成了極大的恐慌,老刑警劉巖镰烧,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拢军,死亡現(xiàn)場離奇詭異,居然都是意外死亡怔鳖,警方通過查閱死者的電腦和手機(jī)茉唉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來结执,“玉大人度陆,你說我怎么就攤上這事∠揍#” “怎么了坚芜?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長斜姥。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么铸敏? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任缚忧,我火速辦了婚禮,結(jié)果婚禮上杈笔,老公的妹妹穿的比我還像新娘闪水。我一直安慰自己,他們只是感情好蒙具,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布球榆。 她就那樣靜靜地躺著,像睡著了一般禁筏。 火紅的嫁衣襯著肌膚如雪持钉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天篱昔,我揣著相機(jī)與錄音每强,去河邊找鬼。 笑死州刽,一個(gè)胖子當(dāng)著我的面吹牛空执,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播穗椅,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼辨绊,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了匹表?” 一聲冷哼從身側(cè)響起门坷,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎桑孩,沒想到半個(gè)月后拜鹤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡流椒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年敏簿,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宣虾。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡惯裕,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出绣硝,到底是詐尸還是另有隱情蜻势,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布鹉胖,位于F島的核電站握玛,受9級特大地震影響够傍,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜挠铲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一冕屯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拂苹,春花似錦安聘、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至脯宿,卻和暖如春念颈,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背嗅绰。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工舍肠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人窘面。 一個(gè)月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓翠语,卻偏偏與公主長得像,于是被迫代替她去往敵國和親财边。 傳聞我的和親對象是個(gè)殘疾皇子肌括,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,744評論 0 33
  • 首先覺得很榮幸酣难,有機(jī)會踐行葉老師的易效能時(shí)間管理課程有4個(gè)多月了 谍夭。這時(shí)間說起來也蠻長了,在想想有什么值得...
    零風(fēng)xyxn閱讀 308評論 0 4
  • 今天憨募,我和爸爸下圍棋紧索。我先把黑棋放在了中間,然后爸爸來來了個(gè)雙打吃菜谣,急的我團(tuán)團(tuán)轉(zhuǎn)珠漂,我想出了辦法,我趁機(jī)爸爸沒注...
    李文駿1一L閱讀 201評論 0 0
  • 這少年少年 一直奔跑 順著遠(yuǎn)山綿延 是瘋癲的少年 無法撤回的改變 想象著山的另一邊 海對你說尾膊。此生不見 但他仍是少...
    徐徐虛虛閱讀 118評論 0 0
  • 世事洞明皆學(xué)問媳危,人情練達(dá)即文章。
    莊德坤閱讀 155評論 0 0