LC Two Pointers

[Uber]Trapping rain water

[基本功]将arr左侧分为小于等于pv=arr[0],右侧为大于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]

思路:从尾部扫,遇到第一个升序就发现了机会!比如[1,3]。但是以为是 lexicographically next greater permutation of numbers, 所以仅仅这两个swap还不够,比如[1,2,3,5,4,9,8],我们在[4,9]发现了机会,但是答案是[1,2,3,5,8,4,9],因此要做两个操作,第一,我们要找到之后大于4的最小的数来和swap,第二,4之后的数字需要reverse一下变成升序,因为现在它们是降序排列的。
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
小改动,要求输出最长的substring,不是长度。最后问了一下复杂度。

Easy:
LC 344 Reverse String
定义头尾指针,调换对应的字符

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
逆转字符串中的元音字母

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
删除排序数组中的重复数字,返回剩余数组的长度.
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
删除数组中指定的数字,返回剩余数组的长度, 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
将数组中的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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,712评论 0 33
  • 首先觉得很荣幸,有机会践行叶老师的易效能时间管理课程有4个多月了 。这时间说起来也蛮长了,在想想有什么值得...
    零风xyxn阅读 300评论 0 4
  • 今天,我和爸爸下围棋。我先把黑棋放在了中间,然后爸爸来来了个双打吃,急的我团团转,我想出了办法,我趁机爸爸没注...
    李文骏1一L阅读 183评论 0 0
  • 这少年少年 一直奔跑 顺着远山绵延 是疯癫的少年 无法撤回的改变 想象着山的另一边 海对你说。此生不见 但他仍是少...
    徐徐虚虚阅读 114评论 0 0
  • 世事洞明皆学问,人情练达即文章。
    庄德坤阅读 151评论 0 0