Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1720
Browse files Browse the repository at this point in the history
No.1720.Decode XORed Array
  • Loading branch information
yanglbme committed Jun 22, 2024
1 parent 4d9760a commit 6625114
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 21 deletions.
47 changes: 41 additions & 6 deletions solution/1700-1799/1720.Decode XORed Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,29 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:位运算

根据题目描述,有:

$$
\text{encoded}[i] = \text{arr}[i] \oplus \text{arr}[i + 1]
$$

如果我们将等式两边同时异或上 $\text{arr}[i]$,那么就会得到:

$$
\text{arr}[i] \oplus \text{arr}[i] \oplus \text{arr}[i + 1] = \text{arr}[i] \oplus \text{encoded}[i]
$$

即:

$$
\text{arr}[i + 1] = \text{arr}[i] \oplus \text{encoded}[i]
$$

根据上述推导,我们可以从 $\text{first}$ 开始,依次计算出数组 $\text{arr}$ 的每一个元素。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -98,9 +120,10 @@ class Solution {
class Solution {
public:
vector<int> decode(vector<int>& encoded, int first) {
vector<int> ans{{first}};
for (int i = 0; i < encoded.size(); ++i)
ans.push_back(ans[i] ^ encoded[i]);
vector<int> ans = {{first}};
for (int x : encoded) {
ans.push_back(ans.back() ^ x);
}
return ans;
}
};
Expand All @@ -111,13 +134,25 @@ public:
```go
func decode(encoded []int, first int) []int {
ans := []int{first}
for i, e := range encoded {
ans = append(ans, ans[i]^e)
for i, x := range encoded {
ans = append(ans, ans[i]^x)
}
return ans
}
```

#### TypeScript

```ts
function decode(encoded: number[], first: number): number[] {
const ans: number[] = [first];
for (const x of encoded) {
ans.push(ans.at(-1)! ^ x);
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
51 changes: 43 additions & 8 deletions solution/1700-1799/1720.Decode XORed Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,29 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Bit Manipulation

Based on the problem description, we have:

$$
\text{encoded}[i] = \text{arr}[i] \oplus \text{arr}[i + 1]
$$

If we XOR both sides of the equation with $\text{arr}[i]$, we get:

$$
\text{arr}[i] \oplus \text{arr}[i] \oplus \text{arr}[i + 1] = \text{arr}[i] \oplus \text{encoded}[i]
$$

Which simplifies to:

$$
\text{arr}[i + 1] = \text{arr}[i] \oplus \text{encoded}[i]
$$

Following the derivation above, we can start with $\text{first}$ and sequentially calculate every element of the array $\text{arr}$.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array.

<!-- tabs:start -->

Expand All @@ -69,8 +91,8 @@ tags:
class Solution:
def decode(self, encoded: List[int], first: int) -> List[int]:
ans = [first]
for e in encoded:
ans.append(ans[-1] ^ e)
for x in encoded:
ans.append(ans[-1] ^ x)
return ans
```

Expand All @@ -96,9 +118,10 @@ class Solution {
class Solution {
public:
vector<int> decode(vector<int>& encoded, int first) {
vector<int> ans{{first}};
for (int i = 0; i < encoded.size(); ++i)
ans.push_back(ans[i] ^ encoded[i]);
vector<int> ans = {{first}};
for (int x : encoded) {
ans.push_back(ans.back() ^ x);
}
return ans;
}
};
Expand All @@ -109,13 +132,25 @@ public:
```go
func decode(encoded []int, first int) []int {
ans := []int{first}
for i, e := range encoded {
ans = append(ans, ans[i]^e)
for i, x := range encoded {
ans = append(ans, ans[i]^x)
}
return ans
}
```

#### TypeScript

```ts
function decode(encoded: number[], first: number): number[] {
const ans: number[] = [first];
for (const x of encoded) {
ans.push(ans.at(-1)! ^ x);
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
7 changes: 4 additions & 3 deletions solution/1700-1799/1720.Decode XORed Array/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Solution {
public:
vector<int> decode(vector<int>& encoded, int first) {
vector<int> ans{{first}};
for (int i = 0; i < encoded.size(); ++i)
ans.push_back(ans[i] ^ encoded[i]);
vector<int> ans = {{first}};
for (int x : encoded) {
ans.push_back(ans.back() ^ x);
}
return ans;
}
};
4 changes: 2 additions & 2 deletions solution/1700-1799/1720.Decode XORed Array/Solution.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
func decode(encoded []int, first int) []int {
ans := []int{first}
for i, e := range encoded {
ans = append(ans, ans[i]^e)
for i, x := range encoded {
ans = append(ans, ans[i]^x)
}
return ans
}
4 changes: 2 additions & 2 deletions solution/1700-1799/1720.Decode XORed Array/Solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution:
def decode(self, encoded: List[int], first: int) -> List[int]:
ans = [first]
for e in encoded:
ans.append(ans[-1] ^ e)
for x in encoded:
ans.append(ans[-1] ^ x)
return ans
7 changes: 7 additions & 0 deletions solution/1700-1799/1720.Decode XORed Array/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function decode(encoded: number[], first: number): number[] {
const ans: number[] = [first];
for (const x of encoded) {
ans.push(ans.at(-1)! ^ x);
}
return ans;
}

0 comments on commit 6625114

Please sign in to comment.