Skip to content

Commit 8cbdb6e

Browse files
Merge pull request #297 from SherryShi0108/master
# 086 week1 week2
2 parents c5b7b9a + 7e4d18b commit 8cbdb6e

13 files changed

+485
-0
lines changed

Week_01/id_86/LeetCode_083_086.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//simple C
2+
3+
void MethodFor083_1(struct ListNode* head)
4+
{
5+
if (head == NULL)
6+
{
7+
return NULL;
8+
}
9+
struct ListNode *p, *q;
10+
p = head;
11+
while (p->next)
12+
{
13+
q = p->next;
14+
if (p->val == q->val)
15+
{
16+
p->next = q->next;
17+
}
18+
else
19+
{
20+
p = q;
21+
}
22+
}
23+
return head;
24+
}

Week_01/id_86/LeetCode_083_086.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace AlgorithmTest
8+
{
9+
public class LeetCode_083_086
10+
{
11+
/// <summary>
12+
/// 92ms 23.9MB
13+
/// </summary>
14+
/// <param name="head"></param>
15+
/// <returns></returns>
16+
public ListNode MethodFor093_1(ListNode head)
17+
{
18+
if (head == null)
19+
return null;
20+
if (head.next == null)
21+
return head;
22+
else
23+
{
24+
ListNode listNode = head;
25+
while (listNode.next != null)
26+
{
27+
if (listNode.next.val == listNode.val)
28+
{
29+
listNode.next = listNode.next.next;
30+
}
31+
else
32+
{
33+
listNode = listNode.next;
34+
}
35+
}
36+
}
37+
38+
return head;
39+
}
40+
}
41+
}

Week_01/id_86/LeetCode_083_086.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
class Solution(object):
3+
def RemoveDuplicatesfromSortedList(self, head):
4+
"""
5+
:type head: ListNode
6+
:rtype: ListNode
7+
"""
8+
cur = head
9+
while cur and cur.next:
10+
if cur.val == cur.next.val:
11+
cur.next = cur.next.next
12+
else:
13+
cur = cur.next
14+
return head

Week_01/id_86/LeetCode_905_086.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//simple C
2+
3+
int* MethodFor905_1(int* A, int ASize, int* BSize)
4+
{
5+
int e = 0;
6+
int o = ASize - 1;
7+
int *B = (int *)malloc(sizeof(int) * ASize);
8+
9+
for (int i = 0; i < ASize; i++)
10+
{
11+
if (A[i] % 2 == 0)
12+
{
13+
B[e++] = A[i];
14+
}
15+
else
16+
{
17+
B[o--] = A[i];
18+
}
19+
}
20+
*BSize = ASize;
21+
return B;
22+
}

Week_01/id_86/LeetCode_905_086.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace AlgorithmTest
8+
{
9+
/// <summary>
10+
/// 905. Sort Array By Parity
11+
/// Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.You may return any answer array that satisfies this condition.
12+
/// </summary>
13+
public class LeetCode_905
14+
{
15+
/// <summary>
16+
/// 思路:第一个检查是否是奇数,如果是的话和最后一个交换,在进行检测是否是奇数,和倒数第二个交换,以此往后,到i=n-flag时截止
17+
/// </summary>
18+
/// 260 ms 31.9MB
19+
/// <param name="A"></param>
20+
/// <returns></returns>
21+
public int[] MethodFor905_1(int[] A)
22+
{
23+
int n = A.Length;
24+
int temp;
25+
int flag = 1;
26+
27+
for (int i = 0; i < n ; i++)
28+
{
29+
if (A[i] % 2 == 1)
30+
{
31+
temp = A[i];
32+
A[i] = A[n - flag];
33+
A[n - flag] = temp;
34+
35+
flag++;
36+
i--;
37+
}
38+
if (i==n-flag) { break; }
39+
}
40+
return A;
41+
}
42+
43+
/// <summary>
44+
/// 参考
45+
/// </summary>
46+
/// <param name="A"></param>
47+
/// <returns></returns>
48+
public int[] MethodFor905_2(int[] A)
49+
{
50+
int l = 0;
51+
int r = A.Length - 1;
52+
while (l < r)
53+
{
54+
while (l < r && A[l] % 2 == 0)
55+
{
56+
l++;
57+
}
58+
while (l < r && A[r] % 2 == 1)
59+
{
60+
r--;
61+
}
62+
if (l < r)
63+
{
64+
int temp = A[l];
65+
A[l] = A[r];
66+
A[r] = temp;
67+
}
68+
}
69+
return A;
70+
}
71+
72+
/// <summary>
73+
/// 参考
74+
/// </summary>
75+
/// <param name="A"></param>
76+
/// <returns></returns>
77+
public int[] MethodFor905_3(int[] A)
78+
{
79+
return A.Where(x => x % 2 == 0).Concat(A.Where(x => x % 2 != 0)).ToArray();
80+
}
81+
}
82+
}

Week_01/id_86/LeetCode_905_086.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
class Solution(object):
3+
def sortArrayByParity(self, A):
4+
return [a for a in A if not a % 2] + [a for a in A if a % 2]

Week_01/id_86/LeetCode_922_086.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
int *MethodFor922_1(int const *A, int ASize, int *BSize)
2+
{
3+
int o = 1;
4+
int e = 0;
5+
int *ret = (int *)malloc(sizeof(int) * (*BSize = ASize));
6+
7+
for (int i = 0; i < *BSize; ++i)
8+
{
9+
if (A[i] % 2 == 0)
10+
{
11+
ret[e] = A[i];
12+
e = e + 2;
13+
}
14+
else
15+
{
16+
ret[o] = A[i];
17+
o = o + 2;
18+
}
19+
}
20+
return ret;
21+
}

Week_01/id_86/LeetCode_922_086.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace AlgorithmTest
8+
{
9+
/// <summary>
10+
/// 922. Sort Array By Parity II
11+
/// Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.You may return any answer array that satisfies this condition.
12+
/// </summary>
13+
public class LeetCode_922_086
14+
{
15+
/// <summary>
16+
/// 316ms 35.4MB
17+
/// </summary>
18+
/// <param name="A"></param>
19+
/// <returns></returns>
20+
public int[] MethodFor922_1(int[] A)
21+
{
22+
int n = A.Length;
23+
int[] B = new int[n];
24+
25+
int e = 0;
26+
int o = 1;
27+
28+
for (int i = 0; i < n; i++)
29+
{
30+
if (A[i] % 2 == 0)
31+
{
32+
B[e] = A[i];
33+
e = e + 2;
34+
}
35+
else
36+
{
37+
B[o] = A[i];
38+
o = o + 2;
39+
}
40+
}
41+
return B;
42+
}
43+
}
44+
}

Week_01/id_86/LeetCode_922_086.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def sortArrayByParityII(self, A):
3+
e, o = [a for a in A if not a % 2], [a for a in A if a % 2]
4+
return [e.pop() if not i % 2 else o.pop() for i in range(len(A))]

Week_02/id_86/LeetCode_242_086.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<stdbool.h>
2+
3+
// 120ms 7.1MB
4+
bool MethodFor242_1(char* s, char* t)
5+
{
6+
int flag = 1;
7+
if (strlen(s) != strlen(t))
8+
{
9+
flag = 0;
10+
}
11+
else
12+
{
13+
int a[26] = { 0 };
14+
for (int i = 0; i < strlen(s); i++)
15+
{
16+
a[s[i] - 'a']++;
17+
a[t[i] - 'a']--;
18+
}
19+
20+
for (int i = 0; i < 26; i++)
21+
{
22+
if (a[i] != 0)
23+
{
24+
flag = 0;
25+
}
26+
}
27+
}
28+
return flag;
29+
30+
}

Week_02/id_86/LeetCode_242_086.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace AlgorithmTest
8+
{
9+
class LeetCode_242_086
10+
{
11+
/// <summary>
12+
/// 104ms 21.7MB
13+
/// </summary>
14+
/// <param name="s"></param>
15+
/// <param name="t"></param>
16+
/// <returns></returns>
17+
public bool MethodFor242_1(string s, string t)
18+
{
19+
if (s.Length != t.Length) return false;
20+
21+
Dictionary<char, int> d = new Dictionary<char, int>();
22+
23+
for (int i = 0; i < s.Length; i++)
24+
{
25+
if (d.ContainsKey(s[i]))
26+
{
27+
d[s[i]]++;
28+
29+
if (d[s[i]] == 0)
30+
{
31+
d.Remove(s[i]);
32+
}
33+
}
34+
else
35+
{
36+
d.Add(s[i],1);
37+
}
38+
39+
if (d.ContainsKey(t[i]))
40+
{
41+
d[t[i]]--;
42+
43+
if (d[t[i]]==0)
44+
{
45+
d.Remove(t[i]);
46+
}
47+
}
48+
else
49+
{
50+
d.Add(t[i], -1);
51+
}
52+
}
53+
54+
return d.Count==0;
55+
}
56+
}
57+
}

Week_02/id_86/LeetCode_242_086.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def isAnagram(self, s, t):
3+
dic1 = {}
4+
dic2 = {}
5+
for t in s:
6+
dic1[t] = dic1.get(t,0)+1
7+
for t in t:
8+
dic2[t] = dic2.get(t,0)+1
9+
return dic1 == dic2
10+
11+
def isAnagram2(self, s, t):
12+
return sorted(s) == sorted(t)

0 commit comments

Comments
 (0)