leetcode题解


[leetcode] Count Primes

Count Primes Description: Count the number of prime numbers less than a non-negative number, n. Credits: Special thanks to @mithmatt for adding this problem and creating all test cases. A naive approach, check each i smaller than n that is prime or not. O(n^2), time limit exceeded. class Solution { […]


Word Pattern I && II

Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. Examples: pattern = “abba”, str = “dog cat cat dog” should return true. […]


[lintcode] Expression Tree Build

Expression Tree Build The structure of Expression Tree is a binary tree to evaluate certain expressions. All leaves of the Expression Tree have an number string value. All non-leaves of the Expression Tree have an operator string value. Now, given an expression array, build the expression tree of this expression, […]


[leetcode] Shortest Palindrome

Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For example: Given “aacecaaa”, return “aaacecaaa”. Given “abcd”, return “dcbabcd”. Credits: Special thanks to @ifanchu […]


[leetcode] Missing Ranges

Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return [“2”, “4->49”, “51->74”, “76->99”]. check stores the current number we want to check in […]