mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
BIN
pics/.DS_Store
vendored
BIN
pics/.DS_Store
vendored
Binary file not shown.
@ -439,6 +439,55 @@ var solveSudoku = function(board) {
|
||||
};
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
/**
|
||||
Do not return anything, modify board in-place instead.
|
||||
*/
|
||||
function isValid(col: number, row: number, val: string, board: string[][]): boolean {
|
||||
let n: number = board.length;
|
||||
// 列向检查
|
||||
for (let rowIndex = 0; rowIndex < n; rowIndex++) {
|
||||
if (board[rowIndex][col] === val) return false;
|
||||
}
|
||||
// 横向检查
|
||||
for (let colIndex = 0; colIndex < n; colIndex++) {
|
||||
if (board[row][colIndex] === val) return false;
|
||||
}
|
||||
// 九宫格检查
|
||||
const startX = Math.floor(col / 3) * 3;
|
||||
const startY = Math.floor(row / 3) * 3;
|
||||
for (let rowIndex = startY; rowIndex < startY + 3; rowIndex++) {
|
||||
for (let colIndex = startX; colIndex < startX + 3; colIndex++) {
|
||||
if (board[rowIndex][colIndex] === val) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
function solveSudoku(board: string[][]): void {
|
||||
let n: number = 9;
|
||||
backTracking(n, board);
|
||||
function backTracking(n: number, board: string[][]): boolean {
|
||||
for (let row = 0; row < n; row++) {
|
||||
for (let col = 0; col < n; col++) {
|
||||
if (board[row][col] === '.') {
|
||||
for (let i = 1; i <= n; i++) {
|
||||
if (isValid(col, row, String(i), board)) {
|
||||
board[row][col] = String(i);
|
||||
if (backTracking(n, board) === true) return true;
|
||||
board[row][col] = '.';
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
|
||||
```C
|
||||
|
@ -457,6 +457,58 @@ var solveNQueens = function(n) {
|
||||
};
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
```typescript
|
||||
function solveNQueens(n: number): string[][] {
|
||||
const board: string[][] = new Array(n).fill(0).map(_ => new Array(n).fill('.'));
|
||||
const resArr: string[][] = [];
|
||||
backTracking(n, 0, board);
|
||||
return resArr;
|
||||
function backTracking(n: number, rowNum: number, board: string[][]): void {
|
||||
if (rowNum === n) {
|
||||
resArr.push(transformBoard(board));
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < n; i++) {
|
||||
if (isValid(i, rowNum, board) === true) {
|
||||
board[rowNum][i] = 'Q';
|
||||
backTracking(n, rowNum + 1, board);
|
||||
board[rowNum][i] = '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
function isValid(col: number, row: number, board: string[][]): boolean {
|
||||
const n: number = board.length;
|
||||
if (col < 0 || col >= n || row < 0 || row >= n) return false;
|
||||
// 检查列
|
||||
for (let row of board) {
|
||||
if (row[col] === 'Q') return false;
|
||||
}
|
||||
// 检查45度方向
|
||||
let x: number = col,
|
||||
y: number = row;
|
||||
while (y >= 0 && x < n) {
|
||||
if (board[y--][x++] === 'Q') return false;
|
||||
}
|
||||
// 检查135度方向
|
||||
x = col;
|
||||
y = row;
|
||||
while (x >= 0 && y >= 0) {
|
||||
if (board[y--][x--] === 'Q') return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
function transformBoard(board: string[][]): string[] {
|
||||
const resArr = [];
|
||||
for (let row of board) {
|
||||
resArr.push(row.join(''));
|
||||
}
|
||||
return resArr;
|
||||
}
|
||||
```
|
||||
|
||||
### Swift
|
||||
|
||||
```swift
|
||||
|
@ -40,7 +40,7 @@
|
||||
本题首先要清楚两点:
|
||||
|
||||
* 只有一只股票!
|
||||
* 当前只有买股票或者买股票的操作
|
||||
* 当前只有买股票或者卖股票的操作
|
||||
|
||||
想获得利润至少要两天为一个交易单元。
|
||||
|
||||
|
@ -209,7 +209,50 @@ var findContentChildren = function(g, s) {
|
||||
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
// 大饼干尽量喂胃口大的
|
||||
function findContentChildren(g: number[], s: number[]): number {
|
||||
g.sort((a, b) => a - b);
|
||||
s.sort((a, b) => a - b);
|
||||
const childLength: number = g.length,
|
||||
cookieLength: number = s.length;
|
||||
let curChild: number = childLength - 1,
|
||||
curCookie: number = cookieLength - 1;
|
||||
let resCount: number = 0;
|
||||
while (curChild >= 0 && curCookie >= 0) {
|
||||
if (g[curChild] <= s[curCookie]) {
|
||||
curCookie--;
|
||||
resCount++;
|
||||
}
|
||||
curChild--;
|
||||
}
|
||||
return resCount;
|
||||
};
|
||||
```
|
||||
|
||||
```typescript
|
||||
// 小饼干先喂饱小胃口的
|
||||
function findContentChildren(g: number[], s: number[]): number {
|
||||
g.sort((a, b) => a - b);
|
||||
s.sort((a, b) => a - b);
|
||||
const childLength: number = g.length,
|
||||
cookieLength: number = s.length;
|
||||
let curChild: number = 0,
|
||||
curCookie: number = 0;
|
||||
while (curChild < childLength && curCookie < cookieLength) {
|
||||
if (g[curChild] <= s[curCookie]) {
|
||||
curChild++;
|
||||
}
|
||||
curCookie++;
|
||||
}
|
||||
return curChild;
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
|
||||
```c
|
||||
int cmp(int* a, int* b) {
|
||||
return *a - *b;
|
||||
|
Reference in New Issue
Block a user