classSolution{public:
vector<int>twoSum(vector<int>& numbers,int target){int l =0, r = numbers.size()-1, sum;while(l < r){
sum = numbers[l]+ numbers[r];if(sum == target)break;if(sum < target)++l;else--r;}return vector<int>{l +1, r +1};}};
Python
classSolution:
def twoSum(self, numbers: List[int], target:int)-> List[int]:
l = sum =0
r =len(numbers)-1while l < r:
sum = numbers[l]+ numbers[r]if sum == target:breakif sum < target:
l +=1else:
r -=1return[l+1,r+1]
#Time:O(n + m)-> Total length of num1 list#Space:O(1)classSolution:
def merge(self, nums1: List[int], m:int, nums2: List[int], n:int)-> None:"""
Do notreturn anything, modify nums1 in-place instead."""
length = m + n -1 #Total length of num1 list
m = m -1
n = n -1for i in range(length,-1,-1):if n <0: #Edge case when list2 is null or n -=1 goes to -1breakif nums1[m]> nums2[n]and m >=0: #Edge case when m -=1 goes to -1
nums1[i]= nums1[m]
m -=1else:
nums1[i]= nums2[n]
n -=1
classSolution:
def merge(self, nums1: List[int], m:int, nums2: List[int], n:int)-> None:"""
Do notreturn anything, modify nums1 in-place instead."""
#removezeros from nums1for value in range(n):if0 in nums1:
nums1.remove(0)#appendnums2 items to the nums1 listfor value in nums2:
nums1.append(value)#sortin low to high order/non decreasing order
nums1 = nums1.sort()
classSolution:
def detectCycle(self, head: ListNode)-> ListNode:
slow = head
fast = head
ifnot head:return None
ifnot head.next:return None
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow:breakif fast == slow:while fast != head:
fast = fast.next
head = head.next
return head
else:return None
classSolution{public:
string minWindow(string s, string t){
vector<int>chars(128,0);
vector<bool>flag(128,false);for(int i =0; i < t.size();++i){
flag[t[i]]=true;++chars[t[i]];}int cnt =0, l =0, min_l =0, min_size = s.size()+1;for(int r =0; r < s.size();++r){if(flag[s[r]]){if(--chars[s[r]]>=0){++cnt;}while(cnt == t.size()){if(r - l +1< min_size){
min_l = l;
min_size = r - l +1;}if(flag[s[l]]&&++chars[s[l]]>0){--cnt;}++l;}}}return min_size > s.size()?"": s.substr(min_l, min_size);}};
Python
classSolution:defminWindow(self,s:str,t:str)->str:missing_chars=len(t) # howmanycharacterswedefinitelyneedintheslidingwindowcounter={k:0for k in t} # counter to keep track of count of chars of s in the current window
orig_counter =Counter(t) # original counter of t to account for duplicate chars in t
l =0 # left pointer for sliding window
out =[float('-inf'),float('inf')] # tuple to keep the shortest substring range in tow
#keepincrementing r in a for loopfor r in range(len(s)):#ifthe current char is in t#weincrement its count in our sliding#windowif s[r] in counter:#onlydecrement missing_chars IF the count of this#charis less than what we need i.e. count in tif counter[s[r]]< orig_counter[s[r]]: missing_chars -=1
counter[s[r]]+=1#If at any point we come across#missingchars as 0, it means we have#avalid substring, store it's range#inthe out tuplewhile missing_chars <=0:if(r-l)<(out[1]-out[0]): out =[l, r]#we're going to update our#counterifthischar is present#intif s[l] in counter:
counter[s[l]]-=1#sameas before, only increment missing_chars#IF the current count of char(after decrement)#isstill less than the original count i.e.#thecurrent sliding window now does not have #therequired char count forthis character and we're#missinga characterif counter[s[l]]< orig_counter[s[l]]: missing_chars +=1#weare going to move our l#regardlessuntil we are missing#acharacter again
l +=1#returnthe string ranged bw the out tuple#ifthere is no string and out remains [-inf, inf]#returnan empty stringreturn s[out[0]:out[1]+1]if out[1]-out[0]<len(s)else""
ALgorithmTwo Sum II - Input Array Is SortedC++class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { int l = 0, r = numbers.size() - 1, sum; while (l < r) { sum = numbers[l] + n