Skip to content

Commit ad1d128

Browse files
committed
2 parents 6e19c74 + f788c8e commit ad1d128

21 files changed

+770
-17
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Large diffs are not rendered by default.

c/0155-min-stack.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include <stdlib.h>
2+
#include <assert.h>
3+
4+
// A structure to represent an element of the stack.
5+
typedef struct Node {
6+
int value;
7+
struct Node* next;
8+
} Node;
9+
10+
Node* nodeCreate(int val) {
11+
Node* node = (Node*)malloc(sizeof(Node));
12+
node->value = val;
13+
node->next = NULL;
14+
return node;
15+
}
16+
17+
void nodePush(Node** obj, int val) {
18+
assert(obj);
19+
Node* node = (Node*)malloc(sizeof(Node));
20+
node->value = val;
21+
node->next = *obj;
22+
*obj = node;
23+
}
24+
25+
void nodePop(Node** obj) {
26+
assert(obj);
27+
if (*obj) {
28+
Node* tmp = *obj;
29+
*obj = tmp->next;
30+
free(tmp);
31+
}
32+
}
33+
34+
// A structure to represent a stack.
35+
typedef struct {
36+
int size;
37+
Node* top;
38+
} MyStack;
39+
40+
MyStack* stackCreate()
41+
{
42+
MyStack* stack = (MyStack*)malloc(sizeof(MyStack));
43+
stack->size = 0;
44+
stack->top = NULL;
45+
return stack;
46+
}
47+
48+
void stackPush(MyStack* stack, int item)
49+
{
50+
if (!stack->top) {
51+
stack->top = nodeCreate(item);
52+
} else {
53+
nodePush(&stack->top, item);
54+
}
55+
stack->size++;
56+
}
57+
58+
void stackPop(MyStack* stack)
59+
{
60+
nodePop(&stack->top);
61+
stack->size--;
62+
}
63+
64+
int stackTop(MyStack* stack)
65+
{
66+
assert(stack);
67+
assert(stack->top);
68+
return stack->top->value;
69+
}
70+
71+
typedef struct {
72+
MyStack* stk;
73+
MyStack* minStk;
74+
} MinStack;
75+
76+
MinStack* minStackCreate() {
77+
MinStack* min = (MinStack*)malloc(sizeof(MinStack));
78+
min->stk = NULL;
79+
min->minStk = NULL;
80+
return min;
81+
}
82+
83+
void minStackPush(MinStack* obj, int val) {
84+
assert(obj);
85+
if(!obj->stk) {
86+
obj->stk = stackCreate();
87+
obj->minStk = stackCreate();
88+
}
89+
stackPush(obj->stk, val);
90+
if (obj->minStk->top) {
91+
int minTop = stackTop(obj->minStk);
92+
if ( val < minTop) {
93+
stackPush(obj->minStk, val);
94+
} else {
95+
stackPush(obj->minStk, minTop);
96+
}
97+
} else {
98+
stackPush(obj->minStk, val);
99+
}
100+
}
101+
102+
void minStackPop(MinStack* obj) {
103+
assert(obj);
104+
stackPop(obj->stk);
105+
stackPop(obj->minStk);
106+
}
107+
108+
int minStackTop(MinStack* obj) {
109+
assert(obj);
110+
return stackTop(obj->stk);
111+
}
112+
113+
int minStackGetMin(MinStack* obj) {
114+
assert(obj);
115+
return stackTop(obj->minStk);
116+
}
117+
118+
void minStackFree(MinStack* obj) {
119+
assert(obj);
120+
if (obj->stk) {
121+
while(obj->stk->top) {
122+
stackPop(obj->stk);
123+
}
124+
while(obj->minStk->top) {
125+
stackPop(obj->minStk);
126+
}
127+
free(obj->stk);
128+
free(obj->minStk);
129+
}
130+
free(obj);
131+
}
132+
133+
/**
134+
* Your MinStack struct will be instantiated and called as such:
135+
* MinStack* obj = minStackCreate();
136+
* minStackPush(obj, val);
137+
* minStackPop(obj);
138+
* int param_3 = minStackTop(obj);
139+
* int param_4 = minStackGetMin(obj);
140+
* minStackFree(obj);
141+
*/
142+

c/0225-implement-stack-using-queue.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// A structure to represent an element of the queue.
2+
typedef struct Node {
3+
int value;
4+
struct Node* next;
5+
} Node;
6+
7+
typedef struct MyQueue {
8+
int size;
9+
Node* front;
10+
Node* rear;
11+
} MyQueue;
12+
13+
MyQueue* MyQueueCreate() {
14+
MyQueue* queue = (MyQueue*)malloc(sizeof(MyQueue));
15+
queue->size = 0;
16+
queue->front = NULL;
17+
queue->rear = NULL;
18+
return queue;
19+
}
20+
21+
void MyQueuePush(MyQueue** obj, int val) {
22+
assert(obj);
23+
assert(*obj);
24+
MyQueue* queue = *obj;
25+
Node* node = (Node*)malloc(sizeof(Node));
26+
node->value = val;
27+
node->next = NULL;
28+
if (queue->rear) {
29+
queue->rear->next = node;
30+
queue->rear = node;
31+
} else {
32+
queue->front = node;
33+
queue->rear = node;
34+
}
35+
queue->size++;
36+
}
37+
38+
void MyQueuePop(MyQueue** obj) {
39+
assert(obj);
40+
assert(*obj);
41+
MyQueue* queue = *obj;
42+
Node* tmp = queue->front;
43+
if (queue->size > 0 && tmp) {
44+
queue->front = queue->front->next;
45+
queue->size--;
46+
free(tmp);
47+
}
48+
if (queue->rear == tmp) {
49+
queue->rear = NULL;
50+
}
51+
}
52+
53+
int MyQueueFront(MyQueue** obj) {
54+
assert(obj);
55+
MyQueue* queue = *obj;
56+
assert(queue->front);
57+
return queue->front->value;
58+
}
59+
60+
int MyQueueRear(MyQueue** obj) {
61+
assert(obj);
62+
MyQueue* queue = *obj;
63+
assert(queue->rear);
64+
return queue->rear->value;
65+
}
66+
67+
int MyQueueSize(MyQueue** obj) {
68+
assert(obj);
69+
assert(*obj);
70+
return (*obj)->size;
71+
}
72+
73+
typedef struct {
74+
MyQueue* queue;
75+
} MyStack;
76+
77+
78+
MyStack* myStackCreate() {
79+
MyStack* stack = (MyStack*)malloc(sizeof(MyStack));
80+
stack->queue = NULL;
81+
return stack;
82+
}
83+
84+
void myStackPush(MyStack* obj, int x) {
85+
assert(obj);
86+
int size;
87+
if (!obj->queue) {
88+
obj->queue = MyQueueCreate();
89+
}
90+
MyQueuePush(&obj->queue, x);
91+
size = obj->queue->size;
92+
while(--size > 0) {
93+
MyQueuePush(&obj->queue, MyQueueFront(&obj->queue));
94+
MyQueuePop(&obj->queue);
95+
}
96+
}
97+
98+
int myStackPop(MyStack* obj) {
99+
assert(obj);
100+
int val = MyQueueFront(&obj->queue);
101+
MyQueuePop(&obj->queue);
102+
return val;
103+
}
104+
105+
int myStackTop(MyStack* obj) {
106+
assert(obj);
107+
assert(obj->queue);
108+
return MyQueueFront(&obj->queue);
109+
}
110+
111+
bool myStackEmpty(MyStack* obj) {
112+
assert(obj);
113+
if (obj->queue) {
114+
return obj->queue->size == 0;
115+
}
116+
return true;
117+
}
118+
119+
void myStackFree(MyStack* obj) {
120+
if(obj) {
121+
while(obj->queue && obj->queue->front) {
122+
MyQueuePop(&obj->queue);
123+
}
124+
free(obj);
125+
}
126+
}
127+
128+
/**
129+
* Your MyStack struct will be instantiated and called as such:
130+
* MyStack* obj = myStackCreate();
131+
* myStackPush(obj, x);
132+
* int param_2 = myStackPop(obj);
133+
* int param_3 = myStackTop(obj);
134+
* bool param_4 = myStackEmpty(obj);
135+
* myStackFree(obj);
136+
*/

dart/0014-longest-common-prefix.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
String longestCommonPrefix(List<String> strs) {
3+
String res = "";
4+
for (int i=0; i<strs[0].length; i++) {
5+
for (String s in strs) {
6+
if (i >= s.length || strs[0][i] != s[i]){
7+
return res;
8+
}
9+
}
10+
res += strs[0][i];
11+
}
12+
return res;
13+
}
14+
}

dart/0020-valid-parentheses.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
bool isValid(String s) {
3+
var res = [];
4+
for (int i=0; i<s.length; i++) {
5+
if (s[i] != ']' && s[i] != ')' && s[i] != '}') {
6+
res.add(s[i]);
7+
continue;
8+
} else {
9+
if (res.isEmpty) {
10+
return false;
11+
}
12+
}
13+
14+
if (s[i] == ']') {
15+
if (res.last == '[') {
16+
res.removeLast();
17+
} else {
18+
return false;
19+
}
20+
} else if (s[i] == ')') {
21+
if (res.last == '(') {
22+
res.removeLast();
23+
} else {
24+
return false;
25+
}
26+
} else if (s[i] == '}') {
27+
if (res.last == '{') {
28+
res.removeLast();
29+
} else {
30+
return false;
31+
}
32+
}
33+
}
34+
return res.length == 0;
35+
}
36+
}

dart/0021-merge-two-sorted-lists.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode? next;
6+
* ListNode([this.val = 0, this.next]);
7+
* }
8+
*/
9+
class Solution {
10+
ListNode? mergeTwoLists(ListNode? list1, ListNode? list2) {
11+
ListNode? head = ListNode();
12+
ListNode? cur = head;
13+
while (list1 != null && list2 != null) {
14+
if (list1!.val < list2!.val) {
15+
cur!.next = list1;
16+
list1 = list1!.next;
17+
} else {
18+
cur!.next = list2;
19+
list2 = list2!.next;
20+
}
21+
cur = cur!.next;
22+
}
23+
cur!.next = (list1 == null) ? list2 : list1;
24+
return head.next;
25+
}
26+
}

dart/0027-remove-element.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
int removeElement(List<int> nums, int val) {
3+
int i = 0;
4+
int end = nums.length - 1;
5+
while (i <= end) {
6+
if (nums[i] == val) {
7+
int tmp = nums[end];
8+
nums[end] = nums[i];
9+
nums[i] = tmp;
10+
end -= 1;
11+
} else {
12+
i += 1;
13+
}
14+
}
15+
return i;
16+
}
17+
}

dart/0058-length-of-last-word.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
int lengthOfLastWord(String s) {
3+
final words = s.trim().split(" ");
4+
return words[words.length - 1].length;
5+
}
6+
}

0 commit comments

Comments
 (0)