Category: LeetCode

Base 7

1. Description Given an integer, return its base 7 string representation. 2. Example 3. Solution 4. Complexity Time: O(n) Space: O(1)

Read More

Find the sum of the two elements of a given array which is equal to an given integer

1. Description Asked by Google. Given a list of numbers and a number k, return whether any two numbers from the list add up to k. 2. Example Given [10, 15, 3, 7] and k of 17, return true, since 10+7 is 17. 3. Solution – 3 ways to solve it.

Read More

Shortest Unsorted Continuous Subarray

1. Description Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to find the shortest such subarray and output its length. Example 1: Input: [2, 6, 4, 8, 10, 9, 15] Output: 5 Explanation: […]

Read More

Best time to buy and sell stock – one time transaction

1. Description Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock […]

Read More

Word Pattern

1. Description Give a pattern and a string, and check if the string follows the pattern. 2. Example 3. Solution 3.1 Wrong solution First, I was thinking, if I can use the AscII code to represent the pattern. For example: hello -> ‘a’, and hello -> ‘a’, world -> ‘b’, world->’b’. Use the data structure […]

Read More

Contain Duplicate

LeetCode, check if an array contains duplicated value in it. Apply Java Set feature to solve it.

Read More

Remove Duplicated from Sorted Array

1. Problem Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. 2. Examples 3. Solution Two possible solutions, I think I can solve this problem. #1. Use Java built-in […]

Read More