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

Commit38ea709

Browse files
Merge branch 'neetcode-gh:main' into ginger
2 parents5c28166 +c440eb2 commit38ea709

File tree

75 files changed

+1764
-63
lines changed

Some content is hidden

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

75 files changed

+1764
-63
lines changed

‎README.md

Lines changed: 20 additions & 20 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
classSolution {
2+
public:
3+
stringremoveStars(string s) {
4+
stack<char> stk;
5+
for(int i=0;i<s.size();i++){
6+
if(s[i]=='*'){
7+
stk.pop();
8+
}else stk.push(s[i]);
9+
}
10+
string res ="";
11+
while(!stk.empty()){
12+
res += stk.top();
13+
stk.pop();
14+
}
15+
reverse(res.begin(), res.end());
16+
return res;
17+
}
18+
};

‎csharp/0187-repeated-dna-sequences.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
publicclassSolution
2+
{
3+
publicIList<string>FindRepeatedDnaSequences(strings)
4+
{
5+
varseen=newHashSet<string>();
6+
varresult=newHashSet<string>();
7+
8+
for(vari=0;i<s.Length-9;i++)
9+
{
10+
varcur=s.Substring(i,10);
11+
if(seen.Contains(cur))
12+
result.Add(cur);
13+
seen.Add(cur);
14+
}
15+
16+
returnresult.ToList();
17+
}
18+
}

‎csharp/0344-reverse-string.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
publicclassSolution
2+
{
3+
publicvoidReverseString(char[]s)
4+
{
5+
varh=s.Length/2;
6+
for(inti=0;i<h;i++)
7+
{
8+
vartemp=s[i];
9+
s[i]=s[s.Length-i-1];
10+
s[s.Length-i-1]=temp;
11+
}
12+
}
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
publicclassSolution
2+
{
3+
publicboolCheckSubarraySum(int[]nums,intk)
4+
{
5+
varremainder=newDictionary<int,int>();
6+
remainder.Add(0,-1);
7+
vartotal=0;
8+
for(vari=0;i<nums.Length;i++)
9+
{
10+
total+=nums[i];
11+
varr=total%k;
12+
if(!remainder.ContainsKey(r))
13+
remainder.Add(r,i);
14+
elseif(i-remainder[r]>1)
15+
returntrue;
16+
}
17+
returnfalse;
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
publicclassSolution
2+
{
3+
publicstringMergeAlternately(stringword1,stringword2)
4+
{
5+
varresult=string.Empty;
6+
varfirstPointer=0;
7+
varsecondPointer=0;
8+
while(firstPointer<word1.Length||secondPointer<word2.Length)
9+
{
10+
if(firstPointer<word1.Length)
11+
result+=word1[firstPointer++];
12+
13+
if(secondPointer<word2.Length)
14+
result+=word2[secondPointer++];
15+
}
16+
returnresult;
17+
}
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
classSolution {
2+
intminCostClimbingStairs(List<int> cost) {
3+
for (int i= cost.length-3; i>=0; i--) {
4+
cost[i]+=min(cost[i+1], cost[i+2]);
5+
}
6+
7+
returnmin(cost[0], cost[1]);
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
classSolution {
2+
publicintremoveDuplicates(int[]nums) {
3+
intk =0;
4+
for (intnum:nums) {
5+
if (k <2 ||nums[k-2] !=num) {
6+
nums[k++] =num;
7+
}
8+
}
9+
returnk;
10+
}
11+
}

‎java/0110-balanced-binary-tree.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,32 @@ public static boolean isBalanced(TreeNode root) {
2525
returndfs(root).getKey();
2626
}
2727
}
28+
29+
// Solution using the bottom up approach
30+
// TC and SC is On
31+
32+
classSolution {
33+
34+
publicintheight(TreeNoderoot){
35+
if(root ==null){
36+
return0;
37+
}
38+
39+
intlh =height(root.left);
40+
intrh =height(root.right);
41+
42+
return1 +Math.max(lh,rh);
43+
}
44+
45+
publicbooleanisBalanced(TreeNoderoot) {
46+
47+
if(root ==null){
48+
returntrue;
49+
}
50+
51+
intlh =height(root.left);
52+
intrh =height(root.right);
53+
54+
returnMath.abs(lh -rh) <=1 &&isBalanced(root.left) &&isBalanced(root.right);
55+
}
56+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
classSolution {
2+
publicNodeconnect(Noderoot) {
3+
Nodecurrent =root;
4+
Nodenext =root ==null ?null :root.left;
5+
6+
while (current !=null &&next !=null) {
7+
current.left.next =current.right;
8+
9+
if (current.next !=null) {
10+
current.right.next =current.next.left;
11+
}
12+
13+
current =current.next;
14+
15+
if (current ==null) {
16+
current =next;
17+
next =current.left;
18+
}
19+
}
20+
21+
returnroot;
22+
}
23+
}

‎java/0147-insertion-sort-list.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
classSolution {
2+
publicListNodeinsertionSortList(ListNodehead) {
3+
ListNodedummy =newListNode(0,head);
4+
5+
ListNodeprev =head;
6+
ListNodecur =head.next;
7+
while (cur !=null) {
8+
if (prev.val <=cur.val) {
9+
prev =cur;
10+
cur =cur.next;
11+
}else {
12+
ListNodeelem =dummy;
13+
while (elem.next.val <cur.val) {
14+
elem =elem.next;
15+
}
16+
17+
prev.next =cur.next;
18+
cur.next =elem.next;
19+
elem.next =cur;
20+
21+
cur =prev.next;
22+
}
23+
}
24+
25+
returndummy.next;
26+
}
27+
}

‎java/0162-find-peak-element.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
classSolution {
2+
publicintfindPeakElement(int[]nums) {
3+
intl =0,r =nums.length-1;
4+
while(l<=r){
5+
intm =l + (r-l)/2;
6+
intleft = (m-1==-1)?Integer.MIN_VALUE:nums[m-1];
7+
intright = (m+1==nums.length)?Integer.MIN_VALUE:nums[m+1];
8+
if(nums[m]>left &&nums[m]>right)returnm;
9+
elseif(nums[m]<left)r=m-1;
10+
elsel =m+1;
11+
}
12+
return0;
13+
}
14+
}

‎java/0226-invert-binary-tree.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ public TreeNode invertTree(TreeNode root) {
44
if (root ==null)returnnull;
55
TreeNodenode =newTreeNode(root.val);
66
node.right =invertTree(root.left);
7-
node.val =root.val;
87
node.left =invertTree(root.right);
98
returnnode;
109
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
classNumMatrix {
2+
int[][]preSum;
3+
4+
publicNumMatrix(int[][]matrix) {
5+
intm =matrix.length;
6+
intn =matrix[0].length;
7+
preSum =newint[m +1][n +1];
8+
9+
for (inti =0;i <m;i++) {
10+
for (intj =0;j <n;j++) {
11+
preSum[i +1][j +1] =preSum[i +1][j] +preSum[i][j +1] -preSum[i][j] +matrix[i][j];
12+
}
13+
}
14+
}
15+
16+
publicintsumRegion(introw1,intcol1,introw2,intcol2) {
17+
returnpreSum[row2 +1][col2 +1] -preSum[row2 +1][col1] -preSum[row1][col2 +1] +preSum[row1][col1];
18+
}
19+
}
20+
21+
/**
22+
* Your NumMatrix object will be instantiated and called as such:
23+
* NumMatrix obj = new NumMatrix(matrix);
24+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
25+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
publicclassSolution {
2+
publicintnumberOfArithmeticSlices(int[]nums) {
3+
intres =0;
4+
intn =nums.length;
5+
6+
Map<Long,Integer>[]dp =newHashMap[n];
7+
for (inti =0;i <n;i++) {
8+
dp[i] =newHashMap<>();
9+
}
10+
11+
for (inti =0;i <n;i++) {
12+
for (intj =0;j <i;j++) {
13+
longdiff = (long)nums[i] - (long)nums[j];
14+
dp[i].put(diff,dp[i].getOrDefault(diff,0) +1 +dp[j].getOrDefault(diff,0));
15+
res +=dp[j].getOrDefault(diff,0);
16+
}
17+
}
18+
19+
returnres;
20+
}
21+
}

‎java/0455-assign-cookies.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
classSolution {
2+
publicintfindContentChildren(int[]g,int[]s) {
3+
Arrays.sort(g);
4+
Arrays.sort(s);
5+
6+
inti =0,j =0;
7+
while(i <g.length){
8+
while(j <s.length &&g[i] >s[j])
9+
j +=1;
10+
if(j ==s.length)
11+
break;
12+
i +=1;
13+
j +=1;
14+
}
15+
returni;
16+
}
17+
}

‎java/0661-image-smoother.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
classSolution {
2+
publicint[][]imageSmoother(int[][]img) {
3+
intROWS =img.length,COLS =img[0].length;
4+
int[][]res =newint[ROWS][COLS];
5+
6+
for(intr =0;r <ROWS;r++){
7+
for(intc =0;c <COLS;c++){
8+
inttotal =0,cnt =0;
9+
for(inti =r -1;i <r +2;i++){
10+
for(intj =c -1;j <c +2;j++){
11+
if(i <0 ||i ==ROWS ||j <0 ||j ==COLS)
12+
continue;
13+
total +=img[i][j];
14+
cnt +=1;
15+
}
16+
}
17+
res[r][c] =total /cnt;
18+
}
19+
}
20+
returnres;
21+
}
22+
}
23+
24+
// Optimal Solution
25+
26+
classSolution {
27+
publicint[][]imageSmoother(int[][]img) {
28+
intROWS =img.length,COLS =img[0].length;
29+
30+
for(intr =0;r <ROWS;r++){
31+
for(intc =0;c <COLS;c++){
32+
inttotal =0,cnt =0;
33+
34+
for(inti =r -1;i <r +2;i++){
35+
for(intj =c -1;j <c +2;j++){
36+
if(i <0 ||i ==ROWS ||j <0 ||j ==COLS)
37+
continue;
38+
total +=img[i][j] %256;
39+
cnt +=1;
40+
}
41+
}
42+
img[r][c] =img[r][c] ^ (total /cnt) <<8;
43+
}
44+
}
45+
for(intr =0;r <ROWS;r++){
46+
for(intc =0;c <COLS;c++){
47+
img[r][c] =img[r][c] >>8;
48+
}
49+
}
50+
returnimg;
51+
}
52+
}

‎java/0704-binary-search.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ public int search(int[] nums, int target) {
55
intj =nums.length -1;
66

77
while (i <=j) {
8-
intmid = (i +j) /2;
8+
// mid is calculated this way to prevent integer overflow.
9+
// See: https://blog.research.google/2006/06/extra-extra-read-all-about-it-nearly.html
10+
intmid =i + (j -i) /2;
911

1012
if (nums[mid] ==target)returnmid;elseif (
1113
nums[mid] <target

‎java/0872-leaf-similar-trees.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
classSolution {
2+
publicbooleanleafSimilar(TreeNoderoot1,TreeNoderoot2) {
3+
ArrayList<Integer>ls1 =newArrayList<>();
4+
ArrayList<Integer>ls2 =newArrayList<>();
5+
tree(root1,ls1);
6+
tree(root2,ls2);
7+
returnls1.equals(ls2);
8+
}
9+
privatevoidtree(TreeNoderoot,List<Integer>ls){
10+
if(root ==null)
11+
return;
12+
if(root.left ==null &&root.right ==null)
13+
ls.add(root.val);
14+
tree(root.left,ls);
15+
tree(root.right,ls);
16+
}
17+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp