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

Commit7639cc7

Browse files
committed
Merge branch 'main' ofhttps://github.com/neetcode-gh/leetcode into mirvin/move-python-sols-to-dir
2 parents1ba7c0f +8bad0fb commit7639cc7

File tree

85 files changed

+2069
-46
lines changed

Some content is hidden

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

85 files changed

+2069
-46
lines changed

‎125-Valid-Palindrome.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
classSolution:
22
defisPalindrome(self,s:str)->bool:
3-
l,r=0,len(s)-1
4-
whilel<r:
5-
whilel<randnotself.alphanum(s[l]):
6-
l+=1
7-
whilel<randnotself.alphanum(s[r]):
8-
r-=1
9-
ifs[l].lower()!=s[r].lower():
3+
l=0
4+
r=len(s)-1
5+
whilel<r:
6+
ifs[l].lower()==s[r].lower():
7+
l+=1
8+
r-=1
9+
continue
10+
11+
elifnot (65<=ord(s[l])<=90or97<=ord(s[l])<=122or48<=ord(s[l])<=57):
12+
l+=1
13+
elifnot (65<=ord(s[r])<=90or97<=ord(s[r])<=122or48<=ord(s[r])<=57):
14+
r-=1
15+
else:
1016
returnFalse
11-
l+=1
12-
r-=1
1317
returnTrue
14-
15-
# Could write own alpha-numeric function
16-
defalphanum(self,c):
17-
return (ord('A')<=ord(c)<=ord('Z')or
18-
ord('a')<=ord(c)<=ord('z')or
19-
ord('0')<=ord(c)<=ord('9'))

‎261-Graph-Valid-Tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Problem is free on Lintcode
1+
# Problem is free on Lintcodehttps://www.lintcode.com/problem/178/
22
classSolution:
33
"""
44
@param n: An integer

‎csharp/1-Two-Sum.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
publicclassSolution{
2+
publicint[]TwoSum(int[]nums,inttarget){
3+
Dictionary<int,int>indices=newDictionary<int,int>();
4+
5+
for(inti=0;i<nums.Length;i++){
6+
vardiff=target-nums[i];
7+
if(indices.ContainsKey(diff)){
8+
returnnewint[]{indices[diff],i};
9+
}
10+
indices[nums[i]]=i;
11+
}
12+
returnnull;
13+
}
14+
}
15+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
publicclassSolution{
2+
publicintMaxProfit(int[]prices){
3+
intmaxProfit=0;
4+
intminPrice=prices[0];
5+
6+
for(inti=1;i<prices.Length;i++){
7+
intcurrPrice=prices[i];
8+
minPrice=Math.Min(minPrice,currPrice);
9+
maxProfit=Math.Max(maxProfit,currPrice-minPrice);
10+
}
11+
returnmaxProfit;
12+
}
13+
}

‎csharp/125-Valid-Palindrome.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
publicclassSolution{
2+
publicboolIsPalindrome(strings){
3+
intleft=0;
4+
intright=s.Length-1;
5+
6+
while(left<right){
7+
if(!char.IsLetterOrDigit(s[left])){
8+
left++;
9+
}elseif(!char.IsLetterOrDigit(s[right])){
10+
right--;
11+
}else{
12+
if(char.ToLower(s[left])!=char.ToLower(s[right])){
13+
returnfalse;
14+
}
15+
left++;
16+
right--;
17+
}
18+
}
19+
returntrue;
20+
}
21+
}

‎csharp/155-Min-Stack.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
publicclassMinStack{
2+
privateStack<int>stack;
3+
privateStack<int>minStack;
4+
5+
publicMinStack(){
6+
stack=newStack<int>();
7+
minStack=newStack<int>();
8+
}
9+
10+
publicvoidPush(intval){
11+
stack.Push(val);
12+
intmin=Math.Min(val,minStack.Count!=0?minStack.Peek():val);
13+
minStack.Push(min);
14+
}
15+
16+
publicvoidPop(){
17+
stack.Pop();
18+
minStack.Pop();
19+
}
20+
21+
publicintTop(){
22+
returnstack.Peek();
23+
}
24+
25+
publicintGetMin(){
26+
returnminStack.Peek();
27+
}
28+
}

‎csharp/167-Two-Sum-II.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
publicclassSolution{
2+
publicint[]TwoSum(int[]numbers,inttarget){
3+
// Using 2 pointers. Since sorted, if l+r > target, decrease r.
4+
// Else if l+r < target, increase l. Else, result is found.
5+
intleft=0,right=numbers.Length-1;
6+
7+
while(left<right){
8+
intsum=numbers[left]+numbers[right];
9+
if(sum>target){
10+
right--;
11+
}elseif(sum<target){
12+
left++;
13+
}else{
14+
returnnewint[]{left+1,right+1};
15+
}
16+
}
17+
18+
returnnewint[0];
19+
}
20+
}

‎csharp/20-Valid-Parentheses.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
publicclassSolution{
2+
publicboolIsValid(strings){
3+
varstack=newStack<char>();
4+
varpairs=newDictionary<char,char>(){
5+
[')']='(',
6+
[']']='[',
7+
['}']='{'
8+
};
9+
10+
foreach(charcins){
11+
if(!pairs.ContainsKey(c)){
12+
stack.Push(c);
13+
}elseif(stack.Count==0||stack.Pop()!=pairs[c]){
14+
returnfalse;
15+
}
16+
}
17+
18+
returnstack.Count==0;
19+
}
20+
}

‎csharp/242-Valid-Anagram.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
publicclassSolution{
2+
publicboolIsAnagram(strings,stringt){
3+
if(s.Length!=t.Length)returnfalse;
4+
if(s==t)returntrue;
5+
6+
Dictionary<char,int>sCounts=newDictionary<char,int>();
7+
Dictionary<char,int>tCounts=newDictionary<char,int>();
8+
9+
for(inti=0;i<s.Length;i++){
10+
sCounts[s[i]]=1+(sCounts.ContainsKey(s[i])?sCounts[s[i]]:0);
11+
tCounts[t[i]]=1+(tCounts.ContainsKey(t[i])?tCounts[t[i]]:0);
12+
}
13+
14+
foreach(charcinsCounts.Keys){
15+
inttCount=tCounts.ContainsKey(c)?tCounts[c]:0;
16+
if(sCounts[c]!=tCount)returnfalse;
17+
}
18+
returntrue;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
publicclassSolution{
2+
publicintLengthOfLongestSubstring(strings){
3+
intleftPointer=0,rightPointer=0,maxLength=0;
4+
HashSet<int>chars=newHashSet<int>();
5+
6+
while(rightPointer<s.Length){
7+
charcurrChar=s[rightPointer];
8+
if(chars.Contains(currChar)){// Move left pointer until all duplicate chars removed
9+
chars.Remove(s[leftPointer]);
10+
leftPointer++;
11+
}else{
12+
chars.Add(currChar);
13+
maxLength=Math.Max(maxLength,rightPointer-leftPointer+1);
14+
rightPointer++;
15+
}
16+
}
17+
returnmaxLength;
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
publicclassSolution{
2+
publicintCharacterReplacement(strings,intk){
3+
intleft=0,maxLength=0;
4+
intmostFrequentLetterCount=0;// Count of most frequent letter in the window
5+
int[]charCounts=newint[26];// Counts per letter
6+
7+
for(intright=0;right<s.Length;right++){
8+
charCounts[s[right]-'A']++;
9+
mostFrequentLetterCount=Math.Max(mostFrequentLetterCount,charCounts[s[right]-'A']);
10+
11+
intlettersToChange=(right-left+1)-mostFrequentLetterCount;
12+
if(lettersToChange>k){// Window is invalid, decrease char count and move left pointer
13+
charCounts[s[left]-'A']--;
14+
left++;
15+
}
16+
17+
maxLength=Math.Max(maxLength,(right-left+1));
18+
}
19+
returnmaxLength;
20+
}
21+
}

‎csharp/49-Group-Anagrams.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
publicclassSolution{
2+
publicIList<IList<string>>GroupAnagrams(string[]strs){
3+
vargroups=newDictionary<string,IList<string>>();
4+
5+
foreach(stringsinstrs){
6+
char[]hash=newchar[26];
7+
foreach(charcins){
8+
hash[c-'a']++;
9+
}
10+
11+
stringkey=newstring(hash);
12+
if(!groups.ContainsKey(key)){
13+
groups[key]=newList<string>();
14+
}
15+
groups[key].Add(s);
16+
}
17+
returngroups.Values.ToList();
18+
}
19+
}

‎csharp/704-Binary-Search.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
publicclassSolution{
2+
publicintSearch(int[]nums,inttarget){
3+
intleft=0,right=nums.Length-1;
4+
5+
while(left<=right){
6+
intmid=left+((right-left)/2);// (left + right) / 2 can lead to overflow
7+
if(nums[mid]>target){
8+
right=mid-1;
9+
}elseif(nums[mid]<target){
10+
left=mid+1;
11+
}else{// Found the value
12+
returnmid;
13+
}
14+
}
15+
return-1;
16+
}
17+
}

‎javascript/139-Word-Break.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
letwordBreak=function(s,wordDict){
2+
letdp=newArray(s.length+1);
3+
dp.fill(false);
4+
dp[s.length]=true;
5+
6+
letword='';
7+
for(leti=s.length-1;i>=0;i--){
8+
word=s[i]+word;
9+
10+
if(wordDict.includes(word)&&i+word.length<dp.length){
11+
dp[i]=dp[i+word.length];
12+
word='';
13+
}else{
14+
dp[i]=false;
15+
}
16+
}
17+
18+
returndp[0];
19+
};

‎javascript/252-Meeting-Rooms.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
*@param {number[]} intervals
3+
*@return {boolean}
4+
*/
5+
varcanAttendMeetings=function(intervals){
6+
intervals.sort((a,b)=>a[0]-b[0]);
7+
8+
for(leti=0;i<intervals.length-1;i++){
9+
if(intervals[i][1]>intervals[i+1][0]){
10+
returnfalse;
11+
}
12+
}
13+
returntrue;
14+
};

‎javascript/269-Alien-Dictionary.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
letalienOrder=function(words){
2+
letgraph={};
3+
4+
for(leti=0;i<words.length;i++){
5+
for(letj=0;j<words[i].length;j++){
6+
graph[words[i][j]]=newSet();
7+
}
8+
}
9+
10+
for(leti=0;i<words.length-1;i++){
11+
letword1=words[i];
12+
letword2=words[i+1];
13+
14+
if(word1.length>word2.length&&(word1+'').startsWith(word2)){
15+
return"";
16+
}
17+
18+
for(letj=0;j<Math.min(word1.length,word2.length);j++){
19+
letc1=word1[j];
20+
letc2=word2[j];
21+
22+
if(c1!==c2){
23+
graph[c1].add(c2);
24+
break;
25+
}
26+
}
27+
}
28+
29+
letvisited={};// 'false' = visited, 'true' = current path
30+
letres=[];
31+
32+
functiondfs(c){
33+
if(visited[c]){
34+
returnBoolean(visited[c]);
35+
}
36+
37+
visited[c]='true';
38+
for(letneiofgraph[c]){
39+
if(dfs(nei)){
40+
returntrue;
41+
}
42+
}
43+
44+
visited[c]='false';
45+
res.push(c);
46+
}
47+
48+
Object.keys(graph).forEach(c=>{
49+
if(dfs(c)){
50+
return'';
51+
}
52+
})
53+
54+
returnres.reverse().join('');
55+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
*@param {number[]} nums
3+
*@param {number} k
4+
*@return {number[]}
5+
*/
6+
vartopKFrequent=function(nums,k){
7+
letmap=newMap();
8+
letres=[];
9+
letbucket=Array.from({length:nums.length+1},()=>[]);// to create unique arrays
10+
11+
// storing frequency of numbers in a map
12+
for(letnofnums){
13+
map.set(n,(map.has(n) ?1+map.get(n) :1))
14+
}
15+
16+
// Poppulate the bucket with numbers in frequency
17+
// as the index of the bucket
18+
for(const[key,value]ofmap.entries()){
19+
bucket[value].push(key);
20+
}
21+
22+
for(leti=bucket.length-1;i>=0;i--){
23+
if(bucket[i].length>0){
24+
for(letnofbucket[i]){
25+
res.push(n);
26+
if(res.length===k)
27+
returnres;
28+
}
29+
}
30+
}
31+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp