Skip to content

[pull] main from doocs:main #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions solution/0000-0099/0013.Roman to Integer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ func romanToInt(s string) int {
}
```

### **PHP**

```php
class Solution {
/**
* @param String $s
* @return Integer
*/
function romanToInt($s) {
$hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000);
$rs = 0;
for ($i = 0; $i < strlen($s); $i++) {
$left = $hashmap[$s[$i]];
$right = $hashmap[$s[$i + 1]];
if ($left >= $right) $rs += $left;
else $rs -= $left;
}
return $rs;
}
}
```

### **...**

```
Expand Down
22 changes: 22 additions & 0 deletions solution/0000-0099/0013.Roman to Integer/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@ func romanToInt(s string) int {
}
```

### **PHP**

```php
class Solution {
/**
* @param String $s
* @return Integer
*/
function romanToInt($s) {
$hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000);
$rs = 0;
for ($i = 0; $i < strlen($s); $i++) {
$left = $hashmap[$s[$i]];
$right = $hashmap[$s[$i + 1]];
if ($left >= $right) $rs += $left;
else $rs -= $left;
}
return $rs;
}
}
```

### **...**

```
Expand Down
17 changes: 17 additions & 0 deletions solution/0000-0099/0013.Roman to Integer/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
/**
* @param String $s
* @return Integer
*/
function romanToInt($s) {
$hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000);
$rs = 0;
for ($i = 0; $i < strlen($s); $i++) {
$left = $hashmap[$s[$i]];
$right = $hashmap[$s[$i + 1]];
if ($left >= $right) $rs += $left;
else $rs -= $left;
}
return $rs;
}
}
2 changes: 1 addition & 1 deletion solution/0100-0199/0125.Valid Palindrome/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func verify(ch byte) bool {

### **PHP**

```php
```php
class Solution {
/**
* @param String $s
Expand Down
2 changes: 1 addition & 1 deletion solution/0100-0199/0125.Valid Palindrome/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func verify(ch byte) bool {

### **PHP**

```php
```php
class Solution {
/**
* @param String $s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer <code>k</code>.</p>

<p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions.</p>
<p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k</code> times and sell at most <code>k</code> times.</p>

<p><strong>Note:</strong> You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ wordDictionary.search(&quot;b..&quot;); // return True
<li><code>1 &lt;= word.length &lt;= 25</code></li>
<li><code>word</code> in <code>addWord</code> consists of lowercase English letters.</li>
<li><code>word</code> in <code>search</code> consist of <code>&#39;.&#39;</code> or lowercase English letters.</li>
<li>There will be at most <code>3</code> dots in <code>word</code> for <code>search</code> queries.</li>
<li>There will be at most <code>2</code> dots in <code>word</code> for <code>search</code> queries.</li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>addWord</code> and <code>search</code>.</li>
</ul>

Expand Down
59 changes: 35 additions & 24 deletions solution/1700-1799/1734.Decode XORed Permutation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:位运算**

我们注意到,数组 $perm$ 是前 $n$ 个正整数的排列,因此 $perm$ 的所有元素的异或和为 $1 \oplus 2 \oplus \cdots \oplus n$,记为 $a$。而 $encode[i]=perm[i] \oplus perm[i+1]$,如果我们将 $encode[0],encode[2],\cdots,encode[n-3]$ 的所有元素的异或和记为 $b$,则 $perm[n-1]=a \oplus b$。知道了 $perm$ 的最后一个元素,我们就可以通过逆序遍历数组 $encode$ 求出 $perm$ 的所有元素。

时间复杂度 $O(n)$,其中 $n$ 为数组 $perm$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand All @@ -54,12 +60,13 @@ class Solution:
a = b = 0
for i in range(0, n - 1, 2):
a ^= encoded[i]
for i in range(n + 1):
for i in range(1, n + 1):
b ^= i
ans = [a ^ b]
for e in encoded[::-1]:
ans.append(ans[-1] ^ e)
return ans[::-1]
perm = [0] * n
perm[-1] = a ^ b
for i in range(n - 2, -1, -1):
perm[i] = encoded[i] ^ perm[i + 1]
return perm
```

### **Java**
Expand All @@ -68,23 +75,21 @@ class Solution:

```java
class Solution {

public int[] decode(int[] encoded) {
int n = encoded.length + 1;
int[] ans = new int[n];
int a = 0;
int b = 0;
int a = 0, b = 0;
for (int i = 0; i < n - 1; i += 2) {
a ^= encoded[i];
}
for (int i = 0; i < n + 1; ++i) {
for (int i = 1; i <= n; ++i) {
b ^= i;
}
ans[n - 1] = a ^ b;
int[] perm = new int[n];
perm[n - 1] = a ^ b;
for (int i = n - 2; i >= 0; --i) {
ans[i] = ans[i + 1] ^ encoded[i];
perm[i] = encoded[i] ^ perm[i + 1];
}
return ans;
return perm;
}
}
```
Expand All @@ -96,13 +101,19 @@ class Solution {
public:
vector<int> decode(vector<int>& encoded) {
int n = encoded.size() + 1;
vector<int> ans(n);
int a = 0, b = 0;
for (int i = 0; i < n - 1; i += 2) a ^= encoded[i];
for (int i = 0; i < n + 1; ++i) b ^= i;
ans[n - 1] = a ^ b;
for (int i = n - 2; i >= 0; --i) ans[i] = ans[i + 1] ^ encoded[i];
return ans;
for (int i = 0; i < n - 1; i += 2) {
a ^= encoded[i];
}
for (int i = 1; i <= n; ++i) {
b ^= i;
}
vector<int> perm(n);
perm[n - 1] = a ^ b;
for (int i = n - 2; ~i; --i) {
perm[i] = encoded[i] ^ perm[i + 1];
}
return perm;
}
};
```
Expand All @@ -112,19 +123,19 @@ public:
```go
func decode(encoded []int) []int {
n := len(encoded) + 1
ans := make([]int, n)
a, b := 0, 0
for i := 0; i < n-1; i += 2 {
a ^= encoded[i]
}
for i := 0; i < n+1; i++ {
for i := 1; i <= n; i++ {
b ^= i
}
ans[n-1] = a ^ b
perm := make([]int, n)
perm[n-1] = a ^ b
for i := n - 2; i >= 0; i-- {
ans[i] = ans[i+1] ^ encoded[i]
perm[i] = encoded[i] ^ perm[i+1]
}
return ans
return perm
}
```

Expand Down
52 changes: 29 additions & 23 deletions solution/1700-1799/1734.Decode XORed Permutation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ class Solution:
a = b = 0
for i in range(0, n - 1, 2):
a ^= encoded[i]
for i in range(n + 1):
for i in range(1, n + 1):
b ^= i
ans = [a ^ b]
for e in encoded[::-1]:
ans.append(ans[-1] ^ e)
return ans[::-1]
perm = [0] * n
perm[-1] = a ^ b
for i in range(n - 2, -1, -1):
perm[i] = encoded[i] ^ perm[i + 1]
return perm
```

### **Java**
Expand All @@ -62,20 +63,19 @@ class Solution:
class Solution {
public int[] decode(int[] encoded) {
int n = encoded.length + 1;
int[] ans = new int[n];
int a = 0;
int b = 0;
int a = 0, b = 0;
for (int i = 0; i < n - 1; i += 2) {
a ^= encoded[i];
}
for (int i = 0; i < n + 1; ++i) {
for (int i = 1; i <= n; ++i) {
b ^= i;
}
ans[n - 1] = a ^ b;
int[] perm = new int[n];
perm[n - 1] = a ^ b;
for (int i = n - 2; i >= 0; --i) {
ans[i] = ans[i + 1] ^ encoded[i];
perm[i] = encoded[i] ^ perm[i + 1];
}
return ans;
return perm;
}
}
```
Expand All @@ -87,13 +87,19 @@ class Solution {
public:
vector<int> decode(vector<int>& encoded) {
int n = encoded.size() + 1;
vector<int> ans(n);
int a = 0, b = 0;
for (int i = 0; i < n - 1; i += 2) a ^= encoded[i];
for (int i = 0; i < n + 1; ++i) b ^= i;
ans[n - 1] = a ^ b;
for (int i = n - 2; i >= 0; --i) ans[i] = ans[i + 1] ^ encoded[i];
return ans;
for (int i = 0; i < n - 1; i += 2) {
a ^= encoded[i];
}
for (int i = 1; i <= n; ++i) {
b ^= i;
}
vector<int> perm(n);
perm[n - 1] = a ^ b;
for (int i = n - 2; ~i; --i) {
perm[i] = encoded[i] ^ perm[i + 1];
}
return perm;
}
};
```
Expand All @@ -103,19 +109,19 @@ public:
```go
func decode(encoded []int) []int {
n := len(encoded) + 1
ans := make([]int, n)
a, b := 0, 0
for i := 0; i < n-1; i += 2 {
a ^= encoded[i]
}
for i := 0; i < n+1; i++ {
for i := 1; i <= n; i++ {
b ^= i
}
ans[n-1] = a ^ b
perm := make([]int, n)
perm[n-1] = a ^ b
for i := n - 2; i >= 0; i-- {
ans[i] = ans[i+1] ^ encoded[i]
perm[i] = encoded[i] ^ perm[i+1]
}
return ans
return perm
}
```

Expand Down
18 changes: 12 additions & 6 deletions solution/1700-1799/1734.Decode XORed Permutation/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ class Solution {
public:
vector<int> decode(vector<int>& encoded) {
int n = encoded.size() + 1;
vector<int> ans(n);
int a = 0, b = 0;
for (int i = 0; i < n - 1; i += 2) a ^= encoded[i];
for (int i = 0; i < n + 1; ++i) b ^= i;
ans[n - 1] = a ^ b;
for (int i = n - 2; i >= 0; --i) ans[i] = ans[i + 1] ^ encoded[i];
return ans;
for (int i = 0; i < n - 1; i += 2) {
a ^= encoded[i];
}
for (int i = 1; i <= n; ++i) {
b ^= i;
}
vector<int> perm(n);
perm[n - 1] = a ^ b;
for (int i = n - 2; ~i; --i) {
perm[i] = encoded[i] ^ perm[i + 1];
}
return perm;
}
};
10 changes: 5 additions & 5 deletions solution/1700-1799/1734.Decode XORed Permutation/Solution.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
func decode(encoded []int) []int {
n := len(encoded) + 1
ans := make([]int, n)
a, b := 0, 0
for i := 0; i < n-1; i += 2 {
a ^= encoded[i]
}
for i := 0; i < n+1; i++ {
for i := 1; i <= n; i++ {
b ^= i
}
ans[n-1] = a ^ b
perm := make([]int, n)
perm[n-1] = a ^ b
for i := n - 2; i >= 0; i-- {
ans[i] = ans[i+1] ^ encoded[i]
perm[i] = encoded[i] ^ perm[i+1]
}
return ans
return perm
}
13 changes: 6 additions & 7 deletions solution/1700-1799/1734.Decode XORed Permutation/Solution.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
class Solution {
public int[] decode(int[] encoded) {
int n = encoded.length + 1;
int[] ans = new int[n];
int a = 0;
int b = 0;
int a = 0, b = 0;
for (int i = 0; i < n - 1; i += 2) {
a ^= encoded[i];
}
for (int i = 0; i < n + 1; ++i) {
for (int i = 1; i <= n; ++i) {
b ^= i;
}
ans[n - 1] = a ^ b;
int[] perm = new int[n];
perm[n - 1] = a ^ b;
for (int i = n - 2; i >= 0; --i) {
ans[i] = ans[i + 1] ^ encoded[i];
perm[i] = encoded[i] ^ perm[i + 1];
}
return ans;
return perm;
}
}
Loading