LeetCode Python

[LeetCode] 3. Longest Substring Without Repeating Characters 最长无重复字符的子串 (Medium)
 

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", with the length of 3. 
Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3. 
             Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

# 最长不重复子串
a = "bbbbb"

def LongestSubstring(a):
    # 用于看是否在当前子串
    res = {}
    # 子串变化时与历史对比, 存下当前最长快照
    snap = ""
    # 当前子串
    tmp = ""
    cur_i = 0
    for i in range(len(a)):
        if a[i] not in res:
            res[a[i]] = i
            tmp += a[i]

        else:
            if i == len(a)-1:
                continue

            if len(snap) < len(tmp):
                snap = tmp

            if a[i] == tmp[0]:
                tmp = tmp[1:] + a[i]
                res[a[i]] = i
                cur_i += 1
    
            elif a[i] == tmp[-1]:
                tmp = a[i]
                res = {a[i]: i}
                cur_i = i

            else:
                same_value_index = res[a[i]]
                for j in range(cur_i, same_value_index):
                    if j in res:
                        res.pop(j)
                res[a[i]] = i
                cur_i = same_value_index + 1
                tmp = tmp[same_value_index + 1:] + a[i]

    return snap if len(snap)>len(tmp) else tmp

 

[LeetCode] 5. Longest Palindromic Substring 最长回文子串 (Medium)

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"
class Solution(object):
    def __init__(self):
        self.snap = ""
        self.center = ""

    def palindromic(self, a):
        if len(a) == 1:
            return a
        # 每个点为中心扩散
        for i in range(1, len(a) - 1):
            self.center = a[i]
            l = i-1
            r = i+1
            while l >= 0 and r <= len(a)-1:
                # center左右两边是否相等
                if a[l] == a[r]:
                    self.center = a[l] + self.center + a[r]
                    l -= 1
                    r += 1
                    if len(self.center) > len(self.snap):
                        self.snap = self.center
                # center是一个字符和左右是否有相等的
                elif len(self.center) == 1 and (self.center == a[l] or self.center == a[r]):
                    if self.center == l:
                        l -= 1
                    else:
                        r += 1
                    self.center *= 2
                    if len(self.center) > len(self.snap):
                        self.snap = self.center
                else:
                    break

        return self.snap if len(self.snap) > len(self.center) else self.center

 

[LeetCode] 7. Reverse Integer 翻转整数
 

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321
Example 2:

Input: -123
Output: -321

 

class Solution(object):
    def reverse_int(self, a):
        sign = 1 if a >= 0 else -1
        a = abs(a)
        b = 0
        while a != 0:
            b = b * 10 + a % 10
            if a // 10 == 0:
                break
            else:
                a = a // 10

        return b*sign

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值