怒刷leetcode题目(3)226,83,142,86
来源:互联网 发布:织梦cms拿站 编辑:程序博客网 时间:2024/06/06 04:36
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 1对的,这就是前阵子homebrew大神面试没做出来的那道题
其实这道题并不难…也许只是大神不屑于做这样的题目罢了…
照样的,我们发现以下规律
所有的左子树和右子树交换,除非到了结点(树叶)
主需要交换两棵子树然后对子树递归就好了
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */struct TreeNode* invertTree(struct TreeNode* root) { if(root == NULL){ return NULL; } struct TreeNode* temp = root->left; root->left=root->right; root->right = temp; invertTree(root->left); invertTree(root->right); return root;}
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* deleteDuplicates(struct ListNode* head) { if(head == NULL){ return false; } struct ListNode* pointer = head; while(pointer->next!=NULL){ if(pointer->val==pointer->next->val){ pointer->next = pointer->next->next; }else{ pointer= pointer->next; } } return head;}
142Linked List Cycle II31.4%Medium
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
Follow up:
Can you solve it without using extra space?
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode *detectCycle(struct ListNode *head) { if(head==NULL){ return false; } struct ListNode *slow = head; struct ListNode *fast = head; while(fast!=NULL && fast->next!=NULL){ slow = slow->next; fast = fast->next->next; if(slow == fast){ slow = head; while(slow!=fast){ slow = slow->next; fast = fast->next; } return slow; } } return false;}
这道题目跟141(上一篇博客中提及的)很类似,都是找出环
142实际是升级版,要找出环的入口
同样是使用快慢指针,大家可以在纸上画一下,写一下
如果有环,第一次相遇的时候,慢指针走了L+X的路程(L是起点到环入口的距离,X是入口到相遇时走过的距离,距离有可能比一圈的长度大)
快指针想当然的走了(L+X)*2的路程(其实不一定是2,只不过2比较好计算而已)
而且!!!快指针走过的距离是L+X+m*R(L、X同上,毕竟相遇点相同,m代表的是走过的圈数,R代表圈的长度)
也就是L+X=m*R
所以L=m*R-X
这时候,我们将慢指针回到起点,快指针的每一步的距离变成1
我们可以知道,慢指针和快指针相遇的时候
慢指针走了L,快指针走了m*R+m*R+X-X=2*m*R,也是在环的入口
所以他们再次相遇的节点就是环的入口
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given 1->4->3->2->5->2
and x = 3,
return 1->2->2->4->3->5
.
遍历一次,将节点放进相应的链表,最后相连就好了,注意细节比较重要
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* partition(struct ListNode* head, int x) { if(head==NULL){ return NULL; } if(head->next==NULL){ return head; } struct ListNode* less_list = NULL; struct ListNode* less_list_head = NULL; struct ListNode* greater_list = NULL; struct ListNode* greater_list_head = NULL; struct ListNode* pointer = head; while(head!=NULL){ struct ListNode* next = head->next; head->next = NULL; if(head->val<x){ if(less_list_head==NULL){ less_list_head=head; less_list=head; }else{ less_list->next = head; less_list=less_list->next; } }else{ if(greater_list_head==NULL){ greater_list_head=head; greater_list=head; }else{ greater_list->next = head; greater_list=greater_list->next; } } head=next; } if(less_list_head==NULL){ return greater_list_head; } less_list->next=greater_list_head; return less_list_head;}
- 怒刷leetcode题目(3)226,83,142,86
- LeetCode题目1~3
- leetcode题目总结<3>
- LeetCode题目3
- LeetCode算法题目:3Sum
- leetcode部分题目(C++)
- 怒刷leetcode的题目(1)237、104、136、100
- 怒刷leetcode题目(2)235,191,141,217,
- leetcode题目
- LeetCode 题目
- < 题目 > LeetCode
- 31.leetcode题目226: Invert Binary Tree
- leetcode第3个算法题目
- leetcode 题目标记(补充版)
- MaxGap leetcode题目(桶排序)
- leetcode题目:Word Pattern (Javascript版)
- LeetCode经典题目笔记(一)
- LeetCode经典题目笔记(二)
- Option,None和Some
- 动态规划-钢条切割
- 移植MonkeyRunner的图片对比和获取子图功能的实现-UiAutomator/Robotium篇
- nbench 使用方法
- 位图法寻找数组中重复出现的数字
- 怒刷leetcode题目(3)226,83,142,86
- 54.变换UIButton中的label和image的位置
- 正则表达式
- spring3 jdbctemplate 注解实例(有时间字段转换的方法) .
- Layer 用法的总结
- svn手动启动
- bzoj1212: [HNOI2004]L语言
- [置顶] Android L SurfaceFlinger dump信息全解(1)
- html5之canvas基础1