20201229
"" given an array of integers nums and a target value target, find the two integers in the array and the target values and return their array subscripts. You can assume that there is only one answer for each type of input. However, the same element in an array cannot be used twice. https://leetcode-cn.com/problems/two-sum/ """
def twoSum(nums,target) :
for i in range(0.len(nums)):
for j in range(0.len(nums)):
ifi! =jand nums[i] + nums[j] == target:
return [i,j]
class Solution:
def twoSum(self, nums: List[int], target: int) - >List[int] :
dct = {}
for i, n in enumerate(nums):
cp = target - n
if cp in dct:
return [dct[cp], i]
else:
dct[n] = i
# print (twoSum 4-trichlorobenzene [3], (6))
print(twoSum_1([2.7.11.15].9))
Given a string, find the length of the smallest string that does not contain repeating characters. https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ """
def lengthOfLongestSubstring(s) :
# thought
# an s, save it to see if the next string is in my inventory. If so, retrieve the inventory index to get the length
s_len = len(s)
temproary_s = ""
index_l = 0
for index in range(s_len):
if s[index] in temproary_s:
old_s_index = len(temproary_s) - temproary_s.index(s[index])
index_l = index-old_s_index+2 if index-old_s_index+2 > index_l else index_l
temproary_s = s[index] + temproary_s
index_l = index_l ifindex_l ! =0 else s_len
return index_l
Copy the code
Off work. Bye
20201230
"" Given two positively ordered (from smallest to largest) arrays of size m and n, nums1 and nums2. Please find and return the median of the two positive ordinal groups. https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ """
def findMedianSortedArrays(nums1: list, nums2: list) :
temporary = sorted(nums1 + nums2, key=lambda x: x, reverse=False)
if temporary:
len_t = len(temporary)
print(temporary)
if len_t % 2= =0: # Even
return (temporary[len_t // 2] + temporary[len_t // 2 - 1) /2
else:
return temporary[len_t // 2]
else:
return []
"" variant: find the median of the two arrays first, then find the median of the combined array.
def findMedianSortedArrays_2(nums1: list, nums2: list) :
temporary = []
while True:
if temporary:
len_t = len(temporary)
print(temporary)
if len_t % 2= =0: # Even
return (temporary[len_t // 2] + temporary[len_t // 2 - 1) /2
else:
return temporary[len_t // 2]
len_n1 = len(nums1)
len_n2 = len(nums2)
l_n1 = [nums1[len_n1 // 2], nums1[len_n1 // 2 - 1]] if len_n1 % 2= =0 and len_n1 > 1 else [
nums1[len_n1 // 2]] iflen_n1 ! =0 else []
l_n2 = [nums2[len_n2 // 2], nums2[len_n2 // 2 - 1]] if len_n2 % 2= =0 and len_n2 > 1 else [
nums2[len_n2 // 2]] iflen_n2 ! =0 else []
temporary = sorted(l_n1 + l_n2, key=lambda x: x, reverse=False)
Copy the code