Leetcode - Reverse
Python的Reverse方法
1 | from io import StringIO |
7. Reverse Integer[E]
https://leetcode.com/problems/reverse-integer/solution/
Description
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
1 | Input: 123 |
Example 2:
1 | Input: -123 |
Example 3:
1 | Input: 120 |
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: []. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Solution
https://leetcode.com/problems/reverse-integer/solution/
1 | class Solution: |
1 | class Solution: |
190. Reverse Bits
https://leetcode.com/problems/reverse-bits/
Description
Reverse bits of a given 32 bits unsigned integer.
Example 1:
1 | Input: 00000010100101000001111010011100 |
Example 2:
1 | Input: 11111111111111111111111111111101 |
Note:
- Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
- In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 2 above the input represents the signed integer
-3and the output represents the signed integer-1073741825.
Follow up:
If this function is called many times, how would you optimize it?
Solution
1 | def reverseBits(self, n): |
https://leetcode.com/problems/reverse-bits/solution/
1 | class Solution: |
557. Reverse Words in a String III[E]
https://leetcode.com/problems/reverse-words-in-a-string-iii/
Description
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
1 | Input: "Let's take LeetCode contest" |
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
Solution
https://leetcode.com/problems/reverse-words-in-a-string-iii/solution/
1 | class Solution: |
150. Evaluate Reverse Polish Notation[M]
https://leetcode.com/problems/evaluate-reverse-polish-notation/
Description
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Note:
- Division between two integers should truncate toward zero.
- The given RPN expression is always valid. That means the expression would always evaluate to a result and there won’t be any divide by zero operation.
Example 1:
1 | Input: ["2", "1", "+", "3", "*"] |
Example 2:
1 | Input: ["4", "13", "5", "/", "+"] |
Example 3:
1 | Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] |
Solution
1 | class Solution: |
344. Reverse String[E]
https://leetcode.com/problems/divide-two-integers/
Description
Write a function that reverses a string. The input string is given as an array of characters char[].
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
Example 1:
1 | Input: ["h","e","l","l","o"] |
Example 2:
1 | Input: ["H","a","n","n","a","h"] |
Solution
https://leetcode.com/problems/reverse-string/solution/
1 | class Solution: |
345. Reverse Vowels of a String[E]
https://leetcode.com/problems/reverse-vowels-of-a-string/
Description
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
1 | Input: "hello" |
Example 2:
1 | Input: "leetcode" |
Note:
The vowels does not include the letter “y”.
Solution
1 | class Solution: |





