2016年11月30日 星期三

[LeetCode] 18. 4Sum

轉自LeetCode

Given an array S of n integers, are there elements abc, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]
<Solution>

[LeetCode] 17. Letter Combinations of a Phone Number

轉自LeetCode

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.

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
<Solution>

[LeetCode] 16. 3Sum Closest

轉自LeetCode

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
    For example, given array S = {-1 2 1 -4}, and target = 1.

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
<Solution>

[LeetCode] 15. 3Sum

轉自LeetCode

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
<Solution>

[LeetCode] 14. Longest Common Prefix

轉自LeetCode

Write a function to find the longest common prefix string amongst an array of strings.

<Solution>

[LeetCode] 13. Roman to Integer

轉自LeetCode

Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
<Solution>

[LeetCode] 12. Integer to Roman

轉自LeetCode

Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
<Solution>

2016年11月29日 星期二

[LeetCode] 11. Container With Most Water

轉自LeetCode

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.
<Solution>

[LeetCode] 10. Regular Expression Matching

轉自LeetCode

Implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
<Solution>

[LeetCode] 9. Palindrome Number

轉自LeetCode

Determine whether an integer is a palindrome. Do this without extra space.


Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

<Solution>

[LeetCode] 8. String to Integer (atoi)

轉自LeetCode

Implement atoi to convert a string to an integer.

Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
<Solution>

2016年11月28日 星期一

[LeetCode] 7. Reverse Integer

轉自LeetCode

Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
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 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

<Solution>

[LeetCode] 6. ZigZag Conversion

轉自LeetCode

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)
P   A   H   N
A P L S I I G
Y   I   R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

<Solution>

2016年11月27日 星期日

[LeetCode] 125. Valid Palindrome

轉自LeetCode

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
<Solution>

[LeetCode] 5. Longest Palindromic Substring

轉自LeetCode

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.
Example:
Input: "cbbd"

Output: "bb"
<Solution>

[LeetCode] 4. Median of Two Sorted Arrays

轉自LeetCode

There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]

The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5
<Solution>

[LeetCode] 445. Add Two Numbers II

轉自LeetCode

You are given two linked lists representing two non-negative numbers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
<Solution>

[LeetCode] 394. Decode String

轉自LeetCode

Given an encoded string, return it's decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that kis guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].
Examples:
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
<Solution>

[LeetCode] 205. Isomorphic Strings

轉自LeetCode

Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given "egg""add", return true.
Given "foo""bar", return false.
Given "paper""title", return true.
Note:
You may assume both s and t have the same length.
<Solution>

2016年11月23日 星期三

[LeetCode] 3. Longest Substring Without Repeating Characters

轉自 LeetCode

Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring"pwke" is a subsequence and not a substring.
<Solution>

2016年11月21日 星期一

[LeetCode] 2. Add Two Numbers

轉自LeetCode

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
<Solution>

2016年11月20日 星期日

[LeetCode] 415. Add Strings

轉自 LeetCode

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.
<Solution>

[LeetCode] 1. Two Sum

轉自LeetCode

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
<Solution>

2016年11月19日 星期六

[LeetCode] 441. Arranging Coins

轉自LeetCode

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
Given n, find the total number of full staircase rows that can be formed.

[LeetCode] 384. Shuffle an Array

轉自LeetCode

Shuffle a set of numbers without duplicates.
Example:
// Init an array with set 1, 2, and 3.
int[] nums = {1,2,3};
Solution solution = new Solution(nums);

// Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.
solution.shuffle();

// Resets the array back to its original configuration [1,2,3].
solution.reset();

// Returns the random shuffling of array [1,2,3].
solution.shuffle();
<Solution>

[LeetCode] 453. Minimum Moves to Equal Array Elements

轉自LeetCode

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.

[LeetCode] 412. Fizz Buzz

轉自LeetCode

Write a program that outputs the string representation of numbers from 1 to n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

2016年11月18日 星期五

[LeetCode] 451. Sort Characters By Frequency

轉自LeetCode

Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"

Output:
"eert"

Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:

2016年11月15日 星期二

[LeetCode] 433. Minimum Genetic Mutation

轉自 LeetCode

A gene string can be represented by an 8-character long string, with choices from "A""C""G""T".

Suppose we need to investigate about a mutation (mutation from "start" to "end"), where ONE mutation is defined as ONE single character changed in the gene string.

For example, "AACCGGTT" -> "AACCGGTA" is 1 mutation.
Also, there is a given gene "bank", which records all the valid gene mutations. A gene must be in the bank to make it a valid gene string.

2016年11月12日 星期六

[LeetCode] 388. Longest Absolute File Path

轉自 LeetCode

Suppose we abstract our file system by a string in the following manner:
The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents:
dir
    subdir1
    subdir2
        file.ext
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.

[Electron] 學習筆記(2) - IPC 機制

source code @ github

這次來說明一下 Electron 的 IPC 機制

Electron 使用 Chromium multi-process 的機制,由 main process 來創建 application

renderer process 來負責繪製 web page,而 process 之間是無法直接溝通的

那如果想要 web page 和 GUI 有互動,例如可以透過 button 來關閉整個視窗,那該怎麼做?

這時候就必須透過 IPC (inter-process communication) 來做溝通

[Electron] 學習筆記(1) - Hello World

source code @ GitHub

在這篇教學,要用Electron寫一個簡單的桌面程式,畫面顯示Hello World

首先,到Elecetron的官方Github,這邊會說明怎麼下載,也提供一些其他語言的資源

這篇教學主要是參考官方的文件,裡面的說明已經滿清楚的

直接照著裡面一步一步做就好,之後也可以拿這個版本當作起始模板使用

相關原始碼都放在 basic 這個資料夾下


接下來就用 electron 來寫一個 Hello World

2016年11月5日 星期六

[GO] 用 homebrew 安裝 Go

記錄一下用 homebrew 安裝 go 的過程

環境是 MAC @ OSX 10.11.5


1. brew update

2. brew install go

3. mkdir ~/.go

    => 之後用 go get 的東西,都放在這

4. 新增相關設定到 ~/.bashrc

# go setting
export GOPATH=$HOME/.go
export GOROOT=/user/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin