【LeetCode-Algorithm】【4-Median of Two Sorted Arrays】【Python】

问题:

There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5
这道题的思路相对来说非常简单:首先将两个数组变成一个数组,再对数组进行排序,排序后再取中值。但是这道题非常奇葩,奇葩再哪呢?请听下文:
在LeetCode上提交代码之前,我首先都会在Pycharm上运行一下是不是能正确。于是乎,我的代码如下:
 
  
但是运行的结果是错的,return (nums[length / 2 - 1] + nums[length / 2]) / 2.0错误,原因是 list indices must be integers or slices, not float
于是乎:我按照要求把代码改成如下,主要是把nums里面的数变成了整型:
 
  
结果输出了:2.5 正确
于是我将代码提交到LeetCode系统上,但是不通过,因为它的输出结果为2,我也不知道为什么。
我还测试了最原始的代码,就是上文出现错误的代码,把它提交 LeetCode系统上,竟然正确并且通过了,什么情况??????
Pycharm通过的LeetCode不能通过,LeetCode通过的Pycharm不能通过!!!!总之,思路对了就行了
 
  
 
  
 
  
 
  

你可能感兴趣的:(Python)