LeetCodeExercise

3 min read Page Views

面试问开发被拷打,开发能力严重不足,多刷一刷LC提升一下。

  1. Two Sum

https://leetcode.com/problems/two-sum/description/?envType=problem-list-v2&envId=array

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.

Example 1:
    Input: nums = [2,7,11,15], target = 9
    Output: [0,1]
    Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        result = []

        n = len(nums)
        result = []

        #for i in range(n): picks the first number index.
        #for j in range(i+1, n): picks the second number index, always after i.
        or i in range(n):
                    
            for j in range(i+1,n):
                if nums[i] + nums[j] == target:
                    return i,j
                        
#For each number x, you need another number need = target - x.
#Instead of scanning the rest of the array to find need, you ask a hash map instantly: “Have I seen need before?”

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        seen = {}  # value -> index

        for i, x in enumerate(nums):
            need = target - x
            if need in seen:
                return [seen[need], i]
            seen[x] = i

enumerate function

for i, x in enumerate(iterable):
    # i = index (0, 1, 2, ...)
    # x = value at that index

#Example
nums = [2, 7, 11, 15]

# Without enumerate (the old way):
for i in range(len(nums)):
    x = nums[i]
    print(i, x)

# With enumerate (cleaner):
for i, x in enumerate(nums):
    print(i, x)

# Output both cases:
# 0 2
# 1 7
# 2 11
# 3 15

  1. Add Two Numbers

https://leetcode.com/problems/add-two-numbers/description/

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:
    Input: l1 = [2,4,3], l2 = [5,6,4]
    Output: [7,0,8]
    Explanation: 342 + 465 = 807.
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(0)  # Create a dummy head node
        current = dummy
        carry = 0
        
        # Process both lists while at least one of them has nodes remaining
        while l1 or l2 or carry:
            # Get values (default to 0 if a list has ended)
            x = l1.val if l1 else 0
            y = l2.val if l2 else 0
            
            # handling it like basic add fuction
            # Calculate sum and carry
            total = x + y + carry
            
            carry = total // 10
            
            # Create new node with the sum digit
            current.next = ListNode(total % 10)
            current = current.next
            
            # Move to next nodes if available
            if l1:
                l1 = l1.next
            if l2:
                l2 = l2.next
        
        return dummy.next

  1. Longest Substring Without Repeating Characters
Last updated on 2026-03-24