Skip to content

Commit c96254e

Browse files
Update
2 parents be320aa + 28f3b52 commit c96254e

25 files changed

+818
-49
lines changed

problems/0001.两数之和.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,5 +352,86 @@ List<int> twoSum(List<int> nums, int target) {
352352
}
353353
```
354354

355+
C:
356+
```c
357+
358+
359+
/**
360+
* Note: The returned array must be malloced, assume caller calls free().
361+
*/
362+
363+
// leetcode 支持 ut_hash 函式庫
364+
365+
typedef struct {
366+
int key;
367+
int value;
368+
UT_hash_handle hh; // make this structure hashable
369+
} map;
370+
371+
map* hashMap = NULL;
372+
373+
void hashMapAdd(int key, int value){
374+
map* s;
375+
// key already in the hash?
376+
HASH_FIND_INT(hashMap, &key, s);
377+
if(s == NULL){
378+
s = (map*)malloc(sizeof(map));
379+
s -> key = key;
380+
HASH_ADD_INT(hashMap, key, s);
381+
}
382+
s -> value = value;
383+
}
384+
385+
map* hashMapFind(int key){
386+
map* s;
387+
// *s: output pointer
388+
HASH_FIND_INT(hashMap, &key, s);
389+
return s;
390+
}
391+
392+
void hashMapCleanup(){
393+
map* cur, *tmp;
394+
HASH_ITER(hh, hashMap, cur, tmp){
395+
HASH_DEL(hashMap, cur);
396+
free(cur);
397+
}
398+
}
399+
400+
void hashPrint(){
401+
map* s;
402+
for(s = hashMap; s != NULL; s=(map*)(s -> hh.next)){
403+
printf("key %d, value %d\n", s -> key, s -> value);
404+
}
405+
}
406+
407+
408+
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
409+
int i, *ans;
410+
// hash find result
411+
map* hashMapRes;
412+
hashMap = NULL;
413+
ans = malloc(sizeof(int) * 2);
414+
415+
for(i = 0; i < numsSize; i++){
416+
// key 代表 nums[i] 的值,value 代表所在 index;
417+
hashMapAdd(nums[i], i);
418+
}
419+
420+
hashPrint();
421+
422+
for(i = 0; i < numsSize; i++){
423+
hashMapRes = hashMapFind(target - nums[i]);
424+
if(hashMapRes && hashMapRes -> value != i){
425+
ans[0] = i;
426+
ans[1] = hashMapRes -> value ;
427+
*returnSize = 2;
428+
return ans;
429+
}
430+
}
431+
432+
hashMapCleanup();
433+
return NULL;
434+
}
435+
```
355436
-----------------------
356437
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0019.删除链表的倒数第N个节点.md

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ public:
7575
fast = fast->next;
7676
slow = slow->next;
7777
}
78-
slow->next = slow->next->next;
78+
slow->next = slow->next->next;
79+
80+
// ListNode *tmp = slow->next; C++释放内存的逻辑
81+
// slow->next = tmp->next;
82+
// delete nth;
83+
7984
return dummyHead->next;
8085
}
8186
};
@@ -87,30 +92,27 @@ public:
8792
java:
8893
8994
```java
90-
class Solution {
91-
public ListNode removeNthFromEnd(ListNode head, int n) {
92-
ListNode dummy = new ListNode(-1);
93-
dummy.next = head;
94-
95-
ListNode slow = dummy;
96-
ListNode fast = dummy;
97-
while (n-- > 0) {
98-
fast = fast.next;
99-
}
100-
// 记住 待删除节点slow 的上一节点
101-
ListNode prev = null;
102-
while (fast != null) {
103-
prev = slow;
104-
slow = slow.next;
105-
fast = fast.next;
106-
}
107-
// 上一节点的next指针绕过 待删除节点slow 直接指向slow的下一节点
108-
prev.next = slow.next;
109-
// 释放 待删除节点slow 的next指针, 这句删掉也能AC
110-
slow.next = null;
95+
public ListNode removeNthFromEnd(ListNode head, int n){
96+
ListNode dummyNode = new ListNode(0);
97+
dummyNode.next = head;
98+
99+
ListNode fastIndex = dummyNode;
100+
ListNode slowIndex = dummyNode;
111101
112-
return dummy.next;
102+
//只要快慢指针相差 n 个结点即可
103+
for (int i = 0; i < n ; i++){
104+
fastIndex = fastIndex.next;
113105
}
106+
107+
while (fastIndex.next != null){
108+
fastIndex = fastIndex.next;
109+
slowIndex = slowIndex.next;
110+
}
111+
112+
//此时 slowIndex 的位置就是待删除元素的前一个位置。
113+
//具体情况可自己画一个链表长度为 3 的图来模拟代码来理解
114+
slowIndex.next = slowIndex.next.next;
115+
return dummyNode.next;
114116
}
115117
```
116118

@@ -341,5 +343,28 @@ object Solution {
341343
}
342344
}
343345
```
346+
347+
Rust:
348+
```rust
349+
impl Solution {
350+
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, mut n: i32) -> Option<Box<ListNode>> {
351+
let mut dummy_head = Box::new(ListNode::new(0));
352+
dummy_head.next = head;
353+
let mut fast = &dummy_head.clone();
354+
let mut slow = &mut dummy_head;
355+
while n > 0 {
356+
fast = fast.next.as_ref().unwrap();
357+
n -= 1;
358+
}
359+
while fast.next.is_some() {
360+
fast = fast.next.as_ref().unwrap();
361+
slow = slow.next.as_mut().unwrap();
362+
}
363+
slow.next = slow.next.as_mut().unwrap().next.take();
364+
dummy_head.next
365+
}
366+
}
367+
```
368+
344369
-----------------------
345370
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0024.两两交换链表中的节点.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,5 +380,51 @@ function swapPairs($head)
380380
}
381381
```
382382

383+
Rust:
384+
385+
```rust
386+
// 虚拟头节点
387+
impl Solution {
388+
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
389+
let mut dummy_head = Box::new(ListNode::new(0));
390+
dummy_head.next = head;
391+
let mut cur = dummy_head.as_mut();
392+
while let Some(mut node) = cur.next.take() {
393+
if let Some(mut next) = node.next.take() {
394+
node.next = next.next.take();
395+
next.next = Some(node);
396+
cur.next = Some(next);
397+
cur = cur.next.as_mut().unwrap().next.as_mut().unwrap();
398+
} else {
399+
cur.next = Some(node);
400+
cur = cur.next.as_mut().unwrap();
401+
}
402+
}
403+
dummy_head.next
404+
}
405+
}
406+
```
407+
408+
```rust
409+
// 递归
410+
impl Solution {
411+
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
412+
if head == None || head.as_ref().unwrap().next == None {
413+
return head;
414+
}
415+
416+
let mut node = head.unwrap();
417+
418+
if let Some(mut next) = node.next.take() {
419+
node.next = Solution::swap_pairs(next.next);
420+
next.next = Some(node);
421+
Some(next)
422+
} else {
423+
Some(node)
424+
}
425+
}
426+
}
427+
```
428+
383429
-----------------------
384430
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0035.搜索插入位置.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,57 @@ function searchInsert($nums, $target)
429429
return $r + 1;
430430
}
431431
```
432-
432+
### C
433+
```c
434+
//版本一 [left, right]左闭右闭区间
435+
int searchInsert(int* nums, int numsSize, int target){
436+
//左闭右开区间 [0 , numsSize-1]
437+
int left =0;
438+
int mid =0;
439+
int right = numsSize - 1;
440+
while(left <= right){//左闭右闭区间 所以可以 left == right
441+
mid = left + (right - left) / 2;
442+
if(target < nums[mid]){
443+
//target 在左区间 [left, mid - 1]中,原区间包含mid,右区间边界可以向左内缩
444+
right = mid -1;
445+
}else if( target > nums[mid]){
446+
//target 在右区间 [mid + 1, right]中,原区间包含mid,左区间边界可以向右内缩
447+
left = mid + 1;
448+
}else {
449+
// nums[mid] == target ,顺利找到target,直接返回mid
450+
return mid;
451+
}
452+
}
453+
//数组中未找到target元素
454+
//target在数组所有元素之后,[left, right]是右闭区间,需要返回 right +1
455+
return right + 1;
456+
}
457+
```
458+
```c
459+
//版本二 [left, right]左闭右开区间
460+
int searchInsert(int* nums, int numsSize, int target){
461+
//左闭右开区间 [0 , numsSize)
462+
int left =0;
463+
int mid =0;
464+
int right = numsSize;
465+
while(left < right){//左闭右闭区间 所以 left < right
466+
mid = left + (right - left) / 2;
467+
if(target < nums[mid]){
468+
//target 在左区间 [left, mid)中,原区间没有包含mid,右区间边界不能内缩
469+
right = mid ;
470+
}else if( target > nums[mid]){
471+
// target 在右区间 [mid+1, right)中,原区间包含mid,左区间边界可以向右内缩
472+
left = mid + 1;
473+
}else {
474+
// nums[mid] == target ,顺利找到target,直接返回mid
475+
return mid;
476+
}
477+
}
478+
//数组中未找到target元素
479+
//target在数组所有元素之后,[left, right)是右开区间, return right即可
480+
return right;
481+
}
482+
```
433483

434484
-----------------------
435485
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0045.跳跃游戏II.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,26 @@ class Solution:
214214
return ans
215215
```
216216

217+
```python
218+
# 贪心版本二
219+
class Solution:
220+
def jump(self, nums: List[int]) -> int:
221+
if len(nums) == 1:
222+
return 0
223+
curDistance, nextDistance = 0, 0
224+
step = 0
225+
for i in range(len(nums)-1):
226+
nextDistance = max(nextDistance, nums[i]+i)
227+
if i == curDistance:
228+
curDistance = nextDistance
229+
step += 1
230+
return step
231+
```
232+
233+
234+
217235
### Go
236+
218237
```Go
219238
func jump(nums []int) int {
220239
dp := make([]int, len(nums))
@@ -240,7 +259,71 @@ func min(a, b int) int {
240259
}
241260
```
242261

262+
```go
263+
// 贪心版本一
264+
func jump(nums []int) int {
265+
n := len(nums)
266+
if n == 1 {
267+
return 0
268+
}
269+
cur, next := 0, 0
270+
step := 0
271+
for i := 0; i < n; i++ {
272+
next = max(nums[i]+i, next)
273+
if i == cur {
274+
if cur != n-1 {
275+
step++
276+
cur = next
277+
if cur >= n-1 {
278+
return step
279+
}
280+
} else {
281+
return step
282+
}
283+
}
284+
}
285+
return step
286+
}
287+
288+
func max(a, b int) int {
289+
if a > b {
290+
return a
291+
}
292+
return b
293+
}
294+
```
295+
296+
```go
297+
// 贪心版本二
298+
func jump(nums []int) int {
299+
n := len(nums)
300+
if n == 1 {
301+
return 0
302+
}
303+
cur, next := 0, 0
304+
step := 0
305+
for i := 0; i < n-1; i++ {
306+
next = max(nums[i]+i, next)
307+
if i == cur {
308+
cur = next
309+
step++
310+
}
311+
}
312+
return step
313+
}
314+
315+
func max(a, b int) int {
316+
if a > b {
317+
return a
318+
}
319+
return b
320+
}
321+
```
322+
323+
324+
243325
### Javascript
326+
244327
```Javascript
245328
var jump = function(nums) {
246329
let curIndex = 0

0 commit comments

Comments
 (0)