Posts
Product of Array Except Self
PROBLEM Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
Example 1 **Input**: nums = [1,2,3,4] **Output**: [24,12,8,6] Example 2 **Input**: nums = [-1,1,0,-3,3] **Output**: [0,0,9,0,0] SOLVING We are going to make to iteration.
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 Bits
PROBLEM Reverse bits of a given 32 bits unsigned integer.
SOLVING We’ll use Bit Manipulation method
Steps Create a uint32_t result we’ll return as the answer Repeat 32 time or the longer of your bit result equal to result or (| or bit operation) on the result of n and (& and bit operation) 1 to get the first bit Example Example with 6bit (32 is tooooo long) 011101
=n= [011101] & 1 == 1 =result= [000000] << 1 =n= [001110] & 1 == 0 =result= [000001] << 0 =n= [000111] & 1 == 1 =result= [000010] << 1 =n= [000011] & 1 == 1 =result= [000101] << 1 =n= [000001] & 1 == 1 =result= [001011] << 1 =n= [000000] & 1 == 0 =result= [010111] << 0 =result= [101110] Code class Solution { public: uint32_t reverseBits(uint32_t n) { uint32_t result; for (int i = 0; i < 32; i++) { result = (result << 1) | (n & 1); n >>= 1; } return result; } };
read morePosts
Reverse Linked List
PROBLEM Given the head of a singly Linked List, reverse the list, and return the reversed list.
Example1 *Input*: head = [1,2,3,4,5] *Output*: [5,4,3,2,1] Example2 *Input*: head = [1,2] *Output*: [2,1] Example3 *Input*: head = [] *Output*: [] SOLVING Steps Create prevNode and nextNode to solve this problem nextNode equal head next N head next equal prevNode H prevNode equal head P head equal nextNode return prevNode Example1 P H N ↓ ↓ ↓ NULL [1 -> 2 -> 3 -> 4 -> 5 -> NULL] head next equal prevNode head equal next P H N ↓ ↓ ↓ [NULL <- 1 2 -> 3 -> 4 -> 5 -> NULL] head next equal prevNode head equal next P H N ↓ ↓ ↓ [NULL <- 1 <- 2 3 -> 4 -> 5 -> NULL] head next equal prevNode head equal next P H N ↓ ↓ ↓ [NULL <- 1 <- 2 <- 3 4 -> 5 -> NULL] head next equal prevNode head equal next P H N ↓ ↓ ↓ [NULL <- 1 <- 2 <- 3 <- 4 5 -> NULL] head next equal prevNode head equal next P H N ↓ ↓ ↓ [NULL <- 1 <- 2 <- 3 <- 4 <- 5] NULL] Return prevNode Code class Solution { public: ListNode *reverseList(ListNode *head) { ListNode *nextNode = nullptr; ListNode *prevNode = nullptr; while (head) { nextNode = head->next; head->next = prevNode; prevNode = head; head = nextNode; } return prevNode; } };
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
Rotting Oranges
PROBLEM You are given a m x n grid where each cell can have one of three values:
0 representing an empty cell, 1 representing a fresh orange, or 2 representing a rotten orange. Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.
Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.
read morePosts
Search a 2D Matrix
PROBLEM You are given an m x n integer matrix matrix with the following two properties:
Each row is sorted in non-decreasing order. The first integer of each row is greater than the last integer of the previous row. Given an integer target, return true if target is in matrix or false otherwise.
You must write a solution in O(log(m * n)) time complexity.
Example1 *Input*: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 *Output*: true Example2 *Input*: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 *Output*: false SOLVING we’ll use Binary Search exact value to solve this problem
read morePosts
Search Insert
PROBLEM In an sorted array [1, 3, 5, 6, 8], you want to know the index to insert your target, 5 In an sorted array [1, 3, 5, 6, 8], you want to know the index to insert your target, 2 SOLVING Use of Binary Search method.
This code will return the index 2, where the target already is This code will return the index 1 where the target should be Steps Place Left Pointer at 0 and Right Pointer to the last element Until Left Element is inferior to Right Element: Get the element between left and right Middle Index=(Left Index - RIght Index) / 2 If Middle Element equal the target, return If it’s inferior, Left Pointer becomes the Middle Pointer Otherwise it’s superior, and so Right Pointer become the Middle Pointer If the loop is finished return the Left Pointer Code #include <iostream> #include <vector> int searchInsert(std::vector<int> &nums, int target) { int l = 0; int r = nums.
read more