File tree Expand file tree Collapse file tree 4 files changed +59
-6
lines changed
solution/1700-1799/1720.Decode XORed Array Expand file tree Collapse file tree 4 files changed +59
-6
lines changed Original file line number Diff line number Diff line change 4242 <li><code>0 <= first <= 10<sup>5</sup></code></li>
4343</ul >
4444
45-
4645## 解法
4746
4847<!-- 这里可写通用的实现逻辑 -->
4948
49+ 异或运算。
50+
51+ ` a = b ^ c ` => ` a ^ b = b ^ c ^ b ` => ` c = a ^ b ` 。
52+
5053<!-- tabs:start -->
5154
5255### ** Python3**
5356
5457<!-- 这里可写当前语言的特殊实现逻辑 -->
5558
5659``` 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
5867```
5968
6069### ** Java**
6170
6271<!-- 这里可写当前语言的特殊实现逻辑 -->
6372
6473``` 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+ }
6684```
6785
6886### ** ...**
Original file line number Diff line number Diff line change 3838 <li><code>0 <= first <= 10<sup>5</sup></code></li>
3939</ul >
4040
41-
4241## Solutions
4342
43+ XOR.
44+
45+ ` a = b ^ c ` => ` a ^ b = b ^ c ^ b ` => ` c = a ^ b ` .
46+
4447<!-- tabs:start -->
4548
4649### ** Python3**
4750
4851``` 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
5059```
5160
5261### ** Java**
5362
5463``` 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+ }
5674```
5775
5876### ** ...**
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