Leetcode 22: Generate Parentheses

https://leetcode.com/problems/generate-parentheses/ Generate Parentheses Total Accepted: 62423 Total Submissions: 186597 Difficulty: Medium Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" Code class Solution(object): def generateParenthesis(self, n): “”” :type n: int :rtype: List[str] “”” res …

Leetcode 279: Perfect Squares

https://leetcode.com/problems/perfect-squares/ Perfect Squares Total Accepted: 10875 Total Submissions: 37824 Difficulty: Medium Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, …

Leetcode 69: Sqrt(x)

https://leetcode.com/problems/sqrtx/ Sqrt(x) Total Accepted: 68881 Total Submissions: 291203 Difficulty: Medium Implement int sqrt(int x). Compute and return the square root of x.   Code class Solution(object): def mySqrt(self, x): “”” :type x: int :rtype: int “”” if x < 0: return -1 left, right = 0, x while left <= right: mid = left + …

Leetcode 231: Power of Two

https://leetcode.com/problems/power-of-two/ Power of Two Total Accepted: 32889 Total Submissions: 103319 Difficulty: Easy Given an integer, write a function to determine if it is a power of two. Code class Solution(object): def isPowerOfTwo(self, n): “”” :type n: int :rtype: bool “”” return (n & (n-1)) == 0 and n > 0   Idea: Using the idea …

Leetcode 273: Integer to English Words

https://leetcode.com/problems/integer-to-english-words/ Integer to English Words Total Accepted: 5707 Total Submissions: 36483 Difficulty: Medium Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 – 1. For example, 123 -> “One Hundred Twenty Three” 12345 -> “Twelve Thousand Three Hundred Forty Five” 1234567 -> “One Million Two Hundred …

Leetcode 188: Best Time to Buy and Sell Stock IV

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ Best Time to Buy and Sell Stock IV Total Accepted: 15596 Total Submissions: 80223 Difficulty: Hard Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note:You may not …

Leetcode 63: Unique Paths II

https://leetcode.com/problems/unique-paths-ii/ Unique Paths II Total Accepted: 47668 Total Submissions: 169510 Difficulty: Medium Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle …

Leetcode 62: Unique Paths

https://leetcode.com/problems/unique-paths/ Unique Paths Total Accepted: 62277 Total Submissions: 185422 Difficulty: Medium A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of …

Leetcode 153: Find Minimum in Rotated Sorted Array

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Find Minimum in Rotated Sorted Array Total Accepted: 63536 Total Submissions: 186885 Difficulty: Medium Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists …

Leetcode 281: Zigzag Iterator

https://leetcode.com/problems/zigzag-iterator/ Zigzag Iterator Total Accepted: 1714 Total Submissions: 4685 Difficulty: Medium Given two 1d vectors, implement an iterator to return their elements alternately. For example, given two 1d vectors: v1 = [1, 2] v2 = [3, 4, 5, 6] By calling next repeatedly until hasNext returns false, the order of elements returned by next should …