leedcode
文章平均质量分 60
ZH_learningblog
To be best me!
展开
-
leedcode——判断单链表是否有环及寻找环的入口点
题目:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?思路:(1)设置快慢指针,因为链表中有环,所以最终会相遇,相原创 2017-03-27 15:05:31 · 553 阅读 · 0 评论 -
leedcode——二叉树的前序遍历
题目:Given a binary tree, return the preorder traversal of its nodes' values.思路:(1)递归方法:打印根节点,递归左孩子,递归右孩子(2)非递归方法:将根节点入栈,栈不为空时,获取栈顶元素,打印栈顶元素,如果右孩子不为空,右孩子入栈,如果左孩子不为空,左孩子入栈。(原因:栈是先进后出的顺序,右孩子先入栈最后才原创 2017-03-23 15:37:56 · 359 阅读 · 0 评论 -
leedcode——二叉树的后序遍历
题目:Given a binary tree, return the postorder traversal of its nodes' values.思路:(1)递归方法,递归打印左孩子,递归打印右孩子,打印根节点。(2)非递归方法,使用栈,逐层push左孩子,如果右孩子为NULL或已经打印过,打印根节点,否则把右孩子重复上述步骤。递归代码:/** * Definiti原创 2017-03-23 15:19:39 · 429 阅读 · 0 评论 -
leedcode——链表插入排序
题目:Sort a linked list using insertion sort.思路:定义一个临时链表存放已排序好的链表,将未排序好的节点依次插入。链表元素:1,3,8,4,6,5,2(递归到最后一个节点2)第一轮:temp链表元素为2,head 为5,操作:将5插入到2后面第二轮:temp链表元素为2,5,head为6,操作:将6插入到5后面第三轮:temp链表元素原创 2017-03-22 16:34:43 · 440 阅读 · 0 评论 -
leedcode——链表排序
题目:Sort a linked list in O(n log n) time using constant space complexity.思路1:利用快慢指针找到中间节点,将链表分成两部分,通过递归对左半部分进行递归排序,然后对右半部分进行递归排序,最后将两个链表进行递归排序。思路2:采用快速排序思想,将链表划分成有序链表和无序链表,通过链表插入与删除完成排序。代码1:原创 2017-03-22 15:45:57 · 346 阅读 · 0 评论 -
leedcode——Max Point on a Line
题目:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.思路:使用穷举法解决。先从第一个节点节点开始,循环遍历其他节点,由两点确定一条直线可得,遍历其他节点,通过斜率判断其他剩余节点是否在这条直线上,在直线上则count加1代码:/**原创 2017-03-22 11:26:55 · 346 阅读 · 0 评论 -
leedcode逆波兰表达式
题目:Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Some examples:["2", "1", "+", "3",原创 2017-03-22 10:45:06 · 322 阅读 · 0 评论 -
leedcode二叉树的最小深度与最大深度
一、最小深度 二叉树的最小深度为根节点到最近叶子的距离。(1)空树直接返回false;(2)只有一个根节点,返回深度1;(3)单分支情况:左子树为空,返回右子树的最大值+1;右子树为空,返回左子树的最大值+1;(4)双分支情况:返回左右子树的最小值+1.class Solution {public: int run(TreeNode *root)原创 2017-03-21 22:42:45 · 389 阅读 · 0 评论 -
leedcode—— single-number
题目:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without原创 2017-03-27 16:15:20 · 401 阅读 · 0 评论 -
leedcode—— word-break
题目:Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens ="leetcode",dict =原创 2017-03-27 15:55:13 · 447 阅读 · 0 评论 -
leedcode——reorder list
题目:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorde原创 2017-03-27 11:40:34 · 359 阅读 · 0 评论