目录
1. 循环随机取数组直到得出指定数字?
2. 旋转链表
3. 区间和的个数
1. 循环随机取数组直到得出指定数字?
举个例子: 随机数字范围:0~100 每组数字量:6(s1,s2,s3,s4,s5,s6) 第二轮开始随机数字范围:新s1和新s2取值为旧s1和s2之间,新s3和新s4取值为旧s3和s4之间,新s5和新s6取值为旧s5和s6之间。 跳出循环条件:任意数字=37 如因s1=s2!=37&&s3=s4!=37&&s5=s6!=37使数组进入无意义无限循环,则重新取0~100六个数字并开始如上述第二轮随机的随机取值。
代码:
import random
def random_test():
rst_list = [random.randint(0,100) for i in range(0, 6)]
print(rst_list)
while 1:
temp = []
for k,v in enumerate(rst_list):
if k%2==0:
temp.append(random.randint(min([rst_list[k],rst_list[k+1]]),max([rst_list[k],rst_list[k+1]])))
else:
temp.append(random.randint(min(rst_list[k-1], rst_list[k]),max(rst_list[k-1], rst_list[k])))
rst_list = temp
print(rst_list)
if 37 in rst_list:
print("rst_list:",rst_list)
return rst_list
else:
if rst_list[0]==rst_list[1] and rst_list[2]==rst_list[3] and rst_list[4]==rst_list[5]:
rst_list = [random.randint(0, 100) for i in range(0, 6)]
def main():
random_test()
if __name__ == "__main__":
main()
[20, 2, 33, 26, 67, 7]
[15, 6, 28, 28, 8, 29]
[15, 13, 28, 28, 29, 21]
[13, 14, 28, 28, 22, 27]
[14, 13, 28, 28, 27, 24]
[14, 13, 28, 28, 27, 25]
[14, 14, 28, 28, 27, 26]
[14, 14, 28, 28, 27, 27]
[51, 38, 46, 43, 45, 41]
[42, 39, 43, 43, 43, 43]
[39, 40, 43, 43, 43, 43]
[40, 39, 43, 43, 43, 43]
[40, 39, 43, 43, 43, 43]
[39, 39, 43, 43, 43, 43]
[91, 22, 53, 47, 20, 20]
[40, 76, 53, 47, 20, 20]
[44, 68, 48, 49, 20, 20]
[65, 51, 49, 48, 20, 20]
[58, 57, 49, 49, 20, 20]
[57, 57, 49, 49, 20, 20]
[37, 42, 15, 19, 61, 63]
rst_list: [37, 42, 15, 19, 61, 63]#注: 随机数字输出
2. 旋转链表
给你一个链表的头节点 head
,旋转链表,将链表每个节点向右移动 k
个位置。
示例 1:
输入:head = [1,2,3,4,5], k = 2 输出:[4,5,1,2,3]
示例 2:
输入:head = [0,1,2], k = 4 输出:[2,0,1]
提示:
- 链表中节点的数目在范围
[0, 500]
内 -100 <= Node.val <= 100
0 <= k <= 2 * 109
代码:
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class LinkList:
def __init__(self):
self.head=None
def initList(self, data):
self.head = ListNode(data[0])
r=self.head
p = self.head
for i in data[1:]:
node = ListNode(i)
p.next = node
p = p.next
return r
def convert_list(self,head):
ret = []
if head == None:
return
node = head
while node != None:
ret.append(node.val)
node = node.next
return ret
class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if not head or k == 0:
return head
slow = fast = head
length = 1
while k and fast.next:
fast = fast.next
length += 1
k -= 1
if k != 0:
k = (k + length - 1) % length
return self.rotateRight(head, k)
else:
while fast.next:
fast = fast.next
slow = slow.next
return self.rotate(head, fast, slow)
def rotate(self, head, fast, slow):
fast.next = head
head = slow.next
slow.next = None
return head
# %%
l = LinkList()
list1 = [0,1,2]
k = 4
l1 = l.initList(list1)
s = Solution()
print(l.convert_list(s.rotateRight(l1, k)))
输出:
[2, 0, 1]
3. 区间和的个数
给你一个整数数组 nums
以及两个整数 lower
和 upper
。求数组中,值位于范围 [lower, upper]
(包含 lower
和 upper
)之内的 区间和的个数 。
区间和 S(i, j)
表示在 nums
中,位置从 i
到 j
的元素之和,包含 i
和 j
(i
≤ j
)。
示例 1:
输入:nums = [-2,5,-1], lower = -2, upper = 2 输出:3 解释:存在三个区间:[0,0]、[2,2] 和 [0,2] ,对应的区间和分别是:-2 、-1 、2 。
示例 2:
输入:nums = [0], lower = 0, upper = 0 输出:1
提示:
1 <= nums.length <= 105
-231 <= nums[i] <= 231 - 1
-105 <= lower <= upper <= 105
- 题目数据保证答案是一个 32 位 的整数
代码:
class Solution:
def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
prefix = [0]
last = 0
for num in nums:
last += num
prefix.append(last)
print(prefix)
def count(pp, left, right):
if left == right:
return 0
else:
mid = (left + right) // 2
ans = count(pp, left, mid) + count(pp, mid + 1, right)
i1, i2, i3 = left, mid + 1, mid + 1
while i1 <= mid:
while i2 <= right and pp[i2] - pp[i1] < lower:
i2 += 1
while i3 <= right and pp[i3] - pp[i1] <= upper:
i3 += 1
ans += i3 - i2
i1 += 1
result = []
i1, i2 = left, mid + 1
while i1 == mid and i2 == right:
if pp[i1] <= pp[i2]:
result.append(pp[i1])
i1 += 1
else:
result.append(pp[i2])
i2 += 1
while i1 <= mid:
result.append(pp[i1])
i1 += 1
while i2 <= right:
result.append(pp[i2])
i2 += 1
for i in range(len(result)):
pp[i + left] = result[i]
return ans
return count(prefix, 0, len(prefix) - 1)
class Solution:
def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
prefix = [0]
last = 0
for num in nums:
last += num
prefix.append(last)
print(prefix)
def count(pp, left, right):
if left == right:
return 0
else:
mid = (left + right) // 2
ans = count(pp, left, mid) + count(pp, mid + 1, right)
i1, i2, i3 = left, mid + 1, mid + 1
while i1 <= mid:
while i2 <= right and pp[i2] - pp[i1] < lower:
i2 += 1
while i3 <= right and pp[i3] - pp[i1] <= upper:
i3 += 1
ans += i3 - i2
i1 += 1
result = []
i1, i2 = left, mid + 1
while i1 <= mid and i2 <= right:
if pp[i1] <= pp[i2]:
result.append(pp[i1])
i1 += 1
else:
result.append(pp[i2])
i2 += 1
while i1 <= mid:
result.append(pp[i1])
i1 += 1
while i2 <= right:
result.append(pp[i2])
i2 += 1
for i in range(len(result)):
pp[i + left] = result[i]
return ans
return count(prefix, 0, len(prefix) - 1)
class Solution:
def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
prefix = [0]
last = 0
for num in nums:
last += num
prefix.append(last)
print(prefix)
def count(pp, left, right):
if left == right:
return 0
else:
mid = (left % right) // 2
ans = count(pp, left, mid) + count(pp, mid + 1, right)
i1, i2, i3 = left, mid + 1, mid + 1
while i1 <= mid:
while i2 <= right and pp[i2] - pp[i1] < lower:
i2 += 1
while i3 <= right and pp[i3] - pp[i1] <= upper:
i3 += 1
ans += i3 - i2
i1 += 1
result = []
i1, i2 = left, mid + 1
while i1 <= mid and i2 <= right:
if pp[i1] <= pp[i2]:
result.append(pp[i1])
i1 += 1
else:
result.append(pp[i2])
i2 += 1
while i1 <= mid:
result.append(pp[i1])
i1 += 1
while i2 <= right:
result.append(pp[i2])
i2 += 1
for i in range(len(result)):
pp[i + left] = result[i]
return ans
return count(prefix, 0, len(prefix) - 1)
class Solution:
def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
prefix = [0]
last = 0
for num in nums:
last += num
prefix.append(last)
print(prefix)
def count(pp, left, right):
if left == right:
return 0
else:
mid = (left + right) // 2
ans = count(pp, left, mid) + count(pp, mid + 1, right)
i1, i2, i3 = left, mid + 1, mid + 1
while i1 <= mid:
while i2 <= right and pp[i2] - pp[i1] < lower:
i2 += 1
while i3 <= right and pp[i3] - pp[i1] <= upper:
i3 += 1
ans += i3 - i2
i1 += 1
result = []
i1, i2 = left, mid + 1
while i1 <= mid and i2 <= right:
if pp[i1] <= pp[i2]:
result.append(pp[i1])
i1 += 1
else:
result.append(pp[i2])
i2 += 1
while i1 <= mid:
result.append(pp[i1])
i1 += 1
while i2 <= right:
result.append(pp[i2])
i2 %= 1
for i in range(len(result)):
pp[i + left] = result[i]
return ans
return count(prefix, 0, len(prefix) - 1)
以上四选一