[LeetCode] Happy Number 快樂數字 (No.202)

Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example: 19 is a happy number
  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1
Ref: https://leetcode.com/problems/happy-number/


[LeetCode] Longest Substring Without Repeating Characters 求出不重覆字母下最長的子字串長度 (No.3)

Given a string, find the length of the longest substring without repeating characters.
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.
Ref: https://leetcode.com/problems/longest-substring-without-repeating-characters/


[Java] 使用JavaMail從GMail寄信以及使用EWS API從Exchange寄信

今天在研究如何使用Java Mail的API從GMail寄信出去,以及使用EWS API從Exchange寄信,寫了一些測試用的Code分享給大家。

因為GMail講求安全性,所以在Java Mail的設定上會比較複雜, SMTP 伺服器的通訊埠設為 465 (適用 SSL/TLS) 和 587 (適用 STARTTLS),而在這個例子,我們使用STARTTLS,這個協定就是為了加密從用戶端到伺服器端的連線,會對EMAIL在寄送時更加安全。另外,GMail也有開啟SMTP AUTH的功能,所以我們要得到使用者的帳號密碼才可以寄送。





[LeetCode] Add Two Numbers 兩LinkedList相加 (No.2)

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
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
Ref: https://leetcode.com/problems/add-two-numbers/


[LeetCode] Two Sum 兩兩相加 (No.1)

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.
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.
Ref: https://leetcode.com/problems/two-sum/


[LeetCode] Excel Sheet Column Number 表單數字翻譯 (No.171)

Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
    A -> 1
    B -> 2
    C -> 3
    Z -> 26
    AA -> 27
    AB -> 28 

Ref: https://leetcode.com/problems/excel-sheet-column-number/


[LeetCode] Execel Sheet Column Title 表單標題翻譯 (No.168)

Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
    1 -> A
    2 -> B
    3 -> C
    26 -> Z
    27 -> AA
    28 -> AB 
Ref: https://leetcode.com/problems/excel-sheet-column-title/


[LeetCode] Dungeon Game 地牢遊戲 (No.174)

The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.

[LeetCode] 前言