Yearly Archives: 2015


[leetcode] Add Digits

Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up: Could you do it without any loop/recursion […]


[leetcode] Ugly Number II

Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated as an ugly number. Hint: The naive approach is […]


Differences Between Threads and Processes

Motivation The difference between threads and processes used to confused me. Why do we need threads since we already have processes to support concurrency? What’s the mechanism of the context switch between threads?(rather than processes) How is locks implemented? This article may be your answer. Acknowledgement: Some of the sentences […]


[leetcode] Unique Word Abbreviation

Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations: a) it –> it (no abbreviation) 1 b) d|o|g –> d1g 1 1 1 1—5—-0—-5–8 c) i|nternationalizatio|n –> i18n 1 1—5—-0 d) l|ocalizatio|n –> l10n Assume you have a […]


[leetcode] Palindrome Permutation II

Palindrome Permutation II Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = “aabb”, return [“abba”, “baab”]. Given s = “abc”, return []. First, count the frequency of the each character […]


[leetcode] H-Index

H-Index Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index. According to the definition of h-index on Wikipedia: “A scientist has index h if h of his/her N papers have at leasth citations each, and the other […]