Leetcode - Conversion
6. ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/
Description
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)
1 | P A H N |
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
1 | string convert(string s, int numRows); |
Example 1:
1 | Input: s = "PAYPALISHIRING", numRows = 3 |
Example 2:
1 | Input: s = "PAYPALISHIRING", numRows = 4 |
Solution
https://leetcode.com/problems/zigzag-conversion/solution/
1 | class Solution: |
8. String to Integer (atoi)
https://leetcode.com/problems/string-to-integer-atoi/
Description
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 as whitespace character. - Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
Example 1:
1 | Input: "42" |
Example 2:
1 | Input: " -42" |
Example 3:
1 | Input: "4193 with words" |
Example 4:
1 | Input: "words and 987" |
Example 5:
1 | Input: "-91283472332" |
Solution
1 | class Solution: |
43. Multiply Strings[M]
https://leetcode.com/problems/multiply-strings/
Description
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
Example 1:
1 | Input: num1 = "2", num2 = "3" |
Example 2:
1 | Input: num1 = "123", num2 = "456" |
Note:
- The length of both
num1andnum2is < 110. - Both
num1andnum2contain only digits0-9. - Both
num1andnum2do not contain any leading zero, except the number 0 itself. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
Solution
https://leetcode.com/problems/multiply-strings/discuss/17615/Simple-Python-solution-18-lines
1 | class Solution: |
12. Integer to Roman[M]
https://leetcode.com/problems/integer-to-roman/
Description
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
1 | Symbol Value |
For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven 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:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
1 | Input: 3 |
Example 2:
1 | Input: 4 |
Example 3:
1 | Input: 9 |
Example 4:
1 | Input: 58 |
Example 5:
1 | Input: 1994 |
Solution
1 | class Solution: |
13. Roman to Integer[E]
https://leetcode.com/problems/roman-to-integer/
Description
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
1 | Symbol Value |
For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven 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:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
If I comes before V or X, subtract 1 eg: IV = 4 and IX = 9 // 1 5 - 1 10 If X comes before L or C, subtract 10 eg: XL = 40 and XC = 90 // 10 50 - 10 100 If C comes before D or M, subtract 100 eg: CD = 400 and CM = 900 // 100 500 - 100 1000
Example 1:
1 | Input: "III" |
Example 2:
1 | Input: "IV" |
Example 3:
1 | Input: "IX" |
Example 4:
1 | Input: "LVIII" |
Example 5:
1 | Input: "MCMXCIV" |
Solution
1 | class Solution: |
1 | import time |
91. Decode Ways[M]
https://leetcode.com/problems/decode-ways/
Description
A message containing letters from A-Z is being encoded to numbers using the following mapping:
1 | 'A' -> 1 |
Given a non-empty string containing only digits, determine the total number of ways to decode it.
Example 1:
1 | Input: "12" |
Example 2:
1 | Input: "226" |
Solution
1 |
405. Convert a Number to Hexadecimal[E]
https://leetcode.com/problems/convert-a-number-to-hexadecimal/
Description
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.
Note:
- All letters in hexadecimal (
a-f) must be in lowercase. - The hexadecimal string must not contain extra leading
0s. If the number is zero, it is represented by a single zero character'0'; otherwise, the first character in the hexadecimal string will not be the zero character. - The given number is guaranteed to fit within the range of a 32-bit signed integer.
- You must not use *any* method provided by the library which converts/formats the number to hex directly.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Solution
1 | class Solution: |
273. Integer to English Words[H]
https://leetcode.com/problems/integer-to-english-words/
Description
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
Example 1:
1 | Input: 123 |
Example 2:
1 | Input: 12345 |
Example 3:
1 | Input: 1234567 |
Example 4:
1 | Input: 1234567891 |
Solution
https://leetcode.com/problems/integer-to-english-words/discuss/70632/Recursive-Python
1 | class Solution: |





