Tag: LeetCode

  • LeetCode: 121 Best Time to Buy and Sell Stock

    You are given an array prices where prices[i] is the price of a given stock on the i-th day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit…

  • LeetCode: 18 4Sum

    Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: 0 <= a, b, c, d < n a, b, c, and d are distinct. nums[a] + nums[b] + nums[c] + nums[d] == target Example: Input: nums = [1,…

  • LeetCode: 15 3Sum

    Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets. Example: Input: nums = [-1, 0, 1, 2,…

  • LeetCode: 454 4Sum II

    Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0. Example: Input: nums1 = [1, 2], nums2 = [-2,-1], nums3 = [-1, 2], nums4 = [0, 2]…

  • LeetCode: 704 Binary Search

    Given a sorted (in ascending order) integer array nums of n elements and a target value target, write a function to search target in nums. If target exists, then return its index, otherwise, return -1. Example: Input: nums = [-1, 0, 3, 5, 9, 12], target = 9 Output: 4…

  • LeetCode: 226 Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 问题 翻转一棵二叉树。 解决方案 1 递归方法 Approach 1 / 方法 1 This solution uses a recursive approach. The…

  • LeetCode: 110 Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: A binary tree in which the left and right subtrees of every node differ in height by no more than 1. Example: Input: root = [3,9,20,null,null,15,7] Output: true Input: root =…

  • LeetCode: 125 Valid Palindrome

    Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Example: Input: "A man, a plan, a canal: Panama" Output: true Input: "race a car" Output: false 问题 给定一个字符串 s,判断它是否是一个回文字符串,仅考虑字母数字字符并忽略大小写。 解决方案 1 双指针法 Approach 1 / 方法 1 使用双指针从字符串的两端向中间移动,忽略非字母数字字符,并比较字母数字字符是否相等。 Detailed Steps / 详细步骤…

  • LeetCode: 242. Valid Anagram

    Given two strings s and t, return true if t is an anagram of s, and false otherwise. Example: Input: s = "anagram", t = "nagaram" Output: true Input: s = "rat", t = "car" Output: false 问题 给定两个字符串 s 和 t,如果 t 是 s 的字母异位词,则返回 true,否则返回 false。 解决方案 1…

  • LeetCode: 1 Two Sum

    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…