Skip to content

Commit 7fbc5c5

Browse files
authored
Merge pull request #2 from algorithm001/master
合并主分支的内容
2 parents eeb88f4 + c0531e5 commit 7fbc5c5

24 files changed

+1217
-1
lines changed

Week_01/id_122/LeetCode_142_122.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def detectCycle(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: ListNode
12+
"""
13+
if head is None or head.next is None or head.next.next is None:
14+
return
15+
p1 = head.next
16+
p2 = head.next.next
17+
while(p2 is not None and p2.next is not None and p2.next.next is not None):
18+
p1 = p1.next
19+
p2 = p2.next.next
20+
if p1 is p2:
21+
p1 = head
22+
while p1 is not p2:
23+
p1 = p1.next
24+
p2 = p2.next
25+
return p1
26+
return

Week_01/id_122/LeetCode_21_122.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: yuanyunxu
5+
* Date: 19/4/16
6+
* Time: ÏÂÎç3:41
7+
*/
8+
9+
/**
10+
* Definition for a singly-linked list.
11+
* class ListNode {
12+
* public $val = 0;
13+
* public $next = null;
14+
* function __construct($val) { $this->val = $val; }
15+
* }
16+
*/
17+
18+
class ListNode {
19+
public $val = 0;
20+
public $next = null;
21+
function __construct($val) { $this->val = $val; }
22+
}
23+
24+
class Solution {
25+
26+
/**
27+
* @param ListNode $l1
28+
* @param ListNode $l2
29+
* @return ListNode
30+
*/
31+
function mergeTwoLists($l1, $l2) {
32+
if ($l1 == null) {
33+
return $l2;
34+
}
35+
36+
if ($l2 == null) {
37+
return $l1;
38+
}
39+
40+
$first = $l1;
41+
$second = $l2;
42+
43+
if ($first->val <= $second->val) {
44+
$head = $first;
45+
$first = $first->next;
46+
} else {
47+
$head = $second;
48+
$second = $second->next;
49+
}
50+
51+
$now = $head;
52+
53+
while (true) {
54+
if ($first == null) {
55+
$now->next = $second;
56+
break;
57+
}
58+
if ($second == null) {
59+
$now->next = $first;
60+
break;
61+
}
62+
63+
if ($first->val <= $second->val) {
64+
$now->next = $first;
65+
$first = $first->next;
66+
$now = $now->next;
67+
continue;
68+
}
69+
$now->next = $second;
70+
$second = $second->next;
71+
$now = $now->next;
72+
}
73+
return $head;
74+
}
75+
}

Week_01/id_122/LeetCode_24_122.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Definition for a singly-linked list.
4+
* class ListNode {
5+
* public $val = 0;
6+
* public $next = null;
7+
* function __construct($val) { $this->val = $val; }
8+
* }
9+
*/
10+
class ListNode {
11+
public $val = 0;
12+
public $next = null;
13+
function __construct($val) { $this->val = $val; }
14+
}
15+
16+
class Solution {
17+
18+
/**
19+
* @param ListNode $head
20+
* @return ListNode
21+
*/
22+
function swapPairs($head) {
23+
24+
//没有元素,或者只有一个元素
25+
if ($head == null || $head->next == null) {
26+
return $head;
27+
}
28+
$shaoBing = new ListNode(null);
29+
$shaoBing->next = $head->next;
30+
31+
//到这里至少链表中至少有两个元素
32+
$prev = $head;
33+
$prior = $prev->next;
34+
$next = $prior->next;
35+
36+
while ($next && $next->next) {
37+
$prev->next = $next->next;
38+
$prior->next = $prev;
39+
$prev = $next;
40+
$prior = $next->next;
41+
$next = $prior->next;
42+
}
43+
44+
//最后交换末尾两个元素, 针对单数个链表元素做处理
45+
$prev->next = $next ? $next : null;
46+
$prior->next = $prev;
47+
return $shaoBing->next;
48+
}
49+
}

Week_01/id_122/LeetCode_83_122.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: yuanyunxu
5+
* Date: 19/4/16
6+
* Time: ÏÂÎç3:15
7+
*/
8+
9+
/**
10+
* Definition for a singly-linked list.
11+
* class ListNode {
12+
* public $val = 0;
13+
* public $next = null;
14+
* function __construct($val) { $this->val = $val; }
15+
* }
16+
*/
17+
18+
class ListNode {
19+
public $val = 0;
20+
public $next = null;
21+
function __construct($val) { $this->val = $val; }
22+
}
23+
24+
class Solution {
25+
26+
/**
27+
* @param ListNode $head
28+
* @return ListNode
29+
*/
30+
function deleteDuplicates($head) {
31+
if ($head == null) {
32+
return $head;
33+
}
34+
$prior = $head;
35+
while ($prior->next) {
36+
$next = $prior->next;
37+
if ($prior->val == $next->val) {
38+
$prior->next = $next->next;
39+
$next->next = null;
40+
continue;
41+
}
42+
$prior = $prior->next;
43+
}
44+
return $head;
45+
}
46+
}

Week_01/id_2/LeetCode_20_2.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace leetcode
9+
{
10+
public class 有效的括号
11+
{
12+
public bool IsValid(string s)
13+
{
14+
Stack<char> ts = new Stack<char>();
15+
Dictionary<char, char> dic = new Dictionary<char, char>();
16+
dic.Add(')', '(');
17+
dic.Add('}', '{');
18+
dic.Add(']', '[');
19+
foreach (var item in s)
20+
{
21+
if (dic.ContainsKey(item))
22+
{
23+
if (ts.Count <= 0)
24+
{
25+
return false;
26+
}
27+
28+
var left = ts.Pop();
29+
if (left != dic[item])
30+
{
31+
return false;
32+
}
33+
}
34+
else
35+
{
36+
ts.Push(item);
37+
}
38+
}
39+
40+
if (ts.Count > 0)
41+
{
42+
return false;
43+
}
44+
else
45+
{
46+
return true;
47+
}
48+
}
49+
}
50+
}

Week_01/id_2/LeetCode_21_2.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace leetcode.合并两个有序链表
8+
{
9+
public class ListNode
10+
{
11+
public int val;
12+
public ListNode next;
13+
public ListNode(int x) { val = x; }
14+
}
15+
16+
public class 合并两个有序链表
17+
{
18+
public ListNode MergeTwoLists(ListNode l1, ListNode l2)
19+
{
20+
ListNode head = new ListNode(int.MinValue);
21+
ListNode tail = head;
22+
while (l1 != null && l2 != null)
23+
{
24+
if (l1.val < l2.val)
25+
{
26+
tail.next = l1;
27+
l1 = l1.next;
28+
}
29+
else
30+
{
31+
tail.next = l2;
32+
l2 = l2.next;
33+
}
34+
tail = tail.next;
35+
}
36+
37+
if (l1 != null)
38+
{
39+
tail.next = l1;
40+
}
41+
42+
if (l2 != null)
43+
{
44+
tail.next = l2;
45+
}
46+
head = head.next;
47+
return head;
48+
}
49+
}
50+
}

Week_01/id_2/LeetCode_24_2.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace leetcode.两两交换链表中的节点
8+
{
9+
10+
public class ListNode
11+
{
12+
public int val;
13+
public ListNode next;
14+
public ListNode(int x) { val = x; }
15+
}
16+
17+
public class 两两交换链表中的节点
18+
{
19+
public ListNode SwapPairs(ListNode head)
20+
{
21+
ListNode a = new ListNode(int.MinValue);
22+
ListNode b = head;
23+
ListNode c = head?.next;
24+
a.next = head;
25+
head = a;
26+
27+
while (b != null && c != null)
28+
{
29+
var temp = c.next;
30+
c.next = b;
31+
b.next = temp;
32+
a.next = c;
33+
a = b;
34+
b = b?.next;
35+
c = b?.next;
36+
}
37+
head = head.next;
38+
39+
return head;
40+
}
41+
42+
public ListNode SwapPairs_recursive(ListNode head)
43+
{
44+
if (head == null || head.next == null)
45+
{
46+
return head;
47+
}
48+
49+
var next = head.next;
50+
head.next = SwapPairs_recursive(next.next);
51+
next.next = head;
52+
return next;
53+
}
54+
}
55+
}

Week_01/id_2/LeetCode_503_2.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace leetcode
8+
{
9+
public class 下一个更大元素_II
10+
{
11+
public int[] NextGreaterElements(int[] nums)
12+
{
13+
int[] result = new int[nums.Length];
14+
for (int i = 0; i < nums.Length; i++)
15+
{
16+
result[i] = -1;
17+
for (int j = 1; j < nums.Length; j++)
18+
{
19+
var index = (i + j) % nums.Length;
20+
if (nums[index] > nums[i])
21+
{
22+
result[i] = nums[index];
23+
break;
24+
}
25+
}
26+
}
27+
28+
return result;
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)