File tree 4 files changed +59
-6
lines changed
solution/1700-1799/1720.Decode XORed Array
4 files changed +59
-6
lines changed Original file line number Diff line number Diff line change 42
42
<li><code>0 <= first <= 10<sup>5</sup></code></li>
43
43
</ul >
44
44
45
-
46
45
## 解法
47
46
48
47
<!-- 这里可写通用的实现逻辑 -->
49
48
49
+ 异或运算。
50
+
51
+ ` a = b ^ c ` => ` a ^ b = b ^ c ^ b ` => ` c = a ^ b ` 。
52
+
50
53
<!-- tabs:start -->
51
54
52
55
### ** Python3**
53
56
54
57
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
58
56
59
``` python
57
-
60
+ class Solution :
61
+ def decode (self , encoded : List[int ], first : int ) -> List[int ]:
62
+ res = [first]
63
+ for e in encoded:
64
+ first ^= e
65
+ res.append(first)
66
+ return res
58
67
```
59
68
60
69
### ** Java**
61
70
62
71
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
72
64
73
``` java
65
-
74
+ class Solution {
75
+ public int [] decode (int [] encoded , int first ) {
76
+ int [] res = new int [encoded. length + 1 ];
77
+ res[0 ] = first;
78
+ for (int i = 0 ; i < encoded. length; ++ i) {
79
+ res[i + 1 ] = res[i] ^ encoded[i];
80
+ }
81
+ return res;
82
+ }
83
+ }
66
84
```
67
85
68
86
### ** ...**
Original file line number Diff line number Diff line change 38
38
<li><code>0 <= first <= 10<sup>5</sup></code></li>
39
39
</ul >
40
40
41
-
42
41
## Solutions
43
42
43
+ XOR.
44
+
45
+ ` a = b ^ c ` => ` a ^ b = b ^ c ^ b ` => ` c = a ^ b ` .
46
+
44
47
<!-- tabs:start -->
45
48
46
49
### ** Python3**
47
50
48
51
``` python
49
-
52
+ class Solution :
53
+ def decode (self , encoded : List[int ], first : int ) -> List[int ]:
54
+ res = [first]
55
+ for e in encoded:
56
+ first ^= e
57
+ res.append(first)
58
+ return res
50
59
```
51
60
52
61
### ** Java**
53
62
54
63
``` java
55
-
64
+ class Solution {
65
+ public int [] decode (int [] encoded , int first ) {
66
+ int [] res = new int [encoded. length + 1 ];
67
+ res[0 ] = first;
68
+ for (int i = 0 ; i < encoded. length; ++ i) {
69
+ res[i + 1 ] = res[i] ^ encoded[i];
70
+ }
71
+ return res;
72
+ }
73
+ }
56
74
```
57
75
58
76
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] decode (int [] encoded , int first ) {
3
+ int [] res = new int [encoded .length + 1 ];
4
+ res [0 ] = first ;
5
+ for (int i = 0 ; i < encoded .length ; ++i ) {
6
+ res [i + 1 ] = res [i ] ^ encoded [i ];
7
+ }
8
+ return res ;
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def decode (self , encoded : List [int ], first : int ) -> List [int ]:
3
+ res = [first ]
4
+ for e in encoded :
5
+ first ^= e
6
+ res .append (first )
7
+ return res
You can’t perform that action at this time.
0 commit comments