
LeetCode
LeetCode
IT•轩辕
多言数穷,不如守中。十语九中,未必称奇;一语未中,愆尤骈集。
展开
-
【LeetCode 0006】【字符/几何】Z字形变换
The string is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)And then read line by line: Write the code that will take a string and make this conversion given a原创 2024-07-29 10:00:00 · 199 阅读 · 0 评论 -
【LeetCode 0005】【动态规划】最长回文子串
Given a string , return the longest palindromic substring in .Example 1:Example 2:Constraints:原创 2024-07-28 22:43:01 · 224 阅读 · 0 评论 -
【LeetCode 0004】【数组/分治】寻找两个正序数组的中位数
Given two sorted arrays and of size and respectively, return the median of the two sorted arrays.The overall run time complexity should be .Example 1:Example 2:Constraints:原创 2024-07-25 08:00:00 · 239 阅读 · 0 评论 -
【LeetCode 0003】【滑动窗口】无重复字符的最长子串
Given a string `s`, find the length of the **longest** **substring** without repeating characters.原创 2024-07-24 23:34:45 · 466 阅读 · 1 评论 -
【LeetCode 0002】【链表】两数相加
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.原创 2024-07-24 23:31:12 · 402 阅读 · 0 评论 -
【LeetCode 0037】【回溯/剪枝】解数独
Write a program to solve a Sudoku puzzle by filling the empty cells.A sudoku solution must satisfy all of the following rules:The character indicates empty cells.Example 1:Constraints:原创 2024-07-22 00:03:16 · 357 阅读 · 0 评论 -
【LeetCode 0338】【位运算/递归式】求比特位
Given an integer , return *an array of length such that for each (), is the number of ’s in the binary representation of *.Example 1:Example 2:Constraints:Follow up:原创 2024-07-21 08:00:00 · 695 阅读 · 1 评论 -
【LeetCode 0231】【位运算】2的N次方
Given an integer , return if it is a power of two. Otherwise, return .An integer is a power of two, if there exists an integer such that .Example 1:Example 2:Example 3:Constraints:Follow up: Could you solve it without loops/recursion?原创 2024-07-20 23:38:28 · 421 阅读 · 1 评论 -
【LeetCode 0191】【位运算】求1的个数
Write a function that takes the binary representation of a positive integer and returns the number of set bits it has (also known as the Hamming weight).Example 1:Output: 3Explanation:The input binary string 1011 has a total of three set bits.Example 2:Out原创 2024-07-19 09:34:10 · 425 阅读 · 0 评论 -
【LeetCode 0069】【二分查找】求平方根
Given a non-negative integer , return the square root of rounded down to the nearest integer. The returned integer should be non-negative as well.You must not use any built-in exponent function or operator.Example 1:Example 2:Constraints:原创 2024-07-17 23:43:01 · 351 阅读 · 0 评论 -
【LeetCode 0051】【剪枝】N皇后
The n-queens puzzle is the problem of placing queens on an chessboard such that no two queens attack each other.Given an integer , return all distinct solutions to the n-queens puzzle. You may return the answer in any order.Each solution contains a disti原创 2024-07-15 23:53:32 · 724 阅读 · 0 评论 -
【LeetCode 0022】【DSF】生成括号
Given `n` pairs of parentheses, write a function to *generate all combinations of well-formed parentheses*.原创 2024-07-14 18:22:22 · 212 阅读 · 0 评论 -
【LeetCode 0111】【递归/分治】二叉树的最小深度
0, 10^5]原创 2024-07-13 14:21:45 · 360 阅读 · 2 评论 -
【LeetCode 0104】【递归/分治】二叉树的最大深度
Given the of a binary tree, return its maximum depth.A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Example 1:Example 2:Constraints:原创 2024-07-13 14:16:52 · 410 阅读 · 2 评论 -
【LeetCode 0102】【BSF/DSF】二叉树的层级遍历
Given the of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).Example 1:Example 2:Example 3:Constraints:原创 2024-07-12 23:47:56 · 291 阅读 · 1 评论 -
【LeetCode 0122】【DSF/DP/Greedy】买卖股票的最佳时机2
You are given an integer array where is the price of a given stock on the day.On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the原创 2024-07-11 23:06:54 · 335 阅读 · 0 评论 -
【LeetCode 0169】【摩尔投票算法】主元素
Given an array of size , return the majority element.The majority element is the element that appears more than times. You may assume that the majority element always exists in the array.Example 1:Example 2:Constraints:Follow-up: Could you solve the pr原创 2024-07-11 22:44:33 · 691 阅读 · 0 评论 -
【LeetCode 0236】【递归】求二叉树的最近公共祖先
qpq。原创 2024-07-10 23:42:43 · 644 阅读 · 0 评论 -
【LeetCode 0235】【递归】求二叉查找树的最近公共祖先
qpq。原创 2024-07-10 23:27:27 · 585 阅读 · 0 评论 -
【LeetCode 0050】【分治/递归】求x的n次方
Implement pow(x, n), which calculates raised to the power (i.e., ).Example 1:Example 2:Example 3:Constraints:原创 2024-07-09 23:58:16 · 371 阅读 · 0 评论 -
【LeetCode 0088】 【数组/双指针】合并两个有序数组
You are given two integer arrays and , sorted in non-decreasing order, and two integers and , representing the number of elements in and respectively.Merge and into a single array sorted in non-decreasing order.The final sorted array should not be re原创 2024-07-09 23:43:09 · 266 阅读 · 0 评论 -
【LeetCode 0070】【递归/动态规划】爬楼梯
n1or2。原创 2024-07-08 23:28:42 · 236 阅读 · 1 评论 -
【LeetCode 0098】【BST】验证有效的二叉查找树
Given the of a binary tree, determine if it is a valid binary search tree (BST).A valid BST is defined as follows:Example 1:Example 2:Constraints:原创 2024-07-08 09:30:00 · 436 阅读 · 0 评论 -
【LeetCode 0239】【大堆/双端队列】滑动窗口中的最大值
You are given an array of integers , there is a sliding window of size which is moving from the very left of the array to the very right. You can only see the numbers in the window. Each time the sliding window moves right by one position.Return the max原创 2024-07-07 18:10:24 · 212 阅读 · 0 评论 -
【LeetCode 0141】【链表】【双指针之快慢指针】判断给定单链表是否存在环
Given , 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 pointer. Internally, is used to denote the index原创 2024-07-07 13:51:29 · 619 阅读 · 0 评论 -
【LeetCode 0024】【链表】交换单链表相邻两个节点
0, 100]原创 2024-07-07 13:39:15 · 431 阅读 · 0 评论 -
【LeetCode 0206】【链表】反转单链表
Given the of a singly linked list, reverse the list, and return the reversed list.Example 1:Example 2:Example 3:Constraints:Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?原创 2024-07-06 17:24:12 · 241 阅读 · 0 评论 -
【LeetCode 0242】【Map/排序】有效的异位词
Given two strings and , return if is an anagram of , and otherwise.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.Example 1:Example 2:Constraints:F原创 2024-07-05 23:23:19 · 188 阅读 · 0 评论 -
【LeetCode 0146】【设计】【双向循环链表】实现LRU缓存
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.Implement the class:The functions and must each run in average time complexity.Example 1:Constraints:原创 2024-07-04 20:31:48 · 449 阅读 · 0 评论 -
【LeetCode 0703】【设计】【优先队列】返回数据流中第K大的元素
Design a class to find the largest element in a stream. Note that it is the largest element in the sorted order, not the distinct element.Implement class:Example 1:Constraints:原创 2024-07-03 23:56:38 · 223 阅读 · 0 评论 -
【LeetCode 0232】【设计】用FILO栈实现FIFO队列
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (, , , and ).Implement the class:* Pushes element x to the back of the queue.* Removes the element from the front原创 2024-07-03 20:53:59 · 596 阅读 · 0 评论 -
【LeetCode 0225】【设计】用FIFO队列实现FILO栈
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (, , , and ).Implement the class:Notes:Example 1:Constraints:Follow-up: Can you implement the stack using only one原创 2024-07-02 23:22:20 · 779 阅读 · 0 评论 -
【LeetCode 0020】【栈】有效的括号
Given a string containing just the characters , , , , and , determine if the input string is valid.An input string is valid if:Example 1:Example 2:Example 3:Constraints:* * consists of parentheses only .原创 2024-07-01 23:59:53 · 278 阅读 · 0 评论 -
【LeetCode 0151】【字符串】反转字符串中的单词
【代码】【LeetCode 0151】【字符串】反转字符串中的单词。原创 2023-12-04 21:30:39 · 251 阅读 · 0 评论 -
【LeetCode 0028】【字符串】实现strStr
【代码】【LeetCode 0028】【字符串】实现strStr。原创 2023-12-03 23:54:49 · 111 阅读 · 0 评论 -
【LeetCode 0125】【双指针】有效回文
【代码】【LeetCode 0125】【双指针】有效回文。原创 2023-12-02 09:00:00 · 133 阅读 · 0 评论 -
【LeetCode 0170】【哈希】两数之和(3) 数据结构设计
【代码】【LeetCode 0167】【数组】两数之和(3) 数据结构设计。原创 2023-12-01 09:15:00 · 148 阅读 · 0 评论 -
【LeetCode 0167】【数组】两数之和(2)
【代码】【LeetCode 0167】【数组】两数之和(2)原创 2023-11-30 23:53:59 · 127 阅读 · 0 评论 -
【LeetCode 0001】【数组】两数之和
Given an array of integers and an integer , return indices of the two numbers such that they add up to .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.Examp原创 2023-11-29 23:45:51 · 400 阅读 · 0 评论 -
【LeetCode 0026】【数组】 删除有序数组中的重复项
【LeetCode 0026】【数组】 删除有序数组中的重复项数组。转载 2023-11-08 09:30:00 · 73 阅读 · 0 评论