Longest Substring Without Repeating Characters & Rotate List
来源:互联网 发布:联合国贸易数据库 编辑:程序博客网 时间:2024/05/22 11:48
(1) Longest Substring Without Repeating Characters
此题用一个hash table保存每个字符上一次出现过的位置。从前往后扫描,假如发现字符上次出现过,就把当前子串的起始位置start移动到上次出现过的位置之后——这是为了保证从start到i的当前子串中没有任何重复字符。同时,由于start移动,当前子串的内容改变,start移动过程中经历的字符都要剔除。[1]
class Solution {public: int lengthOfLongestSubstring(string s) { int begin=0; int maxlen=0; int hashtable[256]; memset(hashtable,-1,256*sizeof(int)); for(int i=0;i<s.size();i++) { if(hashtable[s[i]]!=-1) while(begin<=hashtable[s[i]]) { hashtable[s[begin]]=-1; begin++; } maxlen=max(maxlen,i-begin+1); hashtable[s[i]]=i; } return maxlen; }};
(2) Rotate List
游标指针从头节点向后移动,当指向尾节点时,得到链表长度len,同时将链表头尾相连,接着游标再向后移动 k % len 步得到结果链表的尾节点。[2]
class Solution {public: ListNode *rotateRight(ListNode *head, int k) { if(!head) return head; int len=1; ListNode *cur=head,*ret; while(cur->next) { cur=cur->next; len++; } cur->next=head; len=len-k%len; while(len>0) { cur=cur->next; len--; } ret=cur->next; cur->next=NULL; return ret; }};
参考:
[1] http://blog.csdn.net/cshaxu/article/details/12433931
[2] http://www.cnblogs.com/infinityu/archive/2013/05/14/3077364.html
0 0
- Longest Substring Without Repeating Characters & Rotate List
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- java序列化及反序列化
- 内核启动卡在Uncompressing Linux..... done, booting the kernel
- 造成segmentation fault的可能原因分析
- 【译】Java面试参考指南(一)
- Uva 123 Searching Quickly
- Longest Substring Without Repeating Characters & Rotate List
- C语言参数入栈顺序
- Xib处理coreData数据库的增删改查常见错误
- Myeclipse 10 破解方法+下载地址
- 杭电1166敌兵布阵
- 当前的几种开源游戏服务端介绍
- 程序员每天该做的事
- asp.net 使用 MongoDB 初体验
- 大数据量的两个表数据复制的一些经验总结