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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值