Tag: LeetCode

  • LeetCode: 162 Find Peak Element

    A peak element is an element that is strictly greater than its neighbors. Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks. You may imagine that nums[-1] = nums[n] = -∞.…

  • LeetCode: 371 Sum of Two Integers

    Given two integers a and b, return the sum of the two integers without using the operators + and -. Example: Input: a = 1, b = 2 Output: 3 Example: Input: a = 2, b = 3 Output: 5 问题 给定两个整数 a 和 b,在不使用 + 和 – 运算符的情况下,返回两个整数的和。 解决方案…

  • LeetCode: 268 Missing Number

    Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Example: Input: nums = [3,0,1] Output: 2 Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3].…

  • LeetCode: 191 Number of 1 Bits

    Here’s a detailed explanation and solution for LeetCode 191: Number of 1 Bits, with explanations provided in both English and Chinese, line by line. Problem Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight). Example: Input:…

  • LeetCode: 572 Subtree of Another Tree

    Given the roots of two binary trees root and subRoot, write a function to determine if subRoot is a subtree of root. A subtree of a binary tree T is a tree that consists of a node in T and all of this node’s descendants. The tree T could also…

  • LeetCode: 141 Linked List Cycle

    Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to…

  • LeetCode 101: 21 Merge Two Sorted Lists

    You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one-sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Example: Input: list1 = [1,2,4], list2…

  • LeetCode: 206 Reverse Linked List

    Given the head of a singly linked list, reverse the list, and return the reversed list. Example: Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] Example: Input: head = [1,2] Output: [2,1] Example: Input: head = [] Output: [] 问题 给定一个单链表的头节点,将链表反转,并返回反转后的链表。 解决方案 1 迭代法 Approach 1 / 方法 1 This solution uses…

  • LeetCode: 20 Valid Parentheses

    Given a string s containing just the characters (, ), {, }, [ and ], determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Example: Input:…

  • LeetCode: 238 Product of Array Except Self

    Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in…