这个题也是括号的匹配,但是不是让你判断是否匹配,而是让找出所有匹配的情况,与以前的题目并不一样,难度也不是一个等级的。看到题目的测试用例,我的第一反应是循环,因为我对循环比较敏感吧,所以我用循环写了一下,但是我发现,n=2的时候很简单就写出了代码,但是n=3的时候就有点麻烦了,当n=4的时候--------我感觉大脑要炸了... 这说明了这个思路不行,必须换种思路去解题,所以我给自己一个n值-------- n=100 !好吧,我...
Partition ListGiven a linked list and a value x, partition it such that all
nodes less than x come before nodes greater than or equal
to x.You should preserve the original relative order of the nodes in each of the
two partitions.For
example,Given 1->4->3->2->5->2 and x =
3,return 1->2->2->4->3->5. 维护两个链表,一个记录比x小的(头newl, 尾left),一个记录不小于x的(头newr,尾right)。扫描一遍链...
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.Note:A subtree must include all of its descendants. Follow up:Can you figure out ways to solve it with O(n) time complexity?此题的意思是找出一棵二叉树里的最大二叉搜索树,即二叉树内的一棵最大的满足二叉搜索树性质的子树。二叉搜索树的定义是:二叉查找树(Bina...
leetcode 2 – Add Two Numbers 题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8题意: 两个链表,每个节点存有一个非负值且是为0-9,把两个链表相加且包含进位,看上面Input的4和6节点对...
这个题目没有动手实践,仅仅是想了个思路。结果一看讨论区的代码瞬间感觉,我想的太复杂了。ps:有点想不明确,既然是要移除元素。为何不留下一个不含删除元素的纯净数组。题目:Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length.
讨论区解决的方法:public class Solution ...
题目:删除有序数组中的重复项(简单)
一、题目描述
给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。
不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
示例:
输入:nums = [0,0,1,1,1,2,2,3,3,4]输出:5, nums = [0,1,2,3,4]解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不...
给定一个头结点为 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。 示例 1:输入:[1,2,3,4,5]输出:此列表中的结点 3 (序列化形式:[3,4,5])返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。注意,我们返回了一个 ListNode 类型的对象 ans,这样:ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.示例 2:输入:[1,2,3,4,5,6]输出:此...
LeetCode: Linked List CycleGiven a linked list, determine if it has a cycle in it. Follow up:
Can you solve it without using extra space?
地址:https://oj.leetcode.com/problems/linked-list-cycle/算法:将链表的指针倒过来,如果最后循环终点是原来链表的头节点,说明链表有环,如果循环的终点不是链表的头节点,那么说明链表没有环(只有一个节点的链表要单独判断)。例如,有如下链表:1->2->3->4->5->6->4(有环的链...
题目链接:first-missing-positive/*** Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space.**/
public class FirstMissingPositive {// 156 / 156 test cases passed.
// Status: Accepted
// Runtime: 235 ms
// Submitted: 0 minutes agopublic int firstMissingPositive...
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
采用两种方式都可以实现DFS和BFS...
给定一个链表,判断链表中是否有环。为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。 示例 1:输入:head = [3,2,0,-4], pos = 1输出:true解释:链表中有一个环,其尾部连接到第二个节点。示例 2:输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。示例 3:输入:head = [1], pos = -1
输出:false
解...
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1],and [2,1,1].
原题链接:https://oj.leetcode.com/problems/permutations-ii/
题目:给定一组包含重复元素的集合,返回所有可能的独立排列。思路:此题只要将之前的全排列那题稍作修改即可,首先将数组排序,这样,相同的元素就是相邻的了,...
Given an unsorted array of integers, find the length of the longest
consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The
longest consecutive elements sequence is [1, 2, 3, 4]. Return
its length: 4.Your algorithm should run in O(n) complexity. [解题思路]数组,O(n)---->使用hash,空间换时间对于当前数N,我们检查N-1, N-2, N-3....N-K是否在hashmap中对于当前数N,我们检查N+1, N+2, ...
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。示例 1:输入: [1,3,5,6], 5输出: 2示例 2:输入: [1,3,5,6], 2输出: 1示例 3:输入: [1,3,5,6], 7输出: 4示例 4:输入: [1,3,5,6], 0输出: 0class Solution: def searchInsert(self, nums, target): """ :type nums: List[int] :type target: ...
Question830. Positions of Large GroupsSolution题目大意:字符串按连续相同字符分组,超过3个就返回首字符和尾字符思路 :举例abcdddeeeeaabbbcdend start end-start
a 0 0
b 1 0,1 1-0=1
c 2 1,2 2-1=1
d 3 2,3 3-2=1
d 4 3
d 5 3
e 6 3,6 6-3=3 3,5
e 7 6
e 8 6
e 9 6
a 10 6,10 10-6=4 6,9
a 11 10
b 12 10,12 12-10=2
b 13 12
b 14 12
c 15 12,15 15-1...