File tree Expand file tree Collapse file tree 8 files changed +121
-64
lines changed
0000-0099/0007.Reverse Integer
0100-0199/0190.Reverse Bits Expand file tree Collapse file tree 8 files changed +121
-64
lines changed Original file line number Diff line number Diff line change 39
39
40
40
<!-- 这里可写当前语言的特殊实现逻辑 -->
41
41
42
- ```python
42
+ 转字符串,进行翻转。
43
43
44
+ ```python
45
+ class Solution:
46
+ def reverse(self, x: int) -> int:
47
+ y = int(str(abs(x))[::-1])
48
+ res = -y if x < 0 else y
49
+ return 0 if res < -2**31 or res > 2**31 -1 else res
44
50
```
45
51
46
52
### **Java**
47
53
48
54
<!-- 这里可写当前语言的特殊实现逻辑 -->
49
55
50
56
```java
51
-
57
+ class Solution {
58
+ public int reverse(int x) {
59
+ long res = 0;
60
+ // 考虑负数情况,所以这里条件为: x != 0
61
+ while (x != 0) {
62
+ res = res * 10 + (x % 10);
63
+ x /= 10;
64
+ }
65
+ return res < Integer.MIN_VALUE || res > Integer.MAX_VALUE ? 0 : (int) res;
66
+ }
67
+ }
52
68
```
53
69
54
70
### **...**
Original file line number Diff line number Diff line change @@ -47,13 +47,26 @@ Assume we are dealing with an environment which could only store integers within
47
47
### **Python3**
48
48
49
49
```python
50
-
50
+ class Solution:
51
+ def reverse(self, x: int) -> int:
52
+ y = int(str(abs(x))[::-1])
53
+ res = -y if x < 0 else y
54
+ return 0 if res < -2**31 or res > 2**31 -1 else res
51
55
```
52
56
53
57
### **Java**
54
58
55
59
```java
56
-
60
+ class Solution {
61
+ public int reverse(int x) {
62
+ long res = 0;
63
+ while (x != 0) {
64
+ res = res * 10 + (x % 10);
65
+ x /= 10;
66
+ }
67
+ return res < Integer.MIN_VALUE || res > Integer.MAX_VALUE ? 0 : (int) res;
68
+ }
69
+ }
57
70
```
58
71
59
72
### **...**
Original file line number Diff line number Diff line change 1
- /*
2
- class Solution {
3
- public int reverse(int x) {
4
- if (x == 0) {
5
- return x;
6
- }
7
-
8
- long tmp = x;
9
- boolean isPositive = true;
10
- if (tmp < 0) {
11
- isPositive = false;
12
- tmp = -tmp;
13
- }
14
-
15
- long val = Long.parseLong(new StringBuilder(String.valueOf(tmp)).reverse().toString());
16
-
17
- return isPositive ? (val > Integer.MAX_VALUE ? 0 : (int) val) : (-val < Integer.MIN_VALUE ? 0 : (int) (-val));
18
-
19
- }
20
- }
21
- */
22
-
23
1
class Solution {
24
2
public int reverse(int x) {
25
3
long res = 0;
@@ -28,9 +6,6 @@ public int reverse(int x) {
28
6
res = res * 10 + (x % 10);
29
7
x /= 10;
30
8
}
31
- return (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE)
32
- ? 0
33
- : (int) res;
34
-
9
+ return res < Integer.MIN_VALUE || res > Integer.MAX_VALUE ? 0 : (int) res;
35
10
}
36
11
}
Original file line number Diff line number Diff line change 1
1
class Solution:
2
- def reverse(self, x):
3
- """
4
- :type x: int
5
- :rtype: int
6
- """
7
- if x==0:
8
- return 0
9
- y=str(abs(x))
10
- y=y[::-1]
11
- y=int(y)
12
- if x<0:
13
- tmp=-y
14
- else:
15
- tmp=y
16
-
17
- if tmp>=2**31-1 or tmp<-(2**31):
18
- return 0
19
- else:
20
- return tmp
2
+ def reverse(self, x: int) -> int:
3
+ y = int(str(abs(x))[::-1])
4
+ res = -y if x < 0 else y
5
+ return 0 if res < -2**31 or res > 2**31 -1 else res
Original file line number Diff line number Diff line change @@ -75,6 +75,40 @@ public class Solution {
75
75
}
76
76
```
77
77
78
+ ### **C++**
79
+
80
+ ```cpp
81
+ class Solution {
82
+ public:
83
+ uint32_t reverseBits(uint32_t n) {
84
+ uint32_t res = 0;
85
+ for (int i = 0; i < 32; ++i) {
86
+ res |= ((n & 1) << (31 - i));
87
+ n >>= 1;
88
+ }
89
+ return res;
90
+ }
91
+ };
92
+ ```
93
+
94
+ ### **JavaScript**
95
+
96
+ ```js
97
+ /**
98
+ * @param {number} n - a positive integer
99
+ * @return {number} - a positive integer
100
+ */
101
+ var reverseBits = function (n) {
102
+ let res = 0;
103
+ for (let i = 0; i < 32 && n > 0; ++i) {
104
+ res |= (n & 1) << (31 - i);
105
+ n >>>= 1;
106
+ }
107
+ // 无符号右移
108
+ return res >>> 0;
109
+ };
110
+ ```
111
+
78
112
### **...**
79
113
80
114
```
Original file line number Diff line number Diff line change @@ -70,6 +70,39 @@ public class Solution {
70
70
}
71
71
```
72
72
73
+ ### **C++**
74
+
75
+ ```cpp
76
+ class Solution {
77
+ public:
78
+ uint32_t reverseBits(uint32_t n) {
79
+ uint32_t res = 0;
80
+ for (int i = 0; i < 32; ++i) {
81
+ res |= ((n & 1) << (31 - i));
82
+ n >>= 1;
83
+ }
84
+ return res;
85
+ }
86
+ };
87
+ ```
88
+
89
+ ### **JavaScript**
90
+
91
+ ```js
92
+ /**
93
+ * @param {number} n - a positive integer
94
+ * @return {number} - a positive integer
95
+ */
96
+ var reverseBits = function (n) {
97
+ let res = 0;
98
+ for (let i = 0; i < 32 && n > 0; ++i) {
99
+ res |= (n & 1) << (31 - i);
100
+ n >>>= 1;
101
+ }
102
+ return res >>> 0;
103
+ };
104
+ ```
105
+
73
106
### **...**
74
107
75
108
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
uint32_t reverseBits(uint32_t n) {
4
- uint32_t res = 0 ;
5
- uint32_t tmp = 1 << 31 ;
6
- while (n)
7
- {
8
- if (n&1)
9
- res |= tmp ;
10
- tmp >>= 1 ;
11
- n >>= 1 ;
4
+ uint32_t res = 0;
5
+ for (int i = 0; i < 32; ++i) {
6
+ res |= ((n & 1) << (31 - i));
7
+ n >>= 1;
12
8
}
13
-
14
- return res ;
9
+ return res;
15
10
}
16
11
};
Original file line number Diff line number Diff line change 1
- const reverseBits = function (n) {
2
- return parseInt(
3
- n.toString(2).split("").reverse().join("").padEnd(32, "0"),
4
- 2
5
- );
1
+ /**
2
+ * @param {number} n - a positive integer
3
+ * @return {number} - a positive integer
4
+ */
5
+ var reverseBits = function (n) {
6
+ let res = 0;
7
+ for (let i = 0; i < 32 && n > 0; ++i) {
8
+ res |= (n & 1) << (31 - i);
9
+ n >>>= 1;
10
+ }
11
+ return res >>> 0;
6
12
};
You can’t perform that action at this time.
0 commit comments