LeetCode Tutorial
Asked In :
Dekh mere Bhai is question(Two Sum) ma tujha sirf itna karna hai ki tujha do INTEGER Given honge NUMS or TARGET tujha sirf un numbers a Index’s return karna hai jin ka sum TARGET ke equal ho bas.
or ha kisi bhi no ko do bar mat use karna , example ma dekh.
Example 1:
Input: nums = [1,8,13,15], target = 9
Output: [0,1] —————->kyuki index 0 ma 1 hai or 1 pe 8 =9;
to ab [1,8] ka sum =9 hai to ab tu [8,1] mat kardio samjha.
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Chal thik hai samaj gaya hoga ab code kr le
Sun mere Bhai Dhyan se suniye hm is question mein HashMap ka use karne Wale Hain kyunki hm HashMap ke andar kisi bhi value ko bahut asani se put kar sakte hain aur fir hm uska index get kr sakte hai.
Tu ab tu soch raha ho ki agar 2 number same hu th fir kya karnage ha to mere yar har value ka index to alag hi hoga na or HashMap same value store karta hai same key nhi .
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap map= new HashMap();
// ab map ma sari values put kr de loop laga ke
for(int i=0;i
Sun mere Bhai Dhyan se suniye hm is question mein HashMap ka use karne Wale Hain kyunki hm HashMap ke andar kisi bhi value ko bahut asani se put kar sakte hain aur fir hm uska index get kr sakte hai.
Tu ab tu soch raha ho ki agar 2 number same hu th fir kya karnage ha to mere yar har value ka index to alag hi hoga na or HashMap same value store karta hai same key nhi .
class Solution {
public:
vector twoSum(vector& nums, int target) {
unordered_map numToIndex;
for (int i = 0; i < nums.size(); ++i) {
if (const auto it = numToIndex.find(target - nums[i]);
it != numToIndex.cend())
return {it->second, i};
numToIndex[nums[i]] = i;
}
throw;
}
};
dekh bhai phele indice jo integer ko store krne ke kaam aata hai , ek ham for loop lagte hai jo enumerate jo interger ko track krne m kaam aata hai.
phir ham complement find out krege aur target se num ko ek ek kr subtract krege aur phir ham ek if loop chalege aur ek ek integer se target ko match krte apne goal tak puchege. aur last m final output nikal jayega.
aur es code Time-complexity O(n) hota hai.
space complexity bhi eska O(n) hota hai.
def two_sum(nums, target):
num_indices = {} # Dictionary to store the indices of numbers
for i, num in enumerate(nums):
complement = target - num # Calculate the complement needed to reach the target
# Check if the complement is in the dictionary
if complement in num_indices:
return [num_indices[complement], i] # Return the indices of the two numbers
# If not, add the current number and its index to the dictionary
num_indices[num] = i
# If no solution is found
return None
# Example usage:
nums = [1, 8, 13, 15]
target = 9
result = two_sum(nums, target)
print(result) # Output: [0, 1]
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap map= new HashMap();
// put elemenets in hashmap
for(int i=0;i
class Solution {
public int[] twoSum(int[] nums, int target) {
Map numToIndex = new HashMap<>();
for (int i = 0; i < nums.length; ++i) {
if (numToIndex.containsKey(target - nums[i]))
return new int[] {numToIndex.get(target - nums[i]), i};
numToIndex.put(nums[i], i);
}
throw new IllegalArgumentException();
}
}
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
numToIndex = {}
for i, num in enumerate(nums):
if target - num in numToIndex:
return numToIndex[target - num], i
numToIndex[num] = i