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

Commit8eca8c9

Browse files
authored
Merge branch 'main' into main
2 parentsf51f5b7 +cb98925 commit8eca8c9

File tree

84 files changed

+1984
-113
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1984
-113
lines changed

‎.github/workflows/stale.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ jobs:
2424
repo-token:${{ secrets.GITHUB_TOKEN }}
2525
stale-issue-message:'Stale issue message'
2626
stale-pr-message:'Stale pull request message'
27-
stale-issue-label:'no-issue-activity'
28-
stale-pr-label:'no-pr-activity'
27+
stale-issue-label:'stale'
28+
stale-pr-label:'stale'
2929
days-before-stale:30
3030
days-before-close:7
31-
days-before-issue-stale:90
31+
days-before-issue-stale:900
3232
exempt-milestones:true
3333
exempt-pr-labels:'pending'

‎README.md

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

‎c/108-Convert-Sorted-Array.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Given an integer array nums where the elements are sorted in
3+
ascending order, convert it to a height-balanced binary search tree.
4+
5+
Space: O(n)
6+
Time: O(n)
7+
*/
8+
9+
structTreeNode*dichomoty_rec(int*nums,inti,intj) {
10+
if (i>j)
11+
returnNULL;
12+
structTreeNode*new_t=malloc(sizeof(structTreeNode));
13+
intm= (i+j)/2;
14+
new_t->val=nums[m];
15+
new_t->left=dichomoty_rec(nums,i,m-1);
16+
new_t->right=dichomoty_rec(nums,m+1,j);
17+
returnnew_t;
18+
}
19+
20+
structTreeNode*sortedArrayToBST(int*nums,intnumsSize){
21+
returndichomoty_rec(nums,0,numsSize-1);
22+
}

‎c/112-Path-Sum.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Given the root of a binary tree and an integer targetSum, return true
3+
if the tree has a root-to-leaf path such that adding up all the values
4+
along the path equals targetSum.
5+
6+
Space: O(log(n)) (due to recursive calls)
7+
Time: O(n)
8+
*/
9+
10+
boolhasPathSum(structTreeNode*root,inttargetSum){
11+
if (!root)
12+
return false;
13+
if (!root->left&& !root->right)
14+
returnroot->val==targetSum;
15+
returnhasPathSum(root->left,targetSum-root->val)||hasPathSum(root->right,targetSum-root->val);
16+
}

‎c/118-Pascals-Triangle.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Given an integer numRows, return the first numRows of Pascal's triangle.
3+
4+
Space: O(n²) (n=numRows)
5+
Time: O(n²)
6+
*/
7+
8+
int**generate(intnumRows,int*returnSize,int**returnColumnSizes){
9+
*returnSize=numRows;
10+
(*returnColumnSizes)=malloc(sizeof(int*)*numRows);
11+
int**ans=malloc(sizeof(int*)*numRows);
12+
for (inti=0;i<numRows;i++) {
13+
(*returnColumnSizes)[i]=i+1;
14+
ans[i]=malloc(sizeof(int)*(i+1));
15+
ans[i][0]=1;
16+
ans[i][i]=1;
17+
}
18+
for (inti=2;i<numRows;i++) {
19+
for (intj=1;j<i;j++) {
20+
ans[i][j]=ans[i-1][j-1]+ans[i-1][j];
21+
}
22+
}
23+
returnans;
24+
}

‎c/1189-Maximum-Number-of-Balloons.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.
3+
4+
Space: O(1)
5+
Time: O(n)
6+
*/
7+
8+
9+
intmin(inta,intb) {
10+
returna<b?a:b;
11+
}
12+
13+
intmaxNumberOfBalloons(char*text){
14+
// Counter for each letter
15+
intb=0;
16+
inta=0;
17+
intl=0;
18+
into=0;
19+
intn=0;
20+
for (inti=0;text[i]!='\0';i++) {
21+
if (text[i]=='b') {
22+
b++;
23+
}elseif (text[i]=='a') {
24+
a++;
25+
}elseif (text[i]=='l') {
26+
l++;
27+
}elseif (text[i]=='o') {
28+
o++;
29+
}elseif (text[i]=='n') {
30+
n++;
31+
}
32+
}
33+
l /=2;// There is 2 'l' in balloon
34+
o /=2;// There is 2 'o' in balloon
35+
returnmin(b,min(a,min(l,min(o,n))));
36+
}

‎c/129-Sum-Root-To-Leaf.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Return the total sum of all root-to-leaf numbers.
3+
4+
Space: O(1)
5+
Time: O(n)
6+
*/
7+
8+
intdfs(structTreeNode*r,intacc) {
9+
if (r==NULL)
10+
returnacc;
11+
if (r->left==NULL&&r->right==NULL)
12+
returnacc*10+r->val;
13+
if (r->left==NULL)
14+
returndfs(r->right,acc*10+r->val);
15+
if (r->right==NULL)
16+
returndfs(r->left,acc*10+r->val);
17+
returndfs(r->right,acc*10+r->val)+dfs(r->left,acc*10+r->val);
18+
}
19+
20+
intsumNumbers(structTreeNode*root){
21+
returndfs(root,0);
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.
3+
4+
Space: O(1)
5+
Time: O(n)
6+
*/
7+
8+
intmax(inta,intb) {
9+
returna>b?a:b;
10+
}
11+
12+
int*replaceElements(int*arr,intarrSize,int*returnSize){
13+
intgreatest=-1;
14+
*returnSize=arrSize;
15+
for (inti=arrSize-1;i>=0;i--) {
16+
intm=greatest;
17+
greatest=max(greatest,arr[i]);
18+
arr[i]=m;
19+
}
20+
returnarr;
21+
}

‎c/169-Majority-Element.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Given an array nums of size n, return the majority element.
3+
4+
Space: O(1)
5+
Time: O(n)
6+
*/
7+
8+
intmajorityElement(int*nums,intnumsSize){
9+
intcandidate=nums[0];
10+
intcount=1;
11+
for (inti=1;i<numsSize;i++) {
12+
if (candidate==nums[i]) {
13+
count++;
14+
}else {
15+
count--;
16+
if (count==0) {
17+
count=1;
18+
candidate=nums[i];
19+
}
20+
}
21+
}
22+
returncandidate;
23+
}

‎c/1905-Count-Sub-Islands.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
3+
Space: O(n²) (due to recursives calls)
4+
Time: O(n²)
5+
*/
6+
7+
booldfs_test(int**grid1,int**grid2,inti,intj,intn,intm) {
8+
// Test if the island in grid2 is entirely in grid1 and delete the island in grid2
9+
boolans= (grid1[i][j]==1);
10+
grid2[i][j]=0;
11+
if (i>0&&grid2[i-1][j]==1)
12+
ans=dfs_test(grid1,grid2,i-1,j,n ,m)&&ans;
13+
if (j>0&&grid2[i][j-1]==1)
14+
ans=dfs_test(grid1,grid2,i,j-1,n ,m)&&ans;
15+
if (i<(n-1)&&grid2[i+1][j]==1)
16+
ans=dfs_test(grid1,grid2,i+1,j,n ,m)&&ans;
17+
if (j<(m-1)&&grid2[i][j+1]==1)
18+
ans=dfs_test(grid1,grid2,i,j+1,n ,m)&&ans;
19+
returnans;
20+
}
21+
22+
intcountSubIslands(int**grid1,intgrid1Size,int*grid1ColSize,int**grid2,intgrid2Size,int*grid2ColSize){
23+
intcpt=0;
24+
for (inti=0;i<grid1Size;i++) {
25+
for (intj=0;j<grid1ColSize[i];j++) {
26+
if (grid2[i][j]==1) {
27+
// Test if the island in grid2 is contained in an island in grid1
28+
if (dfs_test(grid1,grid2,i,j,grid1Size,grid1ColSize[i])){
29+
cpt++;
30+
}
31+
}
32+
}
33+
}
34+
returncpt;
35+
}

‎c/1968-Array-Not-Average-Neighbors.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
You want to rearrange the elements in the array such that every element
3+
in the rearranged array is not equal to the average of its neighbors
4+
5+
Space: O(n)
6+
Time: O(nlog(n)) (quicksort)
7+
*/
8+
9+
intcmp(constvoid*a,constvoid*b) {
10+
return*(int*)a-*(int*)b;
11+
}
12+
int*rearrangeArray(int*nums,intnumsSize,int*returnSize){
13+
int*ans=malloc(sizeof(int)*numsSize);
14+
*returnSize=numsSize;
15+
qsort(nums,numsSize,sizeof(int),cmp);
16+
inti,j=0;
17+
for (i=1;i<numsSize;i+=2) {// Put numbers in odd indexes
18+
ans[i]=nums[j];
19+
j++;
20+
}
21+
for (i=0;i<numsSize;i+=2) {// Put numbers in even indexes
22+
ans[i]=nums[j];
23+
j++;
24+
}
25+
returnans;
26+
}

‎c/205-Isomorphic-Strings.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Given two strings s and t, determine if they are isomorphic.
3+
4+
Space: O(1)
5+
Time: O(n)
6+
*/
7+
8+
boolisIsomorphic(char*s,char*t){
9+
intalphabet_s[256];// Alphabet of t letters to s
10+
intalphabet_t[256];// Alphabet of s letters to t
11+
for (inti=0;i<256;i++){// Fill alphabets with empty values
12+
alphabet_s[i]=-1;
13+
alphabet_t[i]=-1;
14+
}
15+
inti;// To be able to use it outside the loop for
16+
for (i=0;s[i]!='\0';i++) {
17+
if (alphabet_t[s[i]]==-1&&alphabet_s[t[i]]==-1) {
18+
alphabet_t[s[i]]=t[i];
19+
alphabet_s[t[i]]=s[i];
20+
}elseif (alphabet_t[s[i]]==-1) {
21+
if (alphabet_s[t[i]]!=s[i]) {
22+
return false;
23+
}
24+
alphabet_t[s[i]]=t[i];
25+
}elseif (alphabet_s[t[i]]==-1) {
26+
if (alphabet_t[s[i]]!=t[i]) {
27+
return false;
28+
}
29+
alphabet_s[t[i]]=s[i];
30+
}elseif (alphabet_t[s[i]]!=t[i]||alphabet_s[t[i]]!=s[i]) {
31+
return false;
32+
}
33+
}
34+
returnt[i]=='\0';
35+
}

‎c/209-Minimum-Size-Subarray-Sum.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Given an array of positive integers nums and a positive integer target, return
3+
the minimal length of a contiguous subarray of which the sum is greater than or
4+
equal to target
5+
6+
Space: O(1)
7+
Time: O(n)
8+
*/
9+
10+
intmin(inta,intb) {
11+
returna<b?a:b;
12+
}
13+
14+
intminSubArrayLen(inttarget,int*nums,intnumsSize){
15+
intlen=0;
16+
inti=0,j=0;
17+
intcpt=0;
18+
while (j<numsSize) {
19+
cpt+=nums[j];
20+
if (cpt>=target) {
21+
if (len==0)
22+
len=j-i+1;
23+
while (cpt-nums[i] >=target) {
24+
cpt-=nums[i];
25+
len=min(len,j-i);
26+
i++;
27+
}
28+
}
29+
j++;
30+
}
31+
returnlen;
32+
}

‎c/23-Merge-K-Sorted-Lists.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
9+
/**
10+
* Time: O(n.log(k))
11+
* Space: O(1)
12+
*/
13+
14+
typedefstructListNodeListNode;
15+
16+
ListNode*mergeLists(ListNode*l1,ListNode*l2)
17+
{
18+
ListNodedummy;
19+
dummy.next=NULL;
20+
ListNode*tail=&dummy;
21+
22+
while(l1&&l2)
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+
35+
tail=tail->next;
36+
}
37+
38+
if(l1)
39+
{
40+
tail->next=l1;
41+
}
42+
elseif(l2)
43+
{
44+
tail->next=l2;
45+
}
46+
47+
returndummy.next;
48+
}
49+
50+
structListNode*mergeKLists(structListNode**lists,intlistsSize){
51+
52+
if(listsSize==0)
53+
{
54+
returnNULL;
55+
}
56+
57+
while(listsSize>1)
58+
{
59+
ListNode**mergedLists;
60+
size_tmergedListsIndex=0;
61+
62+
for(size_ti=0;i<listsSize;i+=2)
63+
{
64+
ListNode*l1=lists[i];
65+
ListNode*l2= (i+1)<listsSize ?lists[i+1] :NULL;
66+
67+
mergedLists[mergedListsIndex++]=mergeLists(l1,l2);
68+
}
69+
70+
lists=mergedLists;
71+
listsSize= (listsSize+1) /2;
72+
}
73+
74+
returnlists[0];
75+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp