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 3939
4040<!-- 这里可写当前语言的特殊实现逻辑 -->
4141
42- ``` python
42+ 转字符串,进行翻转。
4343
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
4450```
4551
4652### ** Java**
4753
4854<!-- 这里可写当前语言的特殊实现逻辑 -->
4955
5056``` 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+ }
5268```
5369
5470### ** ...**
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
4747### ** Python3**
4848
4949``` 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
5155```
5256
5357### ** Java**
5458
5559``` 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+ }
5770```
5871
5972### ** ...**
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-
231class Solution {
242 public int reverse (int x ) {
253 long res = 0 ;
@@ -28,9 +6,6 @@ public int reverse(int x) {
286 res = res * 10 + (x % 10 );
297 x /= 10 ;
308 }
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 ;
3510 }
3611}
Original file line number Diff line number Diff line change 11class 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 {
7575}
7676```
7777
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+
78112### ** ...**
79113
80114```
Original file line number Diff line number Diff line change @@ -70,6 +70,39 @@ public class Solution {
7070}
7171```
7272
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+
73106### ** ...**
74107
75108```
Original file line number Diff line number Diff line change 11class Solution {
22public:
33 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 ;
128 }
13-
14- return res ;
9+ return res;
1510 }
1611};
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 ;
612} ;
You can’t perform that action at this time.
0 commit comments