forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into nc-refactor
- Loading branch information
Showing
4 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Solution { | ||
public: | ||
bool checkPossibility(vector<int>& nums) { | ||
bool changed = false; | ||
|
||
for(int i = 0; i < nums.size() - 1; i++){ | ||
if(nums[i] <= nums[i+1]){ | ||
continue; | ||
} | ||
if(changed){ | ||
return false; | ||
} | ||
|
||
if(i == 0 || nums[i+1] >= nums[i-1]){ | ||
nums[i] = nums[i+1]; | ||
} | ||
else{ | ||
nums[i+1] = nums[i]; | ||
} | ||
changed = true; | ||
} | ||
|
||
return true; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Solution { | ||
fun findAnagrams(s: String, p: String): List<Int> { | ||
|
||
val pCount = IntArray(26) | ||
val res = ArrayList<Int>() | ||
|
||
for(c in p) | ||
pCount[c - 'a']++ | ||
|
||
var start = 0 | ||
var end = 0 | ||
|
||
while(end < s.length){ | ||
// increase the window | ||
if(pCount[s[end] - 'a'] > 0){ | ||
pCount[s[end++] - 'a']-- | ||
if(end-start == p.length) | ||
res.add(start) | ||
// window size 0? step to next | ||
}else if(start == end){ | ||
start++ | ||
end++ | ||
//decrease the window | ||
}else{ | ||
pCount[s[start++] - 'a']++ | ||
} | ||
} | ||
|
||
return res | ||
} | ||
} |