mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -598,5 +598,30 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
C#:
|
||||
```csharp
|
||||
public class Solution {
|
||||
public int[][] GenerateMatrix(int n) {
|
||||
int[][] answer = new int[n][];
|
||||
for(int i = 0; i < n; i++)
|
||||
answer[i] = new int[n];
|
||||
int start = 0;
|
||||
int end = n - 1;
|
||||
int tmp = 1;
|
||||
while(tmp < n * n)
|
||||
{
|
||||
for(int i = start; i < end; i++) answer[start][i] = tmp++;
|
||||
for(int i = start; i < end; i++) answer[i][end] = tmp++;
|
||||
for(int i = end; i > start; i--) answer[end][i] = tmp++;
|
||||
for(int i = end; i > start; i--) answer[i][start] = tmp++;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
if(n % 2 == 1) answer[n / 2][n / 2] = tmp;
|
||||
return answer;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -163,7 +163,7 @@ public:
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
Java:
|
||||
### Java
|
||||
```Java
|
||||
class Solution {
|
||||
public int findMaxForm(String[] strs, int m, int n) {
|
||||
@ -192,7 +192,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python
|
||||
```python
|
||||
class Solution:
|
||||
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
|
||||
@ -208,7 +208,7 @@ class Solution:
|
||||
return dp[m][n]
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go
|
||||
```go
|
||||
func findMaxForm(strs []string, m int, n int) int {
|
||||
// 定义数组
|
||||
@ -294,7 +294,7 @@ func getMax(a,b int)int{
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript
|
||||
```javascript
|
||||
const findMaxForm = (strs, m, n) => {
|
||||
const dp = Array.from(Array(m+1), () => Array(n+1).fill(0));
|
||||
@ -323,7 +323,7 @@ const findMaxForm = (strs, m, n) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript
|
||||
|
||||
> 滚动数组,二维数组法
|
||||
|
||||
@ -446,7 +446,80 @@ function isValidSubSet(strs: string[], m: number, n: number): boolean {
|
||||
}
|
||||
```
|
||||
|
||||
### Scala
|
||||
|
||||
背包:
|
||||
```scala
|
||||
object Solution {
|
||||
def findMaxForm(strs: Array[String], m: Int, n: Int): Int = {
|
||||
var dp = Array.ofDim[Int](m + 1, n + 1)
|
||||
|
||||
var (oneNum, zeroNum) = (0, 0)
|
||||
|
||||
for (str <- strs) {
|
||||
oneNum = 0
|
||||
zeroNum = 0
|
||||
for (i <- str.indices) {
|
||||
if (str(i) == '0') zeroNum += 1
|
||||
else oneNum += 1
|
||||
}
|
||||
|
||||
for (i <- m to zeroNum by -1) {
|
||||
for (j <- n to oneNum by -1) {
|
||||
dp(i)(j) = math.max(dp(i)(j), dp(i - zeroNum)(j - oneNum) + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dp(m)(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
回溯法(超时):
|
||||
```scala
|
||||
object Solution {
|
||||
import scala.collection.mutable
|
||||
|
||||
var res = Int.MinValue
|
||||
|
||||
def test(str: String): (Int, Int) = {
|
||||
var (zero, one) = (0, 0)
|
||||
for (i <- str.indices) {
|
||||
if (str(i) == '1') one += 1
|
||||
else zero += 1
|
||||
}
|
||||
(zero, one)
|
||||
}
|
||||
|
||||
def travsel(strs: Array[String], path: mutable.ArrayBuffer[String], m: Int, n: Int, startIndex: Int): Unit = {
|
||||
if (startIndex > strs.length) {
|
||||
return
|
||||
}
|
||||
|
||||
res = math.max(res, path.length)
|
||||
|
||||
for (i <- startIndex until strs.length) {
|
||||
|
||||
var (zero, one) = test(strs(i))
|
||||
|
||||
// 如果0的个数小于m,1的个数小于n,则可以回溯
|
||||
if (zero <= m && one <= n) {
|
||||
path.append(strs(i))
|
||||
travsel(strs, path, m - zero, n - one, i + 1)
|
||||
path.remove(path.length - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def findMaxForm(strs: Array[String], m: Int, n: Int): Int = {
|
||||
res = Int.MinValue
|
||||
var path = mutable.ArrayBuffer[String]()
|
||||
travsel(strs, path, m, n, 0)
|
||||
res
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -251,7 +251,7 @@ dp[j] += dp[j - nums[i]];
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
Java:
|
||||
### Java
|
||||
```java
|
||||
class Solution {
|
||||
public int findTargetSumWays(int[] nums, int target) {
|
||||
@ -272,7 +272,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python
|
||||
```python
|
||||
class Solution:
|
||||
def findTargetSumWays(self, nums: List[int], target: int) -> int:
|
||||
@ -288,7 +288,7 @@ class Solution:
|
||||
return dp[bagSize]
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go
|
||||
```go
|
||||
func findTargetSumWays(nums []int, target int) int {
|
||||
sum := 0
|
||||
@ -323,7 +323,7 @@ func abs(x int) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript
|
||||
```javascript
|
||||
const findTargetSumWays = (nums, target) => {
|
||||
|
||||
@ -353,6 +353,8 @@ const findTargetSumWays = (nums, target) => {
|
||||
```
|
||||
|
||||
|
||||
### TypeScript
|
||||
|
||||
TypeScript:
|
||||
|
||||
```ts
|
||||
@ -375,7 +377,25 @@ function findTargetSumWays(nums: number[], target: number): number {
|
||||
};
|
||||
```
|
||||
|
||||
### Scala
|
||||
|
||||
```scala
|
||||
object Solution {
|
||||
def findTargetSumWays(nums: Array[Int], target: Int): Int = {
|
||||
var sum = nums.sum
|
||||
if (math.abs(target) > sum) return 0 // 此时没有方案
|
||||
if ((sum + target) % 2 == 1) return 0 // 此时没有方案
|
||||
var bagSize = (sum + target) / 2
|
||||
var dp = new Array[Int](bagSize + 1)
|
||||
dp(0) = 1
|
||||
for (i <- 0 until nums.length; j <- bagSize to nums(i) by -1) {
|
||||
dp(j) += dp(j - nums(i))
|
||||
}
|
||||
|
||||
dp(bagSize)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -244,6 +244,44 @@ var predictPartyVictory = function(senateStr) {
|
||||
};
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
```typescript
|
||||
function predictPartyVictory(senate: string): string {
|
||||
// 数量差:Count(Radiant) - Count(Dire)
|
||||
let deltaRDCnt: number = 0;
|
||||
let hasR: boolean = true,
|
||||
hasD: boolean = true;
|
||||
const senateArr: string[] = senate.split('');
|
||||
while (hasR && hasD) {
|
||||
hasR = false;
|
||||
hasD = false;
|
||||
for (let i = 0, length = senateArr.length; i < length; i++) {
|
||||
if (senateArr[i] === 'R') {
|
||||
if (deltaRDCnt < 0) {
|
||||
senateArr[i] = '';
|
||||
} else {
|
||||
hasR = true;
|
||||
}
|
||||
deltaRDCnt++;
|
||||
} else if (senateArr[i] === 'D') {
|
||||
if (deltaRDCnt > 0) {
|
||||
senateArr[i] = '';
|
||||
} else {
|
||||
hasD = true;
|
||||
}
|
||||
deltaRDCnt--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return hasR ? 'Radiant' : 'Dire';
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user