**
Leetcode 编程每日练习笔记
**
0315
1. 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
解法1
def solution(nums,target):
if len(nums) < 2:
return
for i in range(0,len(nums) - 1):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i,j]
解法2
def solution(nums,target):
#新建立一个空字典用来保存数值及其在列表中对应的索引
dict1 = { }
#遍历一遍列表对应的时间复杂度为O(n)
for i in range(0,len(nums)):
num = target - nums[i]
if num not in dict1:
dict1[nums[i]] = i
#如果在字典中则返回
else:
return [dict1[num],i]
print(solution([0,2,4,5,7],9))
def solution(nums,target):
#新建立一个空字典用来保存数值及其在列表中对应的索引
dict1 = { }
#遍历一遍列表对应的时间复杂度为O(n)
for i in range(0,len(nums)):
num = target - nums[i]
if num not in dict1:
dict1[nums[i]] = i
#如果在字典中则返回
else:
return [dict1[num],i]
print(solution([0,2,4,5,7],9))
解法3
class Soultion:
def twoSum(self,nums,target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
sorted_id = sorted(range(len(nums)), key = lambda k:nums[k])
head = 0
tail = len(nums) - 1
sum_result = nums[sorted_id[head]] + nums[sorted_id[tail]]
while sum_result != target:
if sum_result > target:
tail -= 1
elif sum_result < target:
head += 1
sum_result = nums[sorted_id[head]] + nums[sorted_id[tail]]
return [sorted_id[head], sorted_id[tail]]
a = Soultion()
b = list([2,7,11,15])
print(a.twoSum(b,9))
2 237 删除链表中的节点
请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。
说明:
• 链表至少包含两个节点。
• 链表中所有节点的值都是唯一的。
• 给定的节点为非末尾节点并且一定是链表中的一个有效节点。
• 不要从你的函数中返回任何结果。
• 示例 1:
• 输入: head = [4,5,1,9], node = 5
• 输出: [4,1,9]
• 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
解决方法1:
class Solution:
def deleteNode(self, node):
“”"
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
“”"
node.val = node.next.val
node.next = node.next.next
0319
970 强整数
给定两个正整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个强整数。
返回值小于或等于 bound 的所有强整数组成的列表。
你可以按任何顺序返回答案。在你的回答中,每个值最多出现一次。
示例 1:
输入:x = 2, y = 3, bound = 10
输出:[2,3,4,5,7,9,10]
解释:
2 = 2^0 + 3^0
3 = 2^1 + 3^0
4 = 2^0 + 3^1
5 = 2^1 + 3^1
7 = 2^2 + 3^1
9 = 2^3 + 3^0
10 = 2^0 + 3^2
解法1
class Solution:
def powerfulIntegers(self, x, y, bound):
“”"
:type x: int
:type y: int
:type bound: int
:rtype: List[int]
“”"
i = 1 if x == 1 else int(math.log(bound, x)) + 1
j = 1 if y == 1 else int(math.log(bound, y)) + 1
res = set()
for m in range(i):
for n in range(j):
tmp = x**m + y**n
if tmp <= bound:
res.add(tmp)
return list(res)
解法2:
class Solution:
def powerfulIntegers(self, x, y, bound):
“”"
:type x: int
:type y: int
:type bound: int
:rtype: List[int]
“”"
res = set()
i = bound if x == 1 else x
j = bound if y == 1 else y
m = 1
while m < bound:
n = 1
while n + m <= bound:
res.add(n + m)
n *= j
m *= i
return list(res)
0322 2 两数相加
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
解法一:
Definition for singly-linked list.
class ListNode:
def init(self, x):
self.val = x
self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
“”"
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
“”"
re = ListNode(0)
r = re
carry = 0
while(l1 or l2):
x= l1.val if l1 else 0
y= l2.val if l2 else 0
s= carry + x + y
carry = s//10
r.next = ListNode(s%10)
r = r.next
if(l1 != None):l1 = l1.next
if(l2 != None):l2 = l2.next
if(carry>0):
r.next = ListNode(1)
return re.next
- 无重复字符的最长子串
- 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
解决方法1:
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
st = {}
i, ans = 0, 0
for j in range(len(s)):
if s[j] in st:
i = max(st[s[j]], i)
ans = max(ans, j - i + 1)
st[s[j]] = j + 1
return ans