111 lines
4.6 KiB
YAML
111 lines
4.6 KiB
YAML
title: Add Two Integers
|
|
slug: add-two-integers
|
|
difficulty: easy
|
|
leetcode_id: 2235
|
|
leetcode_url: https://leetcode.com/problems/add-two-integers/
|
|
categories:
|
|
- math
|
|
patterns: []
|
|
|
|
description: |
|
|
Given two integers `num1` and `num2`, return *the **sum** of the two integers*.
|
|
|
|
constraints: |
|
|
- `-100 <= num1, num2 <= 100`
|
|
|
|
examples:
|
|
- input: "num1 = 12, num2 = 5"
|
|
output: "17"
|
|
explanation: "num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned."
|
|
- input: "num1 = -10, num2 = 4"
|
|
output: "-6"
|
|
explanation: "num1 + num2 = -6, so -6 is returned."
|
|
|
|
explanation:
|
|
intuition: |
|
|
This problem is a fundamental introduction to arithmetic operations in programming.
|
|
|
|
While adding two numbers might seem trivial, it establishes an important foundation: understanding how programming languages handle basic mathematical operations, including **negative numbers** and **integer overflow** considerations.
|
|
|
|
Think of it as verifying that you understand the most basic building block of computation. Every complex algorithm ultimately breaks down into simple operations like addition.
|
|
|
|
approach: |
|
|
**Step 1: Return the sum**
|
|
|
|
- Simply use the `+` operator to add `num1` and `num2`
|
|
- Return the result directly
|
|
|
|
|
|
|
|
This problem requires no special data structures or algorithms. The built-in addition operator handles all cases including:
|
|
- Two positive numbers
|
|
- Two negative numbers
|
|
- One positive and one negative number
|
|
- Zero as either operand
|
|
|
|
common_pitfalls:
|
|
- title: Overthinking the Problem
|
|
description: |
|
|
This problem is intentionally simple. Some developers might overthink it, looking for hidden complexity or edge cases that don't exist.
|
|
|
|
The constraints (`-100 <= num1, num2 <= 100`) ensure that the sum will always fit within standard integer bounds, so overflow is not a concern here.
|
|
wrong_approach: "Complex bit manipulation or handling overflow"
|
|
correct_approach: "Simple addition with the + operator"
|
|
|
|
- title: Forgetting Negative Numbers
|
|
description: |
|
|
While Python handles negative numbers seamlessly, it's worth understanding that the `+` operator works correctly with negative operands.
|
|
|
|
For example, `num1 = -10` and `num2 = 4` correctly produces `-6` because `-10 + 4 = -6`.
|
|
wrong_approach: "Assuming inputs are always positive"
|
|
correct_approach: "Trust the + operator to handle all integer cases"
|
|
|
|
key_takeaways:
|
|
- "**Foundation for complexity**: Even complex algorithms are built from simple operations like addition"
|
|
- "**Operator behaviour**: The `+` operator correctly handles positive, negative, and zero values"
|
|
- "**Constraint awareness**: Always check constraints — here, overflow is not a concern due to small input bounds"
|
|
- "**Simplicity is valid**: Sometimes the simplest solution is the correct one; don't overcomplicate"
|
|
|
|
time_complexity: "O(1). Addition is a constant-time operation regardless of the values."
|
|
space_complexity: "O(1). No additional data structures are used."
|
|
|
|
solutions:
|
|
- approach_name: Direct Addition
|
|
is_optimal: true
|
|
code: |
|
|
def sum(num1: int, num2: int) -> int:
|
|
# Simply return the sum of the two integers
|
|
return num1 + num2
|
|
explanation: |
|
|
**Time Complexity:** O(1) — Single arithmetic operation.
|
|
|
|
**Space Complexity:** O(1) — No additional memory used.
|
|
|
|
This solution directly returns the sum using Python's built-in addition operator. It handles all cases including negative numbers and zero.
|
|
|
|
- approach_name: Bit Manipulation (Without + Operator)
|
|
is_optimal: false
|
|
code: |
|
|
def sum(num1: int, num2: int) -> int:
|
|
# Handle negative numbers with 32-bit masking
|
|
MASK = 0xFFFFFFFF
|
|
MAX_INT = 0x7FFFFFFF
|
|
|
|
while num2 != 0:
|
|
# XOR gives sum without carry
|
|
temp = (num1 ^ num2) & MASK
|
|
# AND shifted left gives carry
|
|
num2 = ((num1 & num2) << 1) & MASK
|
|
num1 = temp
|
|
|
|
# Handle negative results
|
|
return num1 if num1 <= MAX_INT else ~(num1 ^ MASK)
|
|
explanation: |
|
|
**Time Complexity:** O(1) — Maximum 32 iterations for 32-bit integers.
|
|
|
|
**Space Complexity:** O(1) — Only uses a few variables.
|
|
|
|
This approach adds two numbers without using the `+` operator by simulating binary addition. XOR computes the sum without carry, while AND followed by left shift computes the carry. We repeat until there's no carry left.
|
|
|
|
This is included for educational purposes — it demonstrates how addition works at the bit level. In practice, the direct addition approach is preferred.
|