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

Commit85cf118

Browse files
committed
# Conflicts:#python/929-Unique-Email-Addresses.py
2 parentsbc4a3f0 +43119ae commit85cf118

File tree

101 files changed

+2432
-164
lines changed

Some content is hidden

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

101 files changed

+2432
-164
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'

‎CONTRIBUTING.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
Solutions from these languages will be linked from[NeetCode.io](https://neetcode.io):
55

6-
* Python (complete)
7-
* C++ (complete)
6+
* Python
7+
* C++
88
* Java
99
* Javascript
1010

@@ -17,9 +17,8 @@ If you would like to have collaborator permissions on the repo to merge your own
1717
##Contributing Guidelines
1818

1919
-**Match the casing of files and directories**
20-
- For files, it's**`<language>/<problem-set>/<category>/<problem-number>_name_of_problem.<language-extension>`** (e.g.`java/neetcode_150/01_arrays_&_hashing/0001_two_sum.java`)
21-
- Please add the appropriate number of preceding zeros to the problem number, to ensure files get sorted properly (e.g.`0001`,`0012`,`0123`,`1234`)
22-
- Please take the problem name from the URL of the problem, as[described in this issue](https://github.com/neetcode-gh/leetcode/issues/457#issuecomment-1233558291)
20+
- For files, it's**`<language>/<problem-number>-Name-Of-Problem.<language-extension>`** (e.g.`java/1-Two-Sum.java`)
21+
-*Note: This is subject to change in the future, as[described in this issue](https://github.com/neetcode-gh/leetcode/issues/457#issuecomment-1233558291)*
2322
-**Give your PR a succinct and accurate title** (e.g._"Create: 1-Two-Sum.py"_)
2423
- Prefer**one solution/change per PR** (not a hard and fast rule, but will typically make the review cycle shorter)
2524
-**Follow the**[PR template](./.github/pull_request_template.md) and add additional information when needed
@@ -44,7 +43,7 @@ If you would like to have collaborator permissions on the repo to merge your own
4443
**A:** Leetcode's runtime measurement can be severely inaccurate varying based on server load, time of day and many other factors. In general, readability and clarity of the code (in the context of interviews) are features more important than performance gains, however changes that transparently improve performance will be accepted.
4544
##
4645

47-
**Q:** What if the problem I want to add isn't in the Neetcode 150 list(Missing Solutions table)?
46+
**Q:** What if the problem I want to add isn't in the Neetcode 150 listorMissing Solutions table?
4847

4948
**A:** Questions outside of the Neetcode 150 list can be added but please prioritise adding the listed solutions first.
5049
##

‎README.md

Lines changed: 75 additions & 75 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/138-Copy-List-with-Random-Pointer.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Definition for a Node.
3+
* struct Node {
4+
* int val;
5+
* struct Node *next;
6+
* struct Node *random;
7+
* };
8+
*/
9+
10+
/*
11+
Time: O(n)
12+
Space: O(1)
13+
*/
14+
15+
typedefstructNodeNode;
16+
structNode*copyRandomList(structNode*head) {
17+
18+
if(head==NULL)
19+
{
20+
returnNULL;
21+
}
22+
23+
/**
24+
* Insert each new node after each original node
25+
*/
26+
Node*curr=head;
27+
while(curr)
28+
{
29+
// Create the new node
30+
Node*newNode= (Node*)malloc(sizeof(Node));
31+
newNode->val=curr->val;
32+
33+
// Insert new node after the original node
34+
newNode->next=curr->next;
35+
curr->next=newNode;
36+
37+
// Move curr to the next original node
38+
curr=curr->next->next;
39+
}
40+
41+
42+
/**
43+
* Add the random node for each new node
44+
*/
45+
curr=head;
46+
Node*newNode=NULL;
47+
while(curr)
48+
{
49+
// The new node is the next node to the original node
50+
newNode=curr->next;
51+
52+
if(curr->random)
53+
{
54+
newNode->random=curr->random->next;
55+
}
56+
else
57+
{
58+
newNode->random=NULL;
59+
}
60+
61+
// Move curr to the next original node
62+
curr=curr->next->next;
63+
}
64+
65+
/**
66+
* Separate the original nodes list from the new nodes list
67+
*/
68+
Node*originalList=head;
69+
Node*copiedList=head->next;
70+
Node*copiedListHead=head->next;
71+
while(originalList)
72+
{
73+
originalList->next=originalList->next->next;
74+
if(copiedList->next)
75+
{
76+
copiedList->next=copiedList->next->next;
77+
}
78+
else
79+
{
80+
copiedList->next=NULL;
81+
}
82+
83+
originalList=originalList->next;
84+
copiedList=copiedList->next;
85+
}
86+
87+
88+
returncopiedListHead;
89+
}

‎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+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp