pythonmylist = [0,1,2,3,4,5,6,7,8]
mylist[-6:-1]
[3, 4, 5, 6, 7]
name不固定
json{
"code": "233",
"data": [{
"name": "1111",
"values": "asdasd"
}, {
"name": "2222",
"values": "dddeee"
}
]
}
jsonpath:$.data[?(@.name == '1111')].values
Given four integer arrays nums1
, nums2
, nums3
, and nums4
all of length n
, return the number of tuples (i, j, k, l)
such that:
0 <= i, j, k, l < n
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
Example 1:
Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] Output: 2 Explanation: The two tuples are: 1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
Example 2:
Input: nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] Output: 1
pythonclass Solution:
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
'''
用字典哈希表,存储前两个数组的两两之和的个数
再循环后两个数组,如果两两之和的相反数在字典中
count加相反数在字典中的值,返回count
'''
record, count = {}, 0
for i in nums1:
for j in nums2:
if i+j in record:
record[i+j] +=1
else:
record[i+j] =1
for k in nums3:
for l in nums4:
if -(k+l) in record:
count += record[-(k+l)]
return count
# record = {}
# for i in nums1:
# for j in nums2:
# if i+j in record:
# record[i+j] += 1
# else:
# record[i+j] = 1
# count = 0
# for x in nums3:
# for y in nums4:
# if -(x+y) in record:
# count += record[-(x+y)]
# return count
pythondef maxProfit(prices):
'''
121. Best Time to Buy and Sell Stock
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
先定义最大利润为0,最小价为列表第一位
循环遍历价格
定义当前利润等于循环的价格减去最小价
如果当前利润大于最大利润,替换最大利润为当前利润
如果当前循环价格小于最小价,替换最小价等于当前循环价格
循环结束,返回最大利润
'''
maxProfit, minPrice= 0, prices[0]
for price in prices:
curProfit = price - minPrice
if curProfit > maxProfit:
maxProfit = curProfit
elif price < minPrice:
minPrice = price
return maxProfit
'''
定义最大利润,最小价格,
循环体内当前最大利润比较,当前最小价格比较
'''
minprice, maxprofit = float('inf'), 0
for i in prices:
maxprofit = max(maxprofit, i-minprice)
minprice = min(i, minprice)
return maxprofit