瀏覽標籤:

LeetCode

[C#][LeetCode] 479. Largest Palindrome Product

題目

Find the largest palindrome made from the product of two n-digit numbers.
Since the result could be very large, you should return the largest palindrome mod 1337.

Example:

Input: 2
Output: 987
Explanation: 99 x 91 = 9009, 9009 % 1337 = 987

Note:
The range of n is [1,8].

解題過程

這題要找出 N 位數相乘後且結果是迴文的數字
例如:N = 2 最大數字等於 99,那就要找出最大 99 最小 10 的兩位數相乘後且結果是迴文的數字。

最初我用一個很笨的方法迴圈慢慢跑,最後當然是直接逾時,後來改成先取最大數字相乘後的前半段數字,再用迴圈去產生迴文並篩選資料,這樣比前面的笨方法快非常多。
閱讀更多

       

[C#][LeetCode] 494. Target Sum

題目

You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and – as its new symbol.

Find out how many ways to assign symbols to make sum of integers equal to target S.

題目會傳入兩個參數 nums (數字陣列)S (整數),限制只能使用兩個運算符號 +- 改變數字陣列 nums 的數字讓總和等於 S

解題過程

我自己比較少接觸演算法的東西(大學沒學好),就算去翻解答也看不懂別人在寫什麼東西,後來看了 演算法筆記- Backtracking 的圖解後終於理解解答再寫什麼東西

閱讀更多

       

[C#][LeetCode] 515. Find Largest Value in Each Tree Row

題目

You need to find the largest value in each row of a binary tree.

Example:

解題過程

這題要找出二元樹中同階層最大的數字,我的作法是先將每層的數字收集起來,最後再一起比較。

答案

 

       

[C#][LeetCode] 28. Implement strStr()

從傳入參數 haystack 中找出參數 needle 中的索引位置,非常基礎的一題

我的答案

 

       

[C#][LeetCode] 2. Add Two Numbers

輸入兩個 ListNode 物件並將兩個相同層數的數值加總,其值若超過 10 需進位到下個節點,這題我覺得迴圈解法比遞迴更讓人腦袋卡住,可能是我功力不夠吧。

我的遞迴解答:

 

 

我的迴圈作法:

 

 

       

[C#][LeetCode] 804. Unique Morse Code Words

這題要將英文轉換成摩斯密碼,並計算出重複字串的數量,我用 Linq 輕鬆解決。

 

       

[C#][LeetCode] 8. String to Integer (atoi)

外部輸入一個字串,若開頭為數字的話或 +- 符號的話則轉換為 int 的型態,且若超過 int 的最大或最小值則取極限值為準。

我的第一版答案如下:

 

 

參考最佳解答後的優化版:

 

       

[C#][LeetCode][Easy] 657. Judge Route Circle

心得:

控制機器人走路,且最後必須回到起點,把它當作XY軸來理解的話很快就可以解答。
右 X + 1, 左 X – 1, 上 Y + 1, 下 Y – 1,最後 X 與 Y 皆為0則回傳true

問題:

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

Example 1:

Example 2:

答案:

答案 – Linq:

 

       

[MySQL][LeetCode][Easy] 595. Big Countries

心得:

這題只要找出領土大於三百萬或是人口大於兩百五十萬的的資料即可

問題:

There is a table World

A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.

Write a SQL solution to output big countries’ name, population and area.

For example, according to the above table, we should output:

答案:

 

       

[C#][LeetCode][Easy] 535. Encode and Decode TinyURL

題目:

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.
Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.

心得:

這題實務上的話我應該會用資料庫來存key,簡單易用不是嗎?
不過要小心的是不能使用流水號來當作tinyurl的參數,
這樣會有被猜出key的可能性,可能就隨機產生一個短的字串並檢查不能重複來當key

我的答案(轉base64):

 

我的答案(guid):