腾讯精选50题

腾讯精选50题

2. 两数相加(1/50) 中等

https://leetcode-cn.com/problems/add-two-numbers/ 链表

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class  Solution:
def  addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
n = 0
d = ListNode()
p = d
p1 = l1
p2 = l2
while p1 or p2 or n:
num = 0
if p1:
num+=p1.val
p1 = p1.next
if p2:
num+=p2.val
p2 = p2.next
if n:
num+=n
n-=1
n,r = divmod(num,10)
p.next = ListNode(r)
p = p.next
return d.next

4. 寻找两个有序数组的中位数(2/50) 困难

https://leetcode-cn.com/problems/median-of-two-sorted-arrays/

未解决

5. 最长回文子串(3/50) 中等

https://leetcode-cn.com/problems/longest-palindromic-substring/ 动态规划 双指针

class  Solution:
def  longestPalindrome(self, s: str) -> str: #检查是否是回文字符串
ans = ""
def  find(s,l,r):
while (l>=0  and r<len(s) and s[l]==s[r]):
l-=1
r+=1
return s[l+1:r]
for i in  range(len(s)):
s1 = find(s,i,i) #奇数长度
s2 = find(s,i,i+1) #偶数长度
if  len(s1)>len(ans):
ans = s1
if  len(s2)>len(ans):
ans = s2
return ans

7.整数反转(4/50) 中等

https://leetcode-cn.com/problems/reverse-integer/

class Solution:
    def reverse(self, x: int) -> int:
        s = int(str(abs(x))[::-1])
        if x<0:
            s = -s
        if s>2**31-1 or s<(-2)**31:
            return 0
        return s

8.字符串转整数(atoi) (5/50)中等

未解决

9.回文数(6/50) 简单

https://leetcode-cn.com/problems/palindrome-number/

class Solution:
    def isPalindrome(self, x: int) -> bool:
        return str(x) == str(x)[::-1]
全部评论

相关推荐

牛客868257804号:九个中铁八个中建
点赞 评论 收藏
分享
牛客771574427号:恭喜你,华杰
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务