發表文章

目前顯示的是 2016的文章

[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 1 2  + 9 2  = 82 8 2  + 2 2  = 68 6 2  + 8 2  = 100 1 2  + 0 2  + 0 2  = 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. 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. Ref:  https://leetcode.com/problems/longest-substring-without-repeating-characters/

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

今天在研究如何使用Java Mail的API從GMail寄信出去,以及使用EWS API從Exchange寄信,寫了一些測試用的Code分享給大家。 一、用JavaMail從GMail寄信 因為GMail講求安全性,所以在Java Mail的設定上會比較複雜, SMTP 伺服器的通訊埠設為 465 (適用 SSL/TLS) 和 587 (適用 STARTTLS),而在這個例子,我們使用STARTTLS,這個協定就是為了加密從用戶端到伺服器端的連線,會對EMAIL在寄送時更加安全。另外,GMail也有開啟SMTP AUTH的功能,所以我們要得到使用者的帳號密碼才可以寄送。 而如果你有開兩步驟,需要另外建立一個「應用程式密碼」;沒有開兩步驟,則是需要開起「允許安全性較低的應用程式」才能夠正確執行,要不然會報錯。 部份程式碼: 完整程式碼: https://github.com/jimc1682000/JavaMailLab/blob/master/src/tw/com/jimmy/lab/SendFromGMailUsingJavaMailLab.java Ref: http://pclevin.blogspot.tw/2014/11/java-mail-gmail-smtp-server.html https://support.google.com/mail/answer/78775?hl=zh-Hant

[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. Example: 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] 前言

最近發現了一個不錯的網站,叫做 LeetCode ,這個網站裡面有著大量的題目,大部份都是在面試程式設計時常常被問到的問題,很適合想要找工作的人來練練手,又或是覺得自己的技術底不夠,想要再加強的人,來練習一下。 他內建了線上執行CODE和判斷程式是否執行錯誤/逾時的功能,所以有些時候,就算你的演算法都是正確的,但只要逾時,那麼還是不算通過,所以這個時候就可以研究一下其他人是怎麼寫的,他內建有論壇功能,而你也可以使用題目下去搜尋,基本上都可以找到許多參考資料。 也許有的人會覺得這種題庫的東西很沒有意義,而且很多答案也都可以上網找到,那更沒有練習的必要,但我認為,至少對我而言,有些解法我一開始是沒辦法直覺想到,需要另外花時間才能夠思考得到,在現在分秒必爭的大環境下,如果我能夠直覺想到一些解法,我相信一定有著很大的加分,更何況有些解法是我想都沒有想過的呢? 之後我會於週一到週五每天更新一篇LeetCode解法的文章,作為一個紀錄,而大家如果有什麼問題,也都可以隨時討論,雖然我不見得能夠直接回答,但至少我也會找出一些相關資料再提供大家討論。 那麼,就開始CODING吧!

昕創第一屆APP大賽心得

圖片
這是我第一次參加團隊撰寫APP的比賽,學到了不少東西,以下是一些心得跟大家分享: 一、如果團隊沒有任何討論甚至爭執,可能代表大家並沒有認真思考。     我認為如果經過認真思考後,每個人都會有自己的想法,而各自的想法在討論時,就會產生融合或碰撞,進而得到更全面整合的目標,大家也更有共識去完成那個目標才對;而如果沒有討論和爭執,則可能代表著忍氣吞聲或是沒有全面性的思考,這並不是一件好事。 二、線上協作的心態,是非常重要的。     像是Git、Trello、Teamviewer等等的一些線上協作平台,除了技術上要如何操作之外,我更認為的是心態。     心態上,一個是應該把自己的問題當成大家的問題,而大家的問題同時也是自己的問題;又或是自己的解答分享給大家,而他人的答案同時也是拿來參考。     另一個是要立刻分享自己的狀況,就如同一天到晚FB分享、發噗浪那樣,把CODE當成美食美景分享;針對特別問題,還可以TAG別人請求協助。     這兩點是我認為這次團隊寫APP當中,我學到最重要的心得,也許這次的結果不是那麼的好,做出來的程式也還有很多地方可以加強,不過我想,這次的經驗真的幫助我不少,也希望分享出來能給大家一些幫助! 完整APP使用示範影片: 以下是一些照片分享: 入圍決賽獎座 組員照

程式設計常見的幾個原則整理

SOLID原則(SRP, OCP, LSP, ISP, DIP) SRP(Single responsibility,單一職責) 定義:一個class應該只有一個需要改變的原因。 There should never be more than one reason for a class to change. 白話:一個class只作一件事。 OCP(Open-closed,開放封閉) 定義:軟體設計,應該對擴充開放,對修改封閉。 Software entities like classes, modules and functions should be open for extension but closed for modifications. 白話:軟體要很容易擴充功能,且擴充時原有的code都不需修改。 LSP(Liskov substitution,Liskov替換) 定義:子類必須能夠替換其父類別。 Inheritance should ensure that any property proved about supertype objects also holds for subtype objects. 白話:設計父類別時,只把所有的子類都有的東西放進來。 ISP(Interface segregation,介面隔離) 定義:客戶(Client)只要依賴它們所需的介面 Clients should not be forced to depend upon interfaces that they don't use. 白話:設計介面也盡量簡單,別把不相關的東西放進來。 DIP(Dependency inversion,依賴倒轉) 定義: 高階模組不應依賴低階模組,兩者應依賴抽象概念。 High-level modules should not depend on low-level modules. Both should depend on abstractions.  抽象概念不應依賴細節,細節應依賴抽象概念。 Abstractions should not depend on details. Details should depend on abstractions. ...

[心得]30天快速上手TDD讀後心得

看完了《 30快速上手TDD 》,整理一些我認為蠻重要的心得跟大家分享。 1. ATDD(USER STORY)   <->BDD(DSL TO Programming Language)   <->TDD(include integration test, unit test and refactor) 白話來說,就是用戶使用ATDD來寫好USER STORY後,用BDD轉成程式設計師看得懂的規格後,再讓程式設計師用TDD的方式開發測試 Ref: [30天快速上手TDD][Day 26]User Story/ATDD/BDD/TDD - 總結 2. 職責分離==> 找出誰,在做什麼事! 要找出「誰,做什麼事」,有一個相當相當簡單的技巧,相信大家一學就會。針對前面透過人話所整理出來的 function ,只要找出該 function 代表的意義中的 「主詞」、「動詞」、「受詞」 即可。  什麼意思?很簡單:    主詞:代表類別;    動詞:代表方法;    受詞:通常是方法參數;    形容詞:通常是呼叫物件行為後,物件產生的狀態變化。   EX1-CalculatedByBlackCat():黑貓,計算運費 Ref: [30天快速上手TDD][Day 12]Refactoring - 職責分離 3. 介面導向==> 『用該物件的角度去看世界,除了物件自己本身以外,看出去外面的世界,都是介面。』  Ref: [30天快速上手TDD][Day 16]Refactoring - 介面導向