Restore IP Addresses & Sort List & Reorder List
2014-07-27 13:57
411 查看
(1) Restore IP Addresses
dfs, 几点注意的地方[1]:
1. 在验证字符串是否是数字的时候,要注意0的情况,001,010,03都是非法的。所以,如果第一位取出来是0,那么我们就判断字符串是否是"0",不是的情况都是非法的。
2. 取字符串的时候,注意位数不够的问题,不仅<4, 而且<s.length()。
class Solution {
private:
bool isValid(string s){
if(s[0]=='0')
return s=="0";
int num=atoi(s.c_str());
return num<=255 && num>0;
}
void dfs(string s,string tmp, vector<string> &ret,int dep) {
if(dep==3 && isValid(s))
{
ret.push_back(tmp+s);
return;
}
for(int i=1; i<4 && i<s.size(); i++)
{
string sub=s.substr(0,i);
if(isValid(sub))
dfs(s.substr(i,s.size()-i),tmp+sub+'.',ret,dep+1);
}
}
public:
vector<string> restoreIpAddresses(string s) {
vector<string> ret;
if(s.size()<4 || s.size()>12)
return ret;
dfs(s,"",ret,0);
return ret;
}
};
(2) Sort List
nlogn的排序有快速排序、归并排序、堆排序。双向链表用快排比较适合,堆排序也可以用于链表,单向链表适合用归并排序。以下是用归并排序的代码[2]:
class Solution {
private:
ListNode *mergeSort(ListNode *a,ListNode *b) {
if(!a) return b;
if(!b) return a;
ListNode *c,*cur;
if(a->val < b->val)
{ c=a; a=a->next;}
else {c=b; b=b->next;}
cur=c;
while(a && b)
{
if(a->val < b->val)
{
cur->next=a;
a=a->next;
}
else
{
cur->next=b;
b=b->next;
}
cur=cur->next;
}
if(a)
cur->next=a;
else
cur->next=b;
return c;
}
public:
ListNode *sortList(ListNode *head) {
if(!head || !head->next)
return head;
ListNode *fast=head,*slow=head;
while(fast->next && fast->next->next)
{
slow=slow->next;
fast=fast->next->next;
}
fast=slow;
slow=slow->next;
fast->next=NULL;
fast=sortList(head);
slow=sortList(slow);
return mergeSort(fast,slow);
}
};
(3) Reorder List
把整个链表划分成2个等长的子链表,如果原链表长度为奇数,那么第一个子链表的长度多1,然后翻转第二个子链表,最后将两个子链表元素逐个轮流合并。
class Solution {
private:
ListNode *reverseList(ListNode *l) {
if(l->next==NULL)
return l;
ListNode *pre=l,*cur=l->next;
while(cur!=NULL)
{
ListNode* tmp=cur->next;
cur->next=pre;
pre=cur;
cur=tmp;
}
l->next=NULL;
return pre;
}
ListNode *mergeList(ListNode *a, ListNode *b)
{
ListNode *c=a,*cur;
a=a->next;
cur=c;
while(a!=NULL && b!=NULL)
{
cur->next=b;
b=b->next;
cur=cur->next;
cur->next=a;
a=a->next;
cur=cur->next;
}
if(b!=NULL)
cur->next=b;
return c;
}
public:
void reorderList(ListNode *head) {
if(head==NULL || head->next==NULL || head->next->next==NULL)
return;
ListNode *slow=head,*fast=head;
while(fast->next!=NULL && fast->next->next!=NULL)
{
slow=slow->next;
fast=fast->next->next;
}
fast=slow->next;
slow->next=NULL;
fast=reverseList(fast);
head=mergeList(head,fast);
}
};
参考:
[1] http://blog.csdn.net/u011095253/article/details/9158449
[2] http://www.cnblogs.com/TenosDoIt/p/3434550.html
dfs, 几点注意的地方[1]:
1. 在验证字符串是否是数字的时候,要注意0的情况,001,010,03都是非法的。所以,如果第一位取出来是0,那么我们就判断字符串是否是"0",不是的情况都是非法的。
2. 取字符串的时候,注意位数不够的问题,不仅<4, 而且<s.length()。
class Solution {
private:
bool isValid(string s){
if(s[0]=='0')
return s=="0";
int num=atoi(s.c_str());
return num<=255 && num>0;
}
void dfs(string s,string tmp, vector<string> &ret,int dep) {
if(dep==3 && isValid(s))
{
ret.push_back(tmp+s);
return;
}
for(int i=1; i<4 && i<s.size(); i++)
{
string sub=s.substr(0,i);
if(isValid(sub))
dfs(s.substr(i,s.size()-i),tmp+sub+'.',ret,dep+1);
}
}
public:
vector<string> restoreIpAddresses(string s) {
vector<string> ret;
if(s.size()<4 || s.size()>12)
return ret;
dfs(s,"",ret,0);
return ret;
}
};
(2) Sort List
nlogn的排序有快速排序、归并排序、堆排序。双向链表用快排比较适合,堆排序也可以用于链表,单向链表适合用归并排序。以下是用归并排序的代码[2]:
class Solution {
private:
ListNode *mergeSort(ListNode *a,ListNode *b) {
if(!a) return b;
if(!b) return a;
ListNode *c,*cur;
if(a->val < b->val)
{ c=a; a=a->next;}
else {c=b; b=b->next;}
cur=c;
while(a && b)
{
if(a->val < b->val)
{
cur->next=a;
a=a->next;
}
else
{
cur->next=b;
b=b->next;
}
cur=cur->next;
}
if(a)
cur->next=a;
else
cur->next=b;
return c;
}
public:
ListNode *sortList(ListNode *head) {
if(!head || !head->next)
return head;
ListNode *fast=head,*slow=head;
while(fast->next && fast->next->next)
{
slow=slow->next;
fast=fast->next->next;
}
fast=slow;
slow=slow->next;
fast->next=NULL;
fast=sortList(head);
slow=sortList(slow);
return mergeSort(fast,slow);
}
};
(3) Reorder List
把整个链表划分成2个等长的子链表,如果原链表长度为奇数,那么第一个子链表的长度多1,然后翻转第二个子链表,最后将两个子链表元素逐个轮流合并。
class Solution {
private:
ListNode *reverseList(ListNode *l) {
if(l->next==NULL)
return l;
ListNode *pre=l,*cur=l->next;
while(cur!=NULL)
{
ListNode* tmp=cur->next;
cur->next=pre;
pre=cur;
cur=tmp;
}
l->next=NULL;
return pre;
}
ListNode *mergeList(ListNode *a, ListNode *b)
{
ListNode *c=a,*cur;
a=a->next;
cur=c;
while(a!=NULL && b!=NULL)
{
cur->next=b;
b=b->next;
cur=cur->next;
cur->next=a;
a=a->next;
cur=cur->next;
}
if(b!=NULL)
cur->next=b;
return c;
}
public:
void reorderList(ListNode *head) {
if(head==NULL || head->next==NULL || head->next->next==NULL)
return;
ListNode *slow=head,*fast=head;
while(fast->next!=NULL && fast->next->next!=NULL)
{
slow=slow->next;
fast=fast->next->next;
}
fast=slow->next;
slow->next=NULL;
fast=reverseList(fast);
head=mergeList(head,fast);
}
};
参考:
[1] http://blog.csdn.net/u011095253/article/details/9158449
[2] http://www.cnblogs.com/TenosDoIt/p/3434550.html
相关文章推荐
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
- Binary Tree Zigzag Level Order Traversal,Restore IP Addresses,Word Search,Simplify Path
- Leetcode: Reorder List && Summary: Reverse a LinkedList
- LeetCode-Restore IP Addresses<ERROR>
- leetcode day5 -- Reorder List && Linked List Cycle II
- [LeetCode] Restore IP Addresses
- Restore IP Addresses
- leetcode: Restore IP Addresses
- Restore IP Addresses
- lockless编程中几个必知必会的概念(compile reorder && memory barrier && volatile)
- leetcode 125: Restore IP Addresses (uncompleted)
- RESTORE FILELISTONLY & HEADERONLY & LABELONLY & VERIFYONLY
- Android 2.2 API Demos -- Redirection, Reorder Activity and Save&Restore State
- Java实现从IP和端口列表List<String>获取分离的IP和端口值
- [LeetCode] Restore IP Addresses
- [LeetCode] Restore IP Addresses 解题报告
- dedecms5.6 按weight排序,{dede:list pagesize='10' orderby='weight'}无效
- android Collections.sort(List<T> list) 与JAVA Collections.sort(List<T> list)