LeetCode Solution Guide Masterset (C++ Based)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Two Sum (1, Brute Force): Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

1. Brute Force Solution: Setup: - set a variable for the size of the array of integers - create a new int vector to hold values Solve: - create a nested loop through the array of integers (first is less than array size, second is less than first looping variable) - if they are the same, return the values Complexity: - Time O(n^2) - Space O(1)

Binary Search (1, Basic Method): Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n)runtime complexity.

10. Basic Method Solution: Solve: - create an additional int function, with parameters nums, target, start and end - call this int function with nums, target, 0 and nums.size() - 1 as parameters - if start > end, return -1. This means that the value couldn't be found in the binary search - within int function, set mid equal to (start + end)/2 - if target is greater than mid, recursively call int function with start one greater than target, and end - if target is less than mid, recursively call int function with end one less than target, and start - else, return mid Complexity: Time: O(log(n)) Space: O(1)

Two Sum (2, One Pass Hash Table): Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

2. One Pass Hash Table Solution: Setup: - create an unordered map with ints as key and value - create an int vector to hold values Solve: - create a for loop that runs through array - for each loop, create an int variable comp that is the value of the target minus current value - for each loop, use find function to check if comp exists. If so, add both i (loop variable) and the index of key comp within the unordered map - set the current index of the hash to I (loop variable) - return int vector Complexity: - Time: O(n) - Space: O(n) Probably Optimal?

Two Sum (3, Two Pass Hash Table): Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

3. Two Pass Hash Table Solution Setup: - create an unordered map with ints as keys and values - create an int vector to hold final results Solve: - go through given array and use spot in array as value, and the value in the array as the key in hash - go through given array again; find comp of each value - if comp corresponds to an index, add j (loop variable) and the contents at the comp spot in the hash to the int vector - return int vector Complexity: Time: O(n) Space: O(n) Easier Than 1 Pass, Second Best Optimality

Two Sum (4, Two Pointer Method): Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

4. Two Pointer Method Solution Setup: - keep track of size - create a copy of the initial vector - create an empty int vector Solve: - sort the copy of the original vector - create two ints to hold final values - create two ints (pointers), and set one equal to smallest value and the other equal to largest value using for loop (ends when left is greater than or equal to right, no last clause) - if small + large is less than val, increase small; if it is greater than val, decrease large - if they are equal, save the two values as ints - then, run a for loop on the initial vector to determine the indices of those values - add the two values to a vector and print that vector Complexity: Time: O(nlog(n)) Space: O(n)

Valid Palindrome (1, Stack Method): A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise.

5. Stack Method Solution Setup: - create a new string - create a stack of characters Solve Part #1 (Get Alphanum String): - check each value in the original string, and push it onto the newly created string if it is alphanumeric Solve Part #2: - if alphanumeric string is length 0 or 1 return true - otherwise, check if it is odd in length, and then delete middle if that is the case - add each value in first half of the string into stack - for second half of values, check each value and make sure that it corresponds to the top value on the stack. If not, return false. If so, pop the value and continue checking - check at the end and make sure the stack has no values (is empty) - return true if it reaches this point Complexity: Time : O(n) Space: O(n)

Valid Palindrome (2, Two Sides Method): A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise.

6. Two Sides Method Solution Setup: - create a new string Solve Part #1 (Get Alphanum String): - check each value in the original string, and push it onto the newly created string if it is alphanumeric Solve Part #2: - if length of the newly created string is 0 or 1, then return true - otherwise, create a for loop and check if values are the same starting at the beginning and the end of the new string; return false if not - if it gets to end, return true Complexity: Time: O(n) Space: O(n)

Valid Palindrome (3, Two Sides In-Place Method): A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise.

7. Two Sides In-Place Method Solution: Solve: - check if length of string is 0 or 1, and if so, return true - otherwise, enter a for loop with iterator i at beginning and iterator j at end, continues while i is less than j, with i++ and j-- - create two while loops; one checks if i is alphanumeric and is less than j, and the other checks if j is alphanumeric and greater than i. If these are true, increment i and j in the correct direction - after these, have an if statement that compares the lowercase versions of i and j; return false if they are different - if it gets to the end, return true Complexity: Time: O(n) Space: O(1) Probably Optimal?

Invert Binary Tree (1, Recursive Method): Given the root of a binary tree, invert the tree, and return its root.

8. Recursive Method Solution: Solve: - if root isn't nullptr, save left of root in a node - set left of root to right of root - set right of root to left of root saved in node - or, instead of previous two bullets, can use swap function and then set root->right and root->left values - at end, outside of if, return the root Complexity: - Time: O(n) - Space: O(1) Probably Optimal?

Invert Binary Tree (2, Iterative Method): Given the root of a binary tree, invert the tree, and return its root.

9. Iterative Method Solution: Solve: - if root is nullptr, return root - otherwise, create a queue and push root onto the queue - while queue is not empty, save front of queue and pop it. Then, swap the left and right of the saved front's children, and add each to the queue if they are not nullptr - return root Complexity: Time: O(n) Space: O(n)


Set pelajaran terkait

Sociology Exam 3: 10, 11, 12, 14

View Set

Chapter 2. Financial Statements, Taxes, and Cash Flow

View Set

Chapter 16- Social Responsibility and Sustainability

View Set

Psy150 - Chapter 9 - Lifespan development

View Set

Chapter 60: Nursing Management: Alzheimer's Disease, Dementia, and Delirium

View Set

Español 3B: Speaking Quiz [Question and Answer for WHY?] *ANSWERS MAY VARY*

View Set

Module 3, Unit 2 - Firewalls and Load Balancers

View Set

Soc, Sociology - Unit 1 Milestone, Introduction to Sociology - Unit 2 Milestone, Sociology - Final Milestone, Sociology Milestone 5, Sociology - Milestone 4

View Set