Skip to content

C++

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) 解題紀錄

LeetCode: 5-Longest Palindromic Substring 解題紀錄


題目

Given a string s, return the longest palindromic substring in s.

Example:

Input: s = "babad"
Output: "bab"
Note: "aba" is also a valid answer.

題目給定一個輸入字串,而我們要在這個字串中,找到最大的『回文子字串』。回文(palindromic)是什麼意思呢?就是即使字串顛倒過來也是相同的,就好比範例中的 bab 一樣,顛倒過來同樣是 bab

不過這題要求回傳的答案只要是任一最長的回文子字串即可,不需要回傳『全部』的答案,也算是某種程度的佛心來著?(雖說只要將最長的都記錄起來就行了

Read More »LeetCode: 5-Longest Palindromic Substring 解題紀錄

LeetCode: 6-ZigZag Conversion 解題紀錄


題目

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 s, int numRows);

Example:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P     I    N
A   L S  I G
Y A   H R
P     I

這個題目給定輸入一個字串以及行高 numRows,我們將其重新排序成 Z 字型,並再次按照一行行的順序重新回傳字串。

也就是將這一字串重組。

Read More »LeetCode: 6-ZigZag Conversion 解題紀錄