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
class Solution: | |
def findAnagrams(self, s: str, p: str) -> List[int]: | |
result = [] | |
if len(s) < len(p): | |
return result | |
s_freq = [0 for _ in range(26)] | |
p_freq = [0 for _ in range(26)] | |
for i in range(len(p)): |
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
class Solution { | |
public int strStr(String haystack, String needle) { | |
// Check edge cases and if the needle is greater than the haystack | |
if (haystack == null || needle == null || (needle.length() > haystack.length())) { | |
return -1; | |
} | |
// Check if the needle is empty | |
if (needle.length() == 0) { | |
return 0; | |
} |
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
class Solution { | |
public int[] twoSum(int[] nums, int target) { | |
Map<Integer, Integer> map = new HashMap<>(); | |
int a = Integer.MIN_VALUE; | |
int b = Integer.MIN_VALUE; | |
for (int i = 0; i < nums.length; i++) { | |
if (map.containsKey(target - nums[i])) { | |
a = i; | |
b = map.get(target - nums[i]); | |
break; |
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
class Solution { | |
public int[][] merge(int[][] intervals) { | |
Arrays.sort(intervals, (a, b) -> a[0] - b[0]); | |
LinkedList<int[]> result = new LinkedList<>(); | |
for (int[] interval : intervals) { | |
if (result.isEmpty() || result.getLast()[1] < interval[0]) { | |
result.add(interval); | |
} else { | |
result.getLast()[1] = Math.max(result.getLast()[1], interval[1]); | |
} |
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
class Solution { | |
public List<List<Integer>> threeSum(int[] nums) { | |
Arrays.sort(nums); | |
List<List<Integer>> result = new ArrayList<>(); | |
for (int i = 0; i < nums.length; i++) { | |
if (i == 0 || nums[i] != nums[i-1]) { | |
twoSum(nums, result, i); | |
} | |
} | |
return result; |
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
class Solution { | |
public int maxProfit(int[] prices) { | |
int min_price = Integer.MAX_VALUE; | |
int max_profit = Integer.MIN_VALUE; | |
for (int i = 0; i < prices.length; i++) { | |
min_price = Math.min(min_price, prices[i]); | |
max_profit = Math.max(max_profit, prices[i] - min_price); | |
} | |
return max_profit; | |
} |
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
class Solution { | |
public int maxArea(int[] height) { | |
int result = 0; | |
int left = 0; | |
int right = height.length - 1; | |
while (left < right) { | |
result = Math.max(result, Math.min(height[left], height[right]) * (right - left)); | |
if (height[left] < height[right]) { | |
left++; | |
} else { |
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
class Solution { | |
public int maxProduct(int[] nums) { | |
if (nums == null || nums.length == 0) { | |
return 0; | |
} | |
int max = nums[0]; | |
int min = nums[0]; | |
int result = nums[0]; | |
for (int i = 1; i < nums.length; i++) { | |
int current = nums[i]; |
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
class Solution { | |
public int maxSubArray(int[] nums) { | |
if (nums == null || nums.length == 0) { | |
return 0; | |
} | |
int sum = 0; | |
int result = Integer.MIN_VALUE; | |
for (int i = 0; i < nums.length; i++) { | |
sum += nums[i]; | |
result = Math.max(result, sum); |
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
class Solution { | |
public boolean containsDuplicate(int[] nums) { | |
Set<Integer> set = new HashSet<>(); | |
for (int num : nums) { | |
if (set.contains(num)) { | |
return true; | |
} | |
set.add(num); | |
} | |
return false; |