Skip to main content

Posts

Showing posts with the label LeetCode

LeetCode Algorithm Questions by Python

4. Median of Two Sorted Arrays There are two sorted arrays  nums1  and  nums2  of size m and n respectively. Find the median of the two sorted arrays. 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 The idea is to pick up the numbers in nums2 that are smaller than a number in nums1, until we get the median. Pay attention: 1. consider three cases: odd, even, and null; 2. max of nums1 may smaller than min of nums2; 3. use float() while doing the division. My code in Python is as follows: class Solution(object):     def findMedianSortedArrays(self, nums1, nums2):         """         :type nums1: List[int]         :type nums2: List[int]         :rtype: float         """         m = len(nums1)         n = len(nums...

LeetCode Database Questions by MySQL

175. Combine Two Tables Table:  Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId is the primary key column for this table. Table:  Address +-------------+---------+ | Column Name | Type | +-------------+---------+ | AddressId | int | | PersonId | int | | City | varchar | | State | varchar | +-------------+---------+ AddressId is the primary key column for this table. Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people: FirstName, LastName, City, State My Solution: # Write your MySQL query statement below select FirstName, LastName, City, State from Person left join Address on Person.PersonId = Address.PersonId; 176. Second Highest Salary Write a SQL query to get the ...