- 博客(62)
- 收藏
- 关注
原创 937. Reorder Data in Log Files
You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.There are two types of logs:Letter-logs: All words (except the identifier) consist of lowercase English letters.Digit-logs: All words (
2022-03-10 12:10:07
368
原创 253. Meeting Rooms II
Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.Example 1:Input: intervals = [[0,30],[5,10],[15,20]]Output: 2Example 2:Input: intervals = [[7,10],[2,4]]Ou
2022-03-09 12:15:36
401
原创 1268. Search Suggestions System
You are given an array of strings products and a string searchWord.Design a system that suggests at most three product names from products after each character of searchWord is typed. Suggested products should have common prefix with searchWord. If there
2022-03-09 12:13:57
298
原创 210. Course Schedule II
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.For example, the
2022-03-09 12:11:37
262
原创 collections — Container datatypes
This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.namedtuple()factory function for creating tuple subclasses with named fieldsdequelist-like contain
2022-02-04 04:18:26
393
原创 亚麻-146. LRU Cache
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.Implement the LRUCache class:LRUCache(int capacity) Initialize the LRU cache with positive size capacity.int get(int key) Return the value of the key if the key ex
2022-01-22 10:57:32
145
原创 亚麻-1268. Search Suggestions System
You are given an array of strings products and a string searchWord.Design a system that suggests at most three product names from products after each character of searchWord is typed. Suggested products should have common prefix with searchWord. If there
2022-01-21 09:33:14
190
原创 1539. Kth Missing Positive Number
Given an array arr of positive integers sorted in a strictly increasing order, and an integer k.Find the kth positive integer that is missing from this array.Example 1:Input: arr = [2,3,4,7,11], k = 5Output: 9Explanation: The missing positive integers
2021-11-08 11:17:10
141
原创 235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p
2021-11-06 03:51:12
122
原创 100. Same Tree相同的树
Given the roots of two binary trees p and q, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.Example 1:Input: p = [1,2,3], q = [1,2,3]
2021-11-06 03:40:36
112
原创 387. First Unique Character in a String字符串中的第一个唯一字符
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.Example 1:Input: s = "leetcode"Output: 0Example 2:Input: s = "loveleetcode"Output: 2Example 3:Input: s = "aabb"Output: -1class
2021-11-06 03:31:32
116
原创 733. Flood Fill图像渲染
An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.You are also given three integers sr, sc, and newColor. You should perform a flood fill on the image starting from the pixel image[sr][sc].To
2021-11-06 02:54:11
126
原创 167. Two Sum II - Input Array Is Sorted
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 &l
2021-11-06 00:11:01
112
原创 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 1:Input: nums = [3,0,1]Output: 2Explanation: n = 3 since there are 3 numbers, so all numbers are in the r
2021-11-04 08:18:01
127
原创 21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.Example 1:Input: l1 = [1,2,4], l2 = [1,3,4]Output: [1,1,2,3,4,4]Example 2:Input: l1 = [], l2 = []Output: []
2021-11-04 07:56:04
240
原创 217. Contains Duplicate
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.Example 1:Input: nums = [1,2,3,1]Output: trueExample 2:Input: nums = [1,2,3,4]Output: falseExample 3:Input: n
2021-11-02 11:03:46
112
原创 1287. Element Appearing More Than 25% In Sorted Array
同169Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.Example 1:Input: arr = [1,2,2,6,6,6,6,7,10]Output: 6Example 2:Input: arr = [1,1]Output:
2021-11-02 10:38:15
116
原创 118. Pascal‘s Triangle
Given an integer numRows, return the first numRows of Pascal’s triangle.In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:Input: numRows = 5Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]图示:https://upload.wi
2021-10-29 10:30:05
240
原创 169. Majority Element
Given an array nums of size n, return the majority element.The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.Example 1:Input: nums = [3,2,3]Output: 3Example 2
2021-10-27 12:01:20
102
原创 872. Leaf-Similar Trees
Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).Two binary trees are considered leaf-similar if the
2021-10-26 05:46:57
144
原创 884. Uncommon Words from Two Sentences
A sentence is a string of single-space separated words where each word consists only of lowercase letters.A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.Given two sentences s1 and s2, retu
2021-10-26 05:04:16
116
原创 412. Fizz Buzz
Given an integer n, return a string array answer (1-indexed) where:answer[i] == “FizzBuzz” if i is divisible by 3 and 5.answer[i] == “Fizz” if i is divisible by 3.answer[i] == “Buzz” if i is divisible by 5.answer[i] == i if non of the above conditions
2021-10-26 04:43:24
162
原创 349. Intersection of Two Arrays
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.Example 1:Input: nums1 = [1,2,2,1], nums2 = [2,2]Output: [2]Example 2:Input: nums1 =
2021-10-26 04:41:16
115
原创 206. Reverse Linked List
Given the head of a singly linked list, reverse the list, and return the reversed list.class Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ prev = None while
2021-10-26 04:04:31
118
原创 965. Univalued Binary Tree
A binary tree is uni-valued if every node in the tree has the same value.Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.solution:class Solution(object): def __init__(self): self.res = se
2021-10-25 12:42:34
108
原创 1047. Remove All Adjacent Duplicates In String
You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.We repeatedly make duplicate removals on s until we no longer can.Return the final string after al
2021-10-25 08:02:51
95
原创 852. Peak Index in a Mountain Array
Let’s call an array arr a mountain if the following properties hold:arr.length >= 3There exists some i with 0 < i < arr.length - 1 such that:arr[0] < arr[1] < … arr[i-1] < arr[i]arr[i] > arr[i+1] > … > arr[arr.length - 1]Giv
2021-10-25 07:49:49
86
原创 977. Squares of a Sorted Array
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.Example 1:Input: nums = [-4,-1,0,3,10]Output: [0,1,9,16,100]Explanation: After squaring, the array becomes [16,1,0,
2021-10-23 10:40:39
651
原创 905. Sort Array By Parity
与283思路一致Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.Return any array that satisfies this condition.Example 1:Input: nums = [3,1,2,4]Output: [2,4,3,1]Explanation: The outputs [
2021-10-23 09:28:59
93
原创 557. Reverse Words in a String III
Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.Example 1:Input: s = “Let’s take LeetCode contest”Output: “s’teL ekat edoCteeL tsetnoc”Example 2:Input: s = “God
2021-10-23 09:07:23
110
原创 1859. Sorting the Sentence
Sorting the SentenceA sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.A sentence can be shuffled by appending the 1-indexed word position.
2021-10-23 08:51:40
430
原创 1603. Design Parking System
Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.Implement the ParkingSystem class:ParkingSystem(int big, int medium, int small) Initializes o
2021-10-23 08:28:45
180
原创 leetcode hot100------19. 删除链表的倒数第 N 个结点
如果我们要删除节点 y,我们需要知道节点 y 的前驱节点 x,并将 x 的指针指向 y 的后继节点但由于头节点不存在前驱节点,因此我们需要在删除头节点时进行特殊判断。但如果我们添加了哑节点,那么头节点的前驱节点就是哑节点本身,此时我们就只需要考虑通用的情况即可。两种方法:1.计算长度L,用L-n+1得到正方向这个位置,删掉/*** 获得本链表的长度* @return* @date 2016-9-28* @author shaobn*/public int getLength(){int
2021-08-23 12:18:30
198
原创 leetcode hot100------17.电话号码的字母组合
回溯(backtrack):穷举多维度数据的方法,可以想作是多维度的exhaustive search详尽的搜索.大意是:把多维度数据看作是一个多维向量(solution vector),然后运用递回依序递回穷举各个维度的值,制作出所有科恩个数据(solution space),并且在递回途中避免列举出不正确的数据。 backtrack([v1,...,vn]) { //[v1,...,vn]是多维度的向量 //制作出了一组数据,并检验这组数据正不正确 if([v1,...,vn] is we
2021-08-19 12:47:19
175
原创 leetcode hot100------15. 三数之和
方法一:排序 + 双指针「不重复」的本质是什么?我们保持三重循环的大框架不变,只需要保证:第二重循环枚举到的元素不小于当前第一重循环枚举到的元素;第三重循环枚举到的元素不小于当前第二重循环枚举到的元素这种方法的时间复杂度仍然为 O(N^3),毕竟我们还是没有跳出三重循环的大框架。然而它是很容易继续优化的,可以发现,如果我们固定了前两重循环枚举到的元素 a和 b,那么只有唯一的 c 满足 a+b+c=0。当第二重循环往后枚举一个元素 b’时,由于 b′>b,那么满足 a+b’+c’=0的 c’
2021-08-18 05:07:32
191
原创 leetcode hot100------11. 盛最多水的容器
方法一:双指针说明本题是一道经典的面试题,最优的做法是使用「双指针」。如果读者第一次看到这题,不一定能想出双指针的做法。分析我们先从题目中的示例开始,一步一步地解释双指针算法的过程。稍后再给出算法正确性的证明。题目中的示例为:[1, 8, 6, 2, 5, 4, 8, 3, 7]^ ^在初始时,左右指针分别指向数组的左右两端,它们可以容纳的水量为 min(1, 7) * 8 = 8此时我们需要移动一个指针。移动哪一个呢?直觉告诉我们
2021-07-23 15:35:49
413
原创 leetcode hot100------3. 无重复字符的最长子串
方法一:滑动窗口思路和算法我们先用一个例子考虑如何在较优的时间复杂度内通过本题。我们不妨以示例一中的字符串 \texttt{abcabcbb}abcabcbb 为例,找出从每一个字符开始的,不包含重复字符的最长子串,那么其中最长的那个字符串即为答案。对于示例一中的字符串,我们列举出这些结果,其中括号中表示选中的字符以及最长的字符串:以 \texttt{(a)bcabcbb}(a)bcabcbb 开始的最长字符串为 \texttt{(abc)abcbb}(abc)abcbb;以 \texttt{a(
2021-07-21 00:38:40
285
原创 leetcode hot100------338. 比特位计数
int func(int x){ int count = 0; while(x) { count++; x = x&(x-1); } return count;} 对于任意的x,转换成二进制后,是形如这样的数字:aa…aa10…00,从右向左数有任意多个0,直到遇见第一个1,字母a用来占位,代表1左边的任意数字。x-1转换成二进制后,是形如这样的数字:aa…aa01…11,从右向左数,原来的任意多个0都变成1,原来的
2021-07-21 00:12:03
167
原创 leetcode hot100------283. 移动零
swap(nums, left, right);Syntax:public static void swap(List list, int i, int j)Parameters: This method takes the following argument as a Parameterlist – The list in which to swap elements.i – the index of one element to be swapped.j – the index of th
2021-07-20 15:54:26
138
原创 leetcode hot100------234. 回文链表
方法一:将值复制到数组中后用双指针法一共为两个步骤:复制链表值到数组列表中。使用双指针法判断是否为回文。第一步,我们需要遍历链表将值复制到数组列表中。我们用 currentNode 指向当前节点。每次迭代向数组添加 currentNode.val,并更新 currentNode = currentNode.next,当 currentNode = null 时停止循环。执行第二步的最佳方法取决于你使用的语言。在 Python 中,很容易构造一个列表的反向副本,也很容易比较两个列表。而在其他语言中,
2021-07-20 15:12:05
231
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅