🤖המדריך להייטיקיסט המתחיל

Two Sum

קל
שאלה מראיונות עבודה ממאגר שאלות של LeetCode שאלה מספר 1 נשאל ב: Google, Meta, Amazon
Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`.

- Each input is guaranteed to have exactly one solution.
- You may not use the same element twice.
- You can return the answer in any order.

פתרון קוד

JavaScript
Python
function twoSum(nums, target) {
  const valueToIndex = new Map();
  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i];
    if (valueToIndex.has(complement)) {
      return [valueToIndex.get(complement), i];
    }
    valueToIndex.set(nums[i], i);
  }
  return [];
}

הסבר וידאו כיצד לפתור את השאלה

Book Cover

לעבור את ראיון העבודה הבא שלך בהצלחה

קורס דיגיטלי מקיף עם +25 שיעורים מעשיים, כשעתיים של וידאו, וליווי של מראיין בכיר.

התחל עכשיו ב-99 ₪ בלבד! 🚀