[LeetCode] Next Permutation (Java)

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory. Here are some examples. Inputs are in the left-hand column and … Read more

[LeetCode] Divide Two Integers (Java)

Divide two integers without using multiplication, division and mod operator. Analysis We can keep subtract divisor from dividend until dividend is smaller than 0, than count the subtract numbers. But it will costs a very long time if the divisor is very small comparing to dividend. Shift can be used to solve this problem. We … Read more

[LeetCode] Merge k Sorted Lists (Java)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Analysis At first I thought this problem was quite easy. We can just pick the smallest one from all heads of lists and then connect it to the final list. But the complexity of this algorithm will be O(n2k), in which … Read more

[LeetCode] Letter Combinations of a Phone Number (Java)

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below.

Note: Although the above answer is in lexicographical order, your answer could be in any order you want. Analysis It’s a easy question. But we … Read more

Create shortcut in Ubuntu

We may install some software manually. For example, we can run the Idea Intellij by running idea.sh. But it’s not so convenient.  So we can create a shortcut for the problem. Create a Idea.desktop file on the desktop. The content is as follow.

You may need to change the paths in the file and … Read more

[LeetCode] Longest Common Prefix (Java)

Write a function to find the longest common prefix string amongst an array of strings. Analysis This is a simple problem. We can just check for each position of every string in the string array. But we need to take care of some strings that is shorter than others. Since there is two loops, we … Read more

[LeetCode] Container With Most Water (Java)

Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container. Analysis It could take … Read more

[LeetCode] Reverse Integer (Java)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Spoilers: Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!If the integer’s last digit is 0, what should the output be? ie, cases such as … Read more

[LeetCode] ZigZag Conversion (Java)

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

And then read line by line: “PAHNAPLSIIGYIR” Write the code that will take a string and make this conversion given a number of rows:

convert(“PAYPALISHIRING”, … Read more