Movatterモバイル変換


[0]ホーム

URL:


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

Commitf788c8e

Browse files
authored
Merge branch 'neetcode-gh:main' into main
2 parentsec27ad3 +bbc1e33 commitf788c8e

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+
typedefstructNode {
6+
intvalue;
7+
structNode*next;
8+
}Node;
9+
10+
Node*nodeCreate(intval) {
11+
Node*node= (Node*)malloc(sizeof(Node));
12+
node->value=val;
13+
node->next=NULL;
14+
returnnode;
15+
}
16+
17+
voidnodePush(Node**obj,intval) {
18+
assert(obj);
19+
Node*node= (Node*)malloc(sizeof(Node));
20+
node->value=val;
21+
node->next=*obj;
22+
*obj=node;
23+
}
24+
25+
voidnodePop(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+
typedefstruct {
36+
intsize;
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+
returnstack;
46+
}
47+
48+
voidstackPush(MyStack*stack,intitem)
49+
{
50+
if (!stack->top) {
51+
stack->top=nodeCreate(item);
52+
}else {
53+
nodePush(&stack->top,item);
54+
}
55+
stack->size++;
56+
}
57+
58+
voidstackPop(MyStack*stack)
59+
{
60+
nodePop(&stack->top);
61+
stack->size--;
62+
}
63+
64+
intstackTop(MyStack*stack)
65+
{
66+
assert(stack);
67+
assert(stack->top);
68+
returnstack->top->value;
69+
}
70+
71+
typedefstruct {
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+
returnmin;
81+
}
82+
83+
voidminStackPush(MinStack*obj,intval) {
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+
intminTop=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+
voidminStackPop(MinStack*obj) {
103+
assert(obj);
104+
stackPop(obj->stk);
105+
stackPop(obj->minStk);
106+
}
107+
108+
intminStackTop(MinStack*obj) {
109+
assert(obj);
110+
returnstackTop(obj->stk);
111+
}
112+
113+
intminStackGetMin(MinStack*obj) {
114+
assert(obj);
115+
returnstackTop(obj->minStk);
116+
}
117+
118+
voidminStackFree(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+
typedefstructNode {
3+
intvalue;
4+
structNode*next;
5+
}Node;
6+
7+
typedefstructMyQueue {
8+
intsize;
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+
returnqueue;
19+
}
20+
21+
voidMyQueuePush(MyQueue**obj,intval) {
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+
voidMyQueuePop(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+
intMyQueueFront(MyQueue**obj) {
54+
assert(obj);
55+
MyQueue*queue=*obj;
56+
assert(queue->front);
57+
returnqueue->front->value;
58+
}
59+
60+
intMyQueueRear(MyQueue**obj) {
61+
assert(obj);
62+
MyQueue*queue=*obj;
63+
assert(queue->rear);
64+
returnqueue->rear->value;
65+
}
66+
67+
intMyQueueSize(MyQueue**obj) {
68+
assert(obj);
69+
assert(*obj);
70+
return (*obj)->size;
71+
}
72+
73+
typedefstruct {
74+
MyQueue*queue;
75+
}MyStack;
76+
77+
78+
MyStack*myStackCreate() {
79+
MyStack*stack= (MyStack*)malloc(sizeof(MyStack));
80+
stack->queue=NULL;
81+
returnstack;
82+
}
83+
84+
voidmyStackPush(MyStack*obj,intx) {
85+
assert(obj);
86+
intsize;
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+
intmyStackPop(MyStack*obj) {
99+
assert(obj);
100+
intval=MyQueueFront(&obj->queue);
101+
MyQueuePop(&obj->queue);
102+
returnval;
103+
}
104+
105+
intmyStackTop(MyStack*obj) {
106+
assert(obj);
107+
assert(obj->queue);
108+
returnMyQueueFront(&obj->queue);
109+
}
110+
111+
boolmyStackEmpty(MyStack*obj) {
112+
assert(obj);
113+
if (obj->queue) {
114+
returnobj->queue->size==0;
115+
}
116+
return true;
117+
}
118+
119+
voidmyStackFree(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+
classSolution {
2+
StringlongestCommonPrefix(List<String> strs) {
3+
String res="";
4+
for (int i=0; i<strs[0].length; i++) {
5+
for (String sin 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+
classSolution {
2+
boolisValid(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+
returnfalse;
11+
}
12+
}
13+
14+
if (s[i]==']') {
15+
if (res.last=='[') {
16+
res.removeLast();
17+
}else {
18+
returnfalse;
19+
}
20+
}elseif (s[i]==')') {
21+
if (res.last=='(') {
22+
res.removeLast();
23+
}else {
24+
returnfalse;
25+
}
26+
}elseif (s[i]=='}') {
27+
if (res.last=='{') {
28+
res.removeLast();
29+
}else {
30+
returnfalse;
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+
classSolution {
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+
classSolution {
2+
intremoveElement(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+
classSolution {
2+
intlengthOfLastWord(String s) {
3+
final words= s.trim().split(" ");
4+
return words[words.length-1].length;
5+
}
6+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp