This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Write an efficient algorithm that searches for a target value in an m x n integer matrix. The matrix has the following properties: | |
Integers in each row are sorted in ascending from left to right. | |
Integers in each column are sorted in ascending from top to bottom. | |
Example 1: | |
Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5 | |
Output: true | |
Example 2: | |
Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: | |
Integers in each row are sorted from left to right. | |
The first integer of each row is greater than the last integer of the previous row. | |
Example 1: | |
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 | |
Output: true | |
Example 2: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length. | |
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. | |
Clarification: | |
Confused why the returned value is an integer but your answer is an array? | |
Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller as well. | |
Internally you can think of this: | |
// nums is passed in by reference. (i.e., without making a copy) | |
int len = removeDuplicates(nums); | |
// any modification to nums in your function would be known by the caller. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. | |
The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has a size equal to m + n such that it has enough space to hold additional elements from nums2. | |
Example 1: | |
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 | |
Output: [1,2,2,3,5,6] | |
Example 2: | |
Input: nums1 = [1], m = 1, nums2 = [], n = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. | |
The overall run time complexity should be O(log (m+n)). | |
Example 1: | |
Input: nums1 = [1,3], nums2 = [2] | |
Output: 2.00000 | |
Explanation: merged array = [1,2,3] and median is 2. | |
Example 2: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become: | |
[4,5,6,7,0,1,4] if it was rotated 4 times. | |
[0,1,4,4,5,6,7] if it was rotated 7 times. | |
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. | |
Given the sorted rotated array nums that may contain duplicates, return the minimum element of this array. | |
Example 1: | |
Input: nums = [1,3,5] | |
Output: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: | |
[4,5,6,7,0,1,2] if it was rotated 4 times. | |
[0,1,2,4,5,6,7] if it was rotated 7 times. | |
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. | |
Given the sorted rotated array nums of unique elements, return the minimum element of this array. | |
You must write an algorithm that runs in O(log n) time. | |
Example 1: | |
Input: nums = [3,4,5,1,2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given the head of a singly linked list, reverse the list, and return the reversed list. | |
Example 1: | |
Input: head = [1,2,3,4,5] | |
Output: [5,4,3,2,1] | |
Example 2: | |
Input: head = [1,2] | |
Output: [2,1] | |
Example 3: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. | |
Example 1: | |
Input: l1 = [1,2,4], l2 = [1,3,4] | |
Output: [1,1,2,3,4,4] | |
Example 2: | |
Input: l1 = [], l2 = [] | |
Output: [] | |
Example 3: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given head, the head of a linked list, determine if the linked list has a cycle in it. | |
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter. | |
Return true if there is a cycle in the linked list. Otherwise, return false. | |
Example 1: | |
Input: head = [3,2,0,-4], pos = 1 | |
Output: true | |
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed). |