给定一个链表,如果有环,返回环的起点,如果没环,则返回空指针。法一:unordered_set存做过的节点,一旦出现重复,那么它就是起点了。O(n)空间/** * Definition for singly-linked list. * struct ListNode { * int val...
分类:
其他好文 时间:
2014-12-13 16:27:02
阅读次数:
222
class Solution {//用set判断一个元素是否存在于集合中O(logn)。用到的一个优化是连续的x个数对应的序列长度都是一样的,可以通过判断元素是否遍历过来避免查找。没有这一步会超时。有的也用unordered_set代替set,据说这是hash表,O(1),更快。
public:
int longestConsecutive(vector &v) {
if(...
分类:
其他好文 时间:
2014-11-04 00:12:22
阅读次数:
239
编写代码,移除未排序链表中的重复结点。进阶如果不得使用临时缓冲区,该怎么解决?分析:使用set记录已访问过的值。时间复杂度O(n*logn),若使用unordered_set或者hash_set,则时间复杂度为O(n)。 1 #include 2 #include 3 #include 4 #...
分类:
其他好文 时间:
2014-09-18 22:10:24
阅读次数:
254
// 算法:DFS// 递归函数参数使用引用//Time Limit Exceeded 1 class Solution { 2 public: 3 vector > findLadders( string start, string end, unordered_set &dict) { ...
分类:
其他好文 时间:
2014-09-14 11:18:47
阅读次数:
154
关键字有序保存元素,
map,关联数组,保存关键字-值对,
set,关键字即值,只保存关键字的容器
multimap,关键字可重复出现
multiset,
无序集合
unordered_map,用哈希函数组织的map
unordered_set,用哈希函数组织的set
unordered_multimap,哈希组织的map;关键字可重复出现
unordered_multiset,...
分类:
编程语言 时间:
2014-09-10 14:16:30
阅读次数:
286
---恢复内容开始--- 1 class Solution { 2 public: 3 bool wordBreak(string s, unordered_set &dict) { 4 int nsize=s.size(); 5 int i=0,j=0; 6...
分类:
其他好文 时间:
2014-09-09 11:37:18
阅读次数:
242
class Solution {
public:
bool wordBreak(string s, unordered_set &dict){
int len = s.length();
vector match(len + 1, false);
match[0] = true;
for (int i = 1; i <= len; i++){
for (int k = 0;...
分类:
其他好文 时间:
2014-09-01 22:49:03
阅读次数:
273
1 class Solution { 2 vector > pos; 3 vector sen; 4 public: 5 6 vector wordBreak(string s, unordered_set &dict) { 7 getVector(s,d...
分类:
其他好文 时间:
2014-08-29 00:03:36
阅读次数:
256
这个题目思路:在一个bool型数组中,像接力一样传递匹配成功,传递到最后一个字符,说明匹配成功。说的明白点就是从第i(0~n)个字符开始向后与子串进行匹配,匹配的数组中标记为true,循环比较。需要注意的是:unordered_set的count(T s)查看是否包含该元素。string类的subs...
分类:
其他好文 时间:
2014-08-25 22:46:55
阅读次数:
260
STL hash table, Unordered Contain C++11加入使用hash table实现的Unordered Containers。容器中元素是不排序的,同时我们也不能指定某个元素的位置。 头文件:#include unordered_set; unordered_multis...
分类:
其他好文 时间:
2014-08-10 21:07:20
阅读次数:
294