Python每日一练(20230417)
目录
1. 最大间距 🌟🌟🌟
2. Z 字形变换 🌟🌟
3. 买卖股票的最佳时机 II 🌟🌟
🌟 每日一练刷题专栏 🌟
Golang每日一练 专栏
Python每日一练 专栏
C/C++每日一练 专栏
Java每日一练 专栏
1. 最大间距
给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值。
如果数组元素个数小于 2,则返回 0。
示例 1:
输入: [3,6,9,1] 输出: 3 解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3。
示例 2:
输入: [10] 输出: 0 解释: 数组元素个数小于 2,因此返回 0。
说明:
- 你可以假设数组中所有元素都是非负整数,且数值在 32 位有符号整数范围内。
- 请尝试在线性时间复杂度和空间复杂度的条件下解决此问题。
出处:
https://edu.csdn.net/practice/25797443
代码:
class Solution(object):def maximumGap(self, nums):if len(nums) < 2:return 0min_val, max_val = min(nums), max(nums)if min_val == max_val:return 0n = len(nums) + 1step = (max_val - min_val) // nexist = [0 for _ in range(n + 1)]max_num = [0 for _ in range(n + 1)]min_num = [0 for _ in range(n + 1)]for num in nums:idx = self.findBucketIndex(num, min_val, max_val, n)max_num[idx] = num if not exist[idx] else max(num, max_num[idx])min_num[idx] = num if not exist[idx] else min(num, min_num[idx])exist[idx] = 1res = 0pre = max_num[0]for i in range(1, n + 1):if exist[i]:res = max(res, min_num[i] - pre)pre = max_num[i]return resdef findBucketIndex(self, num, min_val, max_val, n):return int((num - min_val) * n / (max_val - min_val))return max(depth)
# %%
s = Solution()
nums = [3,6,9,1]
print(s.maximumGap(nums))
输出:
3
2. Z 字形变换
将一个给定字符串 s
根据给定的行数 numRows
,以从上往下、从左到右进行 Z 字形排列。
比如输入字符串为 "PAYPALISHIRING"
行数为 3
时,排列如下:
P A H N A P L S I I G Y I R
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"
。
请你实现这个将字符串进行指定行数变换的函数:
string convert(string s, int numRows);
示例 1:
输入:s = "PAYPALISHIRING", numRows = 3 输出:"PAHNAPLSIIGYIR"
示例 2:
输入:s = "PAYPALISHIRING", numRows = 4 输出:"PINALSIGYAHRPI" 解释: P I N A L S I G Y A H R P I
示例 3:
输入:s = "A", numRows = 1 输出:"A"
提示:
1 <= s.length <= 1000
s
由英文字母(小写和大写)、','
和'.'
组成1 <= numRows <= 1000
出处:
https://edu.csdn.net/practice/25797444
代码:
import math
class Solution:def convert(self, s: str, numRows: int) -> str:n = len(s)N = numRowsif n == 1 or N == 1:return sS = N-2C = 2*N-2R = int(math.floor(n/C))RS = n % (C)CE = n-R*CRR = 1 if (RS <= N) else 1+(RS-N)RX = R*(N-1) + RRoutput = []i = 0while i < N:j = 0k = (N-1-i)while j < RX:r = int(math.floor(j/(N-1)))rs = j % (N-1)offset = i if rs == 0 else N+rs-1index = r*C+offsetif index < len(s):output.append(s[index])if i > 0 and i < N-1:r = int(math.floor(k/(N-1)))rs = k % (N-1)offset = i if rs == 0 else N+rs-1index = r*C+offsetif index < len(s):output.append(s[index])j += (N-1)k += (N-1)i += 1return ''.join(output)
# %%
s = Solution()
print(s.convert('PAYPALISHIRING', 3))
print(s.convert('PAYPALISHIRING', 4))
输出:
PAHNAPLSIIGYIR
PINALSIGYAHRPI
3. 买卖股票的最佳时机 II
给定一个数组 prices
,其中 prices[i]
是一支给定股票第 i
天的价格。
设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
输入: prices = [7,1,5,3,6,4] 输出: 7 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。 随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
示例 2:
输入: prices = [1,2,3,4,5] 输出: 4 解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。 注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
示例 3:
输入: prices = [7,6,4,3,1] 输出: 0 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
提示:
1 <= prices.length <= 3 * 10^4
0 <= prices[i] <= 10^4
出处:
https://edu.csdn.net/practice/25797445
代码:
class Solution(object):def maxProfit(self, prices):""":type prices: List[int]:rtype: int"""hold = 0pric = []temp = []flag = 0msum = 0if len(prices) <= 2:if not prices:return 0if len(prices) == 1:return 0if prices[0] > prices[1]:return 0if prices[0] < prices[1]:return prices[1] - prices[0]for i in range(len(prices) - 1):if prices[i + 1] > prices[i] and hold != 1:hold = 1flag = icontinueif prices[i + 1] < prices[i] and hold == 1:pric.append(prices[flag])pric.append(prices[i])hold = 0else:continuefor i in range(0, len(pric), 2):temp.append(pric[i + 1] - pric[i])msum = sum(temp)if hold == 1:msum = msum + prices[-1] - prices[flag]return msum
# %%
s = Solution()
prices = [7,1,5,3,6,4]
print(s.maxProfit(prices))
prices = [1,2,3,4,5]
print(s.maxProfit(prices))
prices = [7,6,4,3,1]
print(s.maxProfit(prices))
输出:
7
4
0
🌟 每日一练刷题专栏 🌟
✨ 持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/
![]() |
Golang每日一练 专栏 |
![]() |
Python每日一练 专栏 |
![]() |
C/C++每日一练 专栏 |
![]() |
Java每日一练 专栏 |