leetcode劍指offer-字符串

https://www.jianshu.com/p/9fd6bef32d62

二叉樹,鏈表,字符串

1.翻轉單詞順序列

思路:進行兩次反轉

1.對整個句子反轉;2..對句子中的單詞反轉

2.左旋轉字符串

經過三次反轉:

1.反轉前面的字符串。2.反轉後面的字符串。3.反轉整個字符串

3.字符串的排列

不能直接用力扣46全排列的代碼,力扣46是無重複的數字,這個題是有重複的。

正確的解法:不用not in tmp來判斷這個字母是否需要遍歷,定義一個used矩陣來判斷,就不會去重了。

4.正則表達式匹配

5.迴文鏈表

https://segmentfault.com/a/1190000016779151

兩種方法:

1.用一個棧空間複雜度是o(n)

2.直接在原鏈表上操作,空間複雜度是o(1)

https://blog.csdn.net/qq_34364995/article/details/80640449

思路是,用快慢指針判斷中點位置,然後翻轉後面的一個鏈表,然後比較前後鏈表。

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution(object):
    def reverse(self,head):
        if not head.next:
            return head
        dummy=ListNode(0)
        dummy.next=head
        pre=dummy
        cur=dummy.next
        curnext=dummy.next.next
        cur.next=None
        pre=cur
        cur=curnext
        curnext=curnext.next
        while cur.next:
            cur.next=pre
            pre=cur
            cur=curnext
            curnext=curnext.next
        cur.next=pre
        return cur
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if head is None or head.next is None:
            return True
        if head.next.next is None:
            return head.val == head.next.val
        #用快慢指針找到前半部分,後半部分。對後半部分鏈表進行反轉。
        slow=head
        fast=head
        while fast.next and fast.next.next:
            slow=slow.next
            fast=fast.next.next
        #後半部分是slow.next
        p = self.reverse(slow.next)
        q=head
        while p.next:
            if p.val != q.val:
                return False
            p = p.next
            q = q.next
        return p.val == q.val

6.下一個排列

https://leetcode-cn.com/problems/next-permutation/solution/xia-yi-ge-pai-lie-suan-fa-xiang-jie-si-lu-tui-dao-/

7.至少有K個重複字符的最長子串

解題思路:

1.分冶法,統計字符串各個字符出現的次數,如果有字符數量少於k,那麼包含這個位置的子串統統不成立。

遞歸地求解前半部分和後半部分。
2.光用遞歸會超時,先去除一下字符串左右邊界不符合條件的部分。

lass Solution(object):
    def getcount(self,s):
        count_dic={}
        for i in range(len(s)):
            count_dic[s[i]]=count_dic.get(s[i],0)+1
        return count_dic
    def longestSubstring(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: int
        """
        #s中每個字母出現次數
        count_dic=self.getcount(s)
        #如果全部滿足條件,返回s的長度
        flag=True
        for value in count_dic.values():
            if value<k:
                flag=False
        if flag:
            return len(s)
        #去掉邊界不滿足條件的
        left=0
        right=len(s)-1
        while count_dic[s[left]]<k and left<right:
            left=left+1
        while count_dic[s[right]]<k and left<right:
            right=right-1
        s=s[left:right+1]
        count_dic=self.getcount(s)
        #如果全部滿足條件,返回s的長度
        flag=True
        for value in count_dic.values():
            if value<k:
                flag=False
        if flag:
            return len(s)
        #不滿足條件,從不滿足條件的字母處分割
        for i in range(len(s)):
            if count_dic[s[i]]<k:
                left=self.longestSubstring(s[:i],k)
                right=self.longestSubstring(s[i+1:],k)
                return max(left,right)
        return 0

作者:snow-77
鏈接:https://leetcode-cn.com/problems/longest-substring-with-at-least-k-repeating-characters/solution/pythonfen-ye-fa-bian-jie-tiao-jian-by-snow-77/
來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章