随笔分类 - LeetCode
摘要:题目如下:(题目链接)Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a ...
阅读全文
摘要:题目如下:(题目链接)Sort a linked list inO(nlogn) time using constant space complexity.在上一题中使用了插入排序,时间复杂度为O(n^2)。nlogn的排序有快速排序、归并排序、堆排序。双向链表用快排比较适合,堆排序也可以用于链表,...
阅读全文
摘要:题目链接Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime comple...
阅读全文
摘要:题目链接 链表的插入排序Sort a linked list using insertion sort.建议:为了操作方便,添加一个额外的头结点。代码如下: 本文地址 1 /** 2 * Def...
阅读全文
摘要:题目链接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 long...
阅读全文
摘要:题目链接Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which...
阅读全文
摘要:题目如下: (题目链接)Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that surro...
阅读全文
摘要:LeetCode:Palindrome Partitioning题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分)Given a strings, partitionssuch that every substring of the partition is a palindrome....
阅读全文
摘要:题目如下:实现克隆图的算法 题目链接Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors.OJ's undirected graph serialization:Node...
阅读全文
摘要:题目大意:设计一个用于LRU cache算法的数据结构。 题目链接。关于LRU的基本知识可参考here分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::list)和哈希表(std::unordered_map)作为cache的数据结构,因为:双向链表插入删...
阅读全文
摘要:题目(题目链接):Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values...
阅读全文
摘要:题目:非递归实现二叉树的后序遍历。题目链接算法1:非递归使用栈。首先把根节点压栈,然后循环如下操作:用一个变量来记录上次访问的节点,如果当前栈顶元素左右儿子都为空 或者 上次访问的节点非空且等于栈顶节点的左儿子或右儿子,则直接访问;否则把栈顶元素的右节点和左节点依次压栈。代码如下: 1 /** 2 ...
阅读全文
摘要:题目:非递归实现二叉树的前序遍历。题目链接算法1:使用栈的非递归遍历。先用根节点初始化栈,然后循环如下操作:访问栈顶节点,先后把栈顶节点右节点和左节点压栈(次序不能反,先右节点,后左节点),代码如下: 1 /** 2 * Definition for binary tree 3 * struct...
阅读全文
摘要:题目:判断单链表是否有环,如果有则找出环的起点。题目链接这是另一道题的扩展,利用快慢指针判断出是否有环后,还需要找出环的起点,分析如下:设链表长度为len(链表中非空next指针的个数,下面所说的长度均为非空next指针的个数),链表head到环的起点长度为a,环起点到快慢指针相遇点的长度为b,环的...
阅读全文
摘要:题目:判断单链表是否有环,要求O(1)空间复杂度。题目链接利用快慢指针,开始两个指针均指向链表head,然后循环移动,慢指针移动1步,快指针则移动2步,如果链表有环则两个指针一定会相遇。代码如下: 1 /** 2 * Definition for singly-linked list. 3 * ...
阅读全文
摘要:题目地址:hereThere areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it c...
阅读全文
摘要:题目地址:here题目大意:几个小孩站一排,每个小孩有个等级值,现在给小孩发糖,发的时候要遵守2个规则:(1)每个小孩至少一颗糖(2)两个相邻的小孩中,等级大的小孩一定比等级小的小孩糖多,求发糖的数目的最小值。本文提供两个算法,第一个是我自己做题时用的,第二个是网上看题解时看到的算法1:该算法采用分...
阅读全文
摘要:题目地址:here题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把数组中所有数异或的结果就是这个出现一次的数,leetcode上也有这个题目。关于本题:我们用一个大小为3...
阅读全文
摘要:题目地址:here题目大意:深拷贝一个链表,链表除了含有next指针外,还包含一个random指针,该指针指向字符串中的某个节点或者为空。节点定义为: struct RandomListNode { int label; RandomListNode *next, *random...
阅读全文
摘要:题目地址:请戳我这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解。但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体可以对比两段代码),如果不去掉则会漏掉一些解(前一道题加约束条件是为了更快的判断是字符串是够能被分词,...
阅读全文