https://leetcode.com/problems/shortest-word-distance-iii/ Shortest Word Distance III Total Accepted: 1824 Total Submissions: 4282 Difficulty: Medium This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2. Given a list of words and two words word1 and word2, return the shortest distance between these two words in the …
Author Archives: czxttkl
Leetcode 244: Shortest Word Distance II
https://leetcode.com/problems/shortest-word-distance-ii/ Shortest Word Distance II Total Accepted: 1811 Total Submissions: 5300 Difficulty: Medium This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it? Design a class which …
Leetcode 243: Shortest Word Distance
https://leetcode.com/problems/shortest-word-distance/ Shortest Word Distance Total Accepted: 2492 Total Submissions: 5946 Difficulty: Easy Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example,Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. Given word1 = “coding”, word2 = “practice”, return 3.Given word1 = …
Leetcode 241: Different Ways to Add Parenthesis
https://leetcode.com/problems/different-ways-to-add-parentheses/ Different Ways to Add Parentheses Total Accepted: 9627 Total Submissions: 34073 Difficulty: Medium Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +,- and *. Example 1 Input: "2-1-1". ((2-1)-1) = 0 (2-(1-1)) = 2 Output: …
Continue reading “Leetcode 241: Different Ways to Add Parenthesis”
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 …
Continue reading “Leetcode 188: Best Time to Buy and Sell Stock IV”