Skip to content

Python

LeetCode: 31-Next Permutation 解題紀錄


題目

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).

The replacement must be in place and use only constant extra memory.

Example:

Input: nums = [1,2,3]
Output: [1,3,2]

Input: nums = [3,2,1]
Output: [1,2,3]

Input: nums = [1,1,5]
Output: [1,5,1]

Input: nums = [1]
Output: [1]

題目給定輸入一個陣列,我們要將陣列排列成『下一個較大的值』。比方說題目輸入了 [1,2,3],那麼我們就不能排列成 [3,1,2]、而是要排列成 [1,3,2] —— 因為 132 才下一個較大的值,而非 312。

而當題目給定的陣列沒有下一個較大的排列時,則將陣列『從小排到大』。

順帶一提這題不會返回任何值,直接處理輸入陣列即可。

Read More »LeetCode: 31-Next Permutation 解題紀錄

LeetCode: 13-Roman to Integer 解題紀錄


題目

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.

Example:

Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Read More »LeetCode: 13-Roman to Integer 解題紀錄

LeetCode: 11-Container With Most Water 解題紀錄

題目

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 the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.

Notice that you may not slant the container.

Example:

Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

這個題目也是相當地經典,就是題目給定一陣列,陣列內為由左到右每個 ...... ㄜ,這裡就叫『木板』好了,每個木板的高度

而不同的木板之間,距離單位都是 1。我們要做的就是,找出能讓容器積蓄最多的水兩片木板,最後,回傳可以積蓄水的最大值

Read More »LeetCode: 11-Container With Most Water 解題紀錄

LeetCode: 12-Integer to Roman 解題紀錄


題目

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

-  I can be placed before V (5) and X (10) to make 4 and 9. 
-  X can be placed before L (50) and C (100) to make 40 and 90. 
-  C can be placed before D (500) and M (1000) to make 400 and    900.

Given an integer, convert it to a roman numeral.

Example:

Input: num = 3
Output: "III"

Input: num = 58
Output: "LVIII"

Input: num = 1994
Output: "MCMXCIV"

題目給定一整數值,而我們要做的便是將整數值轉換成羅馬數字 —— 也就是說,我們需要回傳字串型態的答案。

Read More »LeetCode: 12-Integer to Roman 解題紀錄

LeetCode: 14-Longest Common Prefix 解題紀錄


題目

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

If there is no common prefix, return an empty string "".

Example:

Input: strs = ["flower","flow","flight"]
Output: "fl"

Input: strs = ["dog","racecar","car"]
Output: ""

題目給定一陣列,裡頭存放許多組字串,而我們要做的就是找到他們共同的前綴字串並回傳;若無共同的前綴,則回傳空字串 ""

Read More »LeetCode: 14-Longest Common Prefix 解題紀錄

LeetCode: 9-Palindrome Number 解題紀錄


題目

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Follow up: Could you solve it without converting the integer to a string?

Example:

Input: x = 121
Output: true

Input: x = -121
Output: false

本題給定一範圍介於 -2^31-1 和 2^31 之間的整數值,然後我們必須判斷該數值是否顛倒過來也是相同的,有點像是數值本身的『回文』。

當然,這題將其數值轉成字串來解題是非常直覺的。不過在題目的 Follow up 中提到,是否能不轉為字串來解開這個題目呢?

Read More »LeetCode: 9-Palindrome Number 解題紀錄

LeetCode: 8-String to Integer (atoi) 解題紀錄


題目

Implement atoi which converts a string to an integer.

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.

Note:
Only the space character ' ' is considered a whitespace character.
Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−2^31,  2^31 − 1]. If the numerical value is out of the range of representable values, 2^31 − 1 or −2^31 is returned.

Example:

Input: str = "42"
Output: 42

Input: str = "   -42"
Output: -42

Input: str = "4193 with words"
Output: 4193

Input: str = "words and 987"
Output: 0

Input: str = "-91283472332"
Output: -2147483648


本題要實作 atoi() 這一函式,也即是題目給定一組字串輸入,我們要將其可能轉換成整數字串部分提取出來並回傳該整數。

  • 若是字串先碰到文字,則回傳 0
  • 若是該整數 > 2^31 -1 或整數 < -2^31,則回傳整數型態限制的最大值或最小值
Read More »LeetCode: 8-String to Integer (atoi) 解題紀錄
Exit mobile version