题目:
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 思路分析:
和《Leetcode: Linked List Cycle 》一样还是双指针的方法。一个循环链表如图
slow指针走了S=X+Y
fast指针走了F=X+Y+Z+Y
两个指针相遇。...
分类:
其他好文 时间:
2015-04-05 13:25:38
阅读次数:
216
题目:
Given a linked list, determine if it has a cycle in it. 思路分析:
利用快慢指针slow,fast。 slow指针每次走一步,fast指针每次走两步,倘若存在环,则slow和fast必定在某一时刻相遇。C++参考代码:/**
* Definition for singly-linked list.
* struct ListNo...
分类:
其他好文 时间:
2015-04-05 11:59:17
阅读次数:
99
problem:
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
Hide Tags
L...
分类:
其他好文 时间:
2015-04-02 10:31:03
阅读次数:
117
题目链接:Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will u...
分类:
其他好文 时间:
2015-03-20 22:01:20
阅读次数:
169
problem:
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).
n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0)...
分类:
其他好文 时间:
2015-03-18 15:53:14
阅读次数:
110
题意:
给你一棵无根树,求有多少点对之间距离
题解:
树分治。
然后对于一个重心X,我们把它的所有子树中的所有点存到结构体数组中。
结构体中存距离和子树编号。
第一遍sort,我们双指针扫哪些点对符合要求。
第二遍sort,我们把同一子树中还符合要求的点对数目删掉。
sort是O(nlogn)O(nlogn),处理是O(n)O(n)。
然后分治logn层,总时间复杂度O(nl...
分类:
其他好文 时间:
2015-03-16 13:00:34
阅读次数:
185
问题:一个字符串S(暂时只考虑小写字母),选择S中包含26种英文字母的最短子串,如果不包含则返回空字符
分析:双指针,动态维护一个区间。尾指针不断往后扫,当扫到有一个窗口包含了所有26种英文字母的字符串后,再收缩头指针,直到不能再收缩为止。最后记录所有可能的情况中窗口最小的。
代码示例:
#include
#include
#include
using namespace std;
c...
分类:
其他好文 时间:
2015-03-16 11:06:33
阅读次数:
105
题目链接:Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
这道题的要求是向右旋转链表...
分类:
其他好文 时间:
2015-03-15 23:43:32
阅读次数:
353
#include#includeusing namespace std;int arr[100066];int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=0 ; i<n ; ++i) scanf("%d",&arr[i]); sor...
分类:
其他好文 时间:
2015-03-04 06:12:08
阅读次数:
153
Problem
An army of n droids is lined up in one row. Each droid is described by m integers a1,?a2,?...,?am, where ai is the number of details of the i-th type in this droid’s m...
分类:
其他好文 时间:
2015-02-15 13:33:42
阅读次数:
155