Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 47ecc81

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents 0e068f9 + 2de818a commit 47ecc81

32 files changed

+1444
-42
lines changed

‎problems/0018.四数之和.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,96 @@ if (nums[k] + nums[i] > target && nums[i] >= 0) {
151151

152152
## 其他语言版本
153153

154+
### C:
155+
156+
```C
157+
/* qsort */
158+
static int cmp(const void* arg1, const void* arg2) {
159+
int a = *(int *)arg1;
160+
int b = *(int *)arg2;
161+
return (a > b);
162+
}
163+
164+
int** fourSum(int* nums, int numsSize, int target, int* returnSize, int** returnColumnSizes) {
165+
166+
/* 对nums数组进行排序 */
167+
qsort(nums, numsSize, sizeof(int), cmp);
168+
169+
int **res = (int **)malloc(sizeof(int *) * 40000);
170+
int index = 0;
171+
172+
/* k */
173+
for (int k = 0; k < numsSize - 3; k++) { /* 第一级 */
174+
175+
/* k剪枝 */
176+
if ((nums[k] > target) && (nums[k] >= 0)) {
177+
break;
178+
}
179+
/* k去重 */
180+
if ((k > 0) && (nums[k] == nums[k - 1])) {
181+
continue;
182+
}
183+
184+
/* i */
185+
for (int i = k + 1; i < numsSize - 2; i++) { /* 第二级 */
186+
187+
/* i剪枝 */
188+
if ((nums[k] + nums[i] > target) && (nums[i] >= 0)) {
189+
break;
190+
}
191+
/* i去重 */
192+
if ((i > (k + 1)) && (nums[i] == nums[i - 1])) {
193+
continue;
194+
}
195+
196+
/* left and right */
197+
int left = i + 1;
198+
int right = numsSize - 1;
199+
200+
while (left < right) {
201+
202+
/* 防止大数溢出 */
203+
long long val = (long long)nums[k] + nums[i] + nums[left] + nums[right];
204+
if (val > target) {
205+
right--;
206+
} else if (val < target) {
207+
left++;
208+
} else {
209+
int *res_tmp = (int *)malloc(sizeof(int) * 4);
210+
res_tmp[0] = nums[k];
211+
res_tmp[1] = nums[i];
212+
res_tmp[2] = nums[left];
213+
res_tmp[3] = nums[right];
214+
res[index++] = res_tmp;
215+
216+
/* right去重 */
217+
while ((right > left) && (nums[right] == nums[right - 1])) {
218+
right--;
219+
}
220+
/* left去重 */
221+
while ((left < right) && (nums[left] == nums[left + 1])) {
222+
left++;
223+
}
224+
225+
/* 更新right与left */
226+
left++, right--;
227+
}
228+
}
229+
}
230+
}
231+
232+
/* 返回值处理 */
233+
*returnSize = index;
234+
235+
int *column = (int *)malloc(sizeof(int) * index);
236+
for (int i = 0; i < index; i++) {
237+
column[i] = 4;
238+
}
239+
*returnColumnSizes = column;
240+
return res;
241+
}
242+
```
243+
154244
### Java:
155245
156246
```Java

‎problems/0063.不同路径II.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,27 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
550550
};
551551
```
552552
553+
// 版本二: dp改為使用一維陣列,從終點開始遍歷
554+
```typescript
555+
function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
556+
const m = obstacleGrid.length;
557+
const n = obstacleGrid[0].length;
558+
559+
const dp: number[] = new Array(n).fill(0);
560+
dp[n - 1] = 1;
561+
562+
// 由下而上,右而左進行遍歷
563+
for (let i = m - 1; i >= 0; i--) {
564+
for (let j = n - 1; j >= 0; j--) {
565+
if (obstacleGrid[i][j] === 1) dp[j] = 0;
566+
else dp[j] = dp[j] + (dp[j + 1] || 0);
567+
}
568+
}
569+
570+
return dp[0];
571+
};
572+
```
573+
553574
### Rust
554575
555576
```Rust

‎problems/1020.飞地的数量.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,63 @@ func bfs(grid [][]int, i, j int) {
605605
}
606606
```
607607

608+
### JavaScript
609+
610+
```js
611+
/**
612+
* @param {number[][]} grid
613+
* @return {number}
614+
*/
615+
var numEnclaves = function (grid) {
616+
let row = grid.length;
617+
let col = grid[0].length;
618+
let count = 0;
619+
620+
// Check the first and last row, if there is a 1, then change all the connected 1s to 0 and don't count them.
621+
for (let j = 0; j < col; j++) {
622+
if (grid[0][j] === 1) {
623+
dfs(0, j, false);
624+
}
625+
if (grid[row - 1][j] === 1) {
626+
dfs(row - 1, j, false);
627+
}
628+
}
629+
630+
// Check the first and last column, if there is a 1, then change all the connected 1s to 0 and don't count them.
631+
for (let i = 0; i < row; i++) {
632+
if (grid[i][0] === 1) {
633+
dfs(i, 0, false);
634+
}
635+
if (grid[i][col - 1] === 1) {
636+
dfs(i, col - 1, false);
637+
}
638+
}
639+
640+
// Check the rest of the grid, if there is a 1, then change all the connected 1s to 0 and count them.
641+
for (let i = 1; i < row - 1; i++) {
642+
for (let j = 1; j < col - 1; j++) {
643+
dfs(i, j, true);
644+
}
645+
}
646+
647+
function dfs(i, j, isCounting) {
648+
let condition = i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] === 0;
649+
650+
if (condition) return;
651+
if (isCounting) count++;
652+
653+
grid[i][j] = 0;
654+
655+
dfs(i - 1, j, isCounting);
656+
dfs(i + 1, j, isCounting);
657+
dfs(i, j - 1, isCounting);
658+
dfs(i, j + 1, isCounting);
659+
}
660+
661+
return count;
662+
};
663+
```
664+
608665
### Rust
609666

610667
dfs:
@@ -700,3 +757,4 @@ impl Solution {
700757
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
701758
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
702759
</a>
760+

‎problems/1047.删除字符串中的所有相邻重复项.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,33 @@ class Solution:
241241

242242
### Go:
243243

244+
使用栈
245+
```go
246+
func removeDuplicates(s string) string {
247+
stack := make([]rune, 0)
248+
for _, val := range s {
249+
if len(stack) == 0 || val != stack[len(stack)-1] {
250+
stack = append(stack, val)
251+
} else {
252+
stack = stack[:len(stack)-1]
253+
}
254+
}
255+
var res []rune
256+
for len(stack) != 0 { // 将栈中元素放到result字符串汇总
257+
res = append(res, stack[len(stack)-1])
258+
stack = stack[:len(stack)-1]
259+
}
260+
// 此时字符串需要反转一下
261+
l, r := 0, len(res)-1
262+
for l < r {
263+
res[l], res[r] = res[r], res[l]
264+
l++
265+
r--
266+
}
267+
return string(res)
268+
}
269+
```
270+
拿字符串直接作为栈,省去了栈还要转为字符串的操作
244271
```go
245272
func removeDuplicates(s string) string {
246273
var stack []byte

‎problems/kamacoder/0047.参会dijkstra堆.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,104 @@ int main() {
655655

656656
### Java
657657

658+
```Java
659+
660+
import java.util.*;
661+
662+
class Edge {
663+
int to; // 邻接顶点
664+
int val; // 边的权重
665+
666+
Edge(int to, int val) {
667+
this.to = to;
668+
this.val = val;
669+
}
670+
}
671+
672+
class MyComparison implements Comparator<Pair<Integer, Integer>> {
673+
@Override
674+
public int compare(Pair<Integer, Integer> lhs, Pair<Integer, Integer> rhs) {
675+
return Integer.compare(lhs.second, rhs.second);
676+
}
677+
}
678+
679+
class Pair<U, V> {
680+
public final U first;
681+
public final V second;
682+
683+
public Pair(U first, V second) {
684+
this.first = first;
685+
this.second = second;
686+
}
687+
}
688+
689+
public class Main {
690+
public static void main(String[] args) {
691+
Scanner scanner = new Scanner(System.in);
692+
int n = scanner.nextInt();
693+
int m = scanner.nextInt();
694+
695+
List<List<Edge>> grid = new ArrayList<>(n + 1);
696+
for (int i = 0; i <= n; i++) {
697+
grid.add(new ArrayList<>());
698+
}
699+
700+
for (int i = 0; i < m; i++) {
701+
int p1 = scanner.nextInt();
702+
int p2 = scanner.nextInt();
703+
int val = scanner.nextInt();
704+
grid.get(p1).add(new Edge(p2, val));
705+
}
706+
707+
int start = 1; // 起点
708+
int end = n; // 终点
709+
710+
// 存储从源点到每个节点的最短距离
711+
int[] minDist = new int[n + 1];
712+
Arrays.fill(minDist, Integer.MAX_VALUE);
713+
714+
// 记录顶点是否被访问过
715+
boolean[] visited = new boolean[n + 1];
716+
717+
// 优先队列中存放 Pair<节点,源点到该节点的权值>
718+
PriorityQueue<Pair<Integer, Integer>> pq = new PriorityQueue<>(new MyComparison());
719+
720+
// 初始化队列,源点到源点的距离为0,所以初始为0
721+
pq.add(new Pair<>(start, 0));
722+
723+
minDist[start] = 0; // 起始点到自身的距离为0
724+
725+
while (!pq.isEmpty()) {
726+
// 1. 第一步,选源点到哪个节点近且该节点未被访问过(通过优先级队列来实现)
727+
// <节点, 源点到该节点的距离>
728+
Pair<Integer, Integer> cur = pq.poll();
729+
730+
if (visited[cur.first]) continue;
731+
732+
// 2. 第二步,该最近节点被标记访问过
733+
visited[cur.first] = true;
734+
735+
// 3. 第三步,更新非访问节点到源点的距离(即更新minDist数组)
736+
for (Edge edge : grid.get(cur.first)) { // 遍历 cur指向的节点,cur指向的节点为 edge
737+
// cur指向的节点edge.to,这条边的权值为 edge.val
738+
if (!visited[edge.to] && minDist[cur.first] + edge.val < minDist[edge.to]) { // 更新minDist
739+
minDist[edge.to] = minDist[cur.first] + edge.val;
740+
pq.add(new Pair<>(edge.to, minDist[edge.to]));
741+
}
742+
}
743+
}
744+
745+
if (minDist[end] == Integer.MAX_VALUE) {
746+
System.out.println(-1); // 不能到达终点
747+
} else {
748+
System.out.println(minDist[end]); // 到达终点最短路径
749+
}
750+
}
751+
}
752+
753+
```
754+
755+
658756
### Python
659757

660758
### Go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /