Below you will find pages that utilize the taxonomy term “TWOPOINTERS”
Posts
Interval List Intersections
PROBLEM You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, endi] and secondList[j] = [startj, endj]. Each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
A closed interval [a, b] (with a<=b) denotes the set of real numbers x with a<=x<=b.
The intersection of two closed intervals is a set of real numbers that are either empty or represented as a closed interval.
read morePosts
Move Zeroes
PROBLEM Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example1 *Input*: nums = [0,1,0,3,12] *Output*: [1,3,12,0,0] Example2 *Input*: nums = [0] *Output*: [0] Constraints 1<=nums.length<=104 -231<=nums[i]<=231-1 Follow up: Could you minimize the total number of operations done?
SOLVING We’ll use the Two Pointers method
read morePosts
Remove Duplicates from Sorted List II
PROBLEM Given the head of a sorted Linked List, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
Example1 *Input*: head = [1,2,3,3,4,4,5] *Output*: [1,2,5] SOLVING We’ll use Two Pointers method to solve this problem
Steps Create a Node dummy with val (0 for this example) pointing to head Create a Node prev equal to dummy Parcor the Linked List: If the current value and the next are the same: Loop until the node value is different prev->next equal to this node Otherwise move prev and head of a node Return the next of dummy (previously the pointer to head) Code class Solution { public: ListNode *deleteDuplicates(ListNode *head) { ListNode *dummy = new ListNode(0, head); ListNode *prev = dummy; while (head) { if (head->next && head->val == head->next->val) { while (head->next && head->val == head->next->val) head = head->next; prev->next = head->next; } else prev = prev->next; head = head->next; } return dummy->next; } };
read morePosts
Reverse Words
PROBLEM Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example1 *Input*: s = "Let's take LeetCode contest" *Output*: "s'teL ekat edoCteeL tsetnoc" Example2 *Input*: s = "God Ding" *Output*: "diG gniD" Constraints 1<=s.length<=5*104 s contains printable ASCII characters. s does not contain any leading or trailing spaces. There is at least one word in s. All the words in s are separated by a single space.
read morePosts
Rotate Array
PROBLEM Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Example 1 *Input*: nums = [1,2,3,4,5,6,7], k = 3 *Output*: [5,6,7,1,2,3,4] *Explanation*: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2 *Input*: nums = [-1,-100,3,99], k = 2 *Output*: [3,99,-1,-100] *Explanation*: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] SOLVING We’ll use the Two Pointers method
read morePosts
Square Of A Sorted Array
PROBLEM Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order
Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
Example1 *Input*: nums = [-4,-1,0,3,10] *Output*: [0,1,9,16,100] *Explanation*: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2 *Input*: nums = [-7,-3,2,3,11] *Output*: [4,9,9,49,121] SOLVING We will use Two Pointers technique.
read morePosts
Two Pointers
INFO The two pointers is a searching algorithm if you want at the same time:
Search two elements in an array Search and do something in an array Keep in mind an index when moving another Ect…
read morePosts
Two Sum || Array Sorted
PROBLEM Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1<=index1<index2<=numbers.length.
Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
The tests are generated such that there is exactly one solution.
read more