leetcode 57. 插入区间 58. 最后一个单词的长度
作者:互联网
leetcode 57. 插入区间 58. 最后一个单词的长度
57. 插入区间
难度中等512收藏分享切换为英文接收动态反馈
给你一个 无重叠的 *,*按照区间起始端点排序的区间列表。
在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。
示例 1:
输入:intervals = [[1,3],[6,9]], newInterval = [2,5]
输出:[[1,5],[6,9]]
示例 2:
输入:intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
输出:[[1,2],[3,10],[12,16]]
解释:这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠。
示例 3:
输入:intervals = [], newInterval = [5,7]
输出:[[5,7]]
示例 4:
输入:intervals = [[1,5]], newInterval = [2,3]
输出:[[1,5]]
示例 5:
输入:intervals = [[1,5]], newInterval = [2,7]
输出:[[1,7]]
提示:
0 <= intervals.length <= 104
intervals[i].length == 2
0 <= intervals[i][0] <= intervals[i][1] <= 105
intervals
根据intervals[i][0]
按 升序 排列newInterval.length == 2
0 <= newInterval[0] <= newInterval[1] <= 105
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# @Author : mtl
# @Desc : ***
# @File : 57.py
# @Software: PyCharm
from typing import List
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
length = len(intervals)
if length == 0:
return [newInterval]
if intervals[-1][1] < newInterval[0]:
intervals.append(newInterval)
return intervals
if newInterval[1] < intervals[0][0]:
intervals.insert(0, newInterval)
return intervals
i, j = 0, 0
while i < length:
if intervals[i][1] >= newInterval[0]:
j = i
while j <= length:
if j == length or newInterval[1] < intervals[j][0]:
j -= 1
break
if intervals[j][1] > newInterval[1]:
break
j += 1
intervals[i:j + 1] = [[min(intervals[i][0], newInterval[0]), max(intervals[j][1], newInterval[1])]]
break
i += 1
return intervals
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
length = len(intervals)
if length == 0:
return [newInterval]
left, right = 0, 0
for i in range(length):
left = i
if intervals[i][1] >= newInterval[0]:
break
for i in range(length - 1, -1, -1):
right = i
if intervals[i][0] <= newInterval[1] <= intervals[i][1] or intervals[i][1] <= newInterval[1]:
break
if left == right:
if newInterval[1] < intervals[left][0]:
intervals.insert(0, newInterval)
elif newInterval[0] > intervals[left][1]:
intervals.append(newInterval)
else:
intervals[left] = [min(intervals[left][0], newInterval[0]), max(intervals[left][1], newInterval[1])]
else:
intervals[left: right + 1] = [[min(intervals[left][0], newInterval[0]), max(intervals[right][1], newInterval[1])]]
return intervals
if __name__ == '__main__':
intervals = [[1, 3], [6, 9]]
newInterval = [2, 5]
intervals = [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]]
newInterval = [4, 8]
# print(intervals[0:2])
# intervals[0:2] = newInterval
# print(intervals)
# intervals = [[1, 5]]
# newInterval = [2, 7]
# intervals = [[1, 5]]
# newInterval = [6, 8]
# intervals = [[1, 5]]
# newInterval = [0, 0]
intervals = []
newInterval = [5, 7]
intervals = [[1, 3], [6, 9]]
newInterval = [2, 5]
# intervals = [[1, 5], [6, 8]]
# newInterval = [5, 6]
# intervals = [[1, 5], [6, 8]]
# newInterval = [0, 9]
# intervals = [[2, 6], [7, 9]]
# newInterval = [15, 18]
print(Solution().insert(intervals, newInterval))
58. 最后一个单词的长度
难度简单404收藏分享切换为英文接收动态反馈
给你一个字符串 s
,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中最后一个单词的长度。
单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。
示例 1:
输入:s = "Hello World"
输出:5
示例 2:
输入:s = " fly me to the moon "
输出:4
示例 3:
输入:s = "luffy is still joyboy"
输出:6
提示:
1 <= s.length <= 104
s
仅有英文字母和空格' '
组成s
中至少存在一个单词
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# @Author : mtl
# @Desc : ***
# @File : 58.py
# @Software: PyCharm
class Solution:
def lengthOfLastWord(self, s: str) -> int:
ans = 0
s = s.strip()
for i in range(len(s) - 1, -1, -1):
if s[i] == " ":
break
ans += 1
return ans
def lengthOfLastWord(self, s: str) -> int:
ans = 0
for i in range(len(s) - 1, -1, -1):
if s[i] == " " and ans > 0:
break
if s[i] != " ":
ans += 1
return ans
if __name__ == '__main__':
s = "Hello World"
s = " fly me to the moon "
s = "luffy is still joyboy"
s = " fly me to the moon "
# s = "Today is a nice day"
# s = " a"
print(Solution().lengthOfLastWord(s))
标签:__,newInterval,58,示例,57,List,length,intervals,leetcode 来源: https://blog.csdn.net/mtl1994/article/details/121797585