
leetcode
文章平均质量分 66
Cassie曹
这个作者很懒,什么都没留下…
展开
-
minimum-depth-of-binary-tree
题目描述Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.IDEA求树的最小深度:1.递归 先序遍历 (深度优先)原创 2017-02-21 15:58:21 · 161 阅读 · 0 评论 -
candy
题目描述There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least原创 2017-02-25 22:32:00 · 243 阅读 · 0 评论 -
single-number-ii
题目描述Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it witho原创 2017-02-25 16:15:31 · 202 阅读 · 0 评论 -
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 usi原创 2017-02-25 11:23:32 · 231 阅读 · 0 评论 -
copy-list-with-random-pointer
题目描述A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.IDEA实现带随机指针链表的深拷贝。深拷贝要就开辟新原创 2017-02-25 10:38:39 · 215 阅读 · 0 评论 -
word-break-ii
题目描述Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens =原创 2017-02-25 09:35:44 · 196 阅读 · 0 评论 -
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 =["l原创 2017-02-25 08:51:13 · 214 阅读 · 0 评论 -
linked-list-cycle
题目描述Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?IDEA借助缓慢指针能相遇,说明有环CODE/** * Definition for singly-linked list. * struc原创 2017-02-23 10:11:26 · 193 阅读 · 0 评论 -
linked-list-cycle-ii
题目描述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?IDEA借用别人的一个图来说明。设置两个指针,slow每次走一步,原创 2017-02-23 09:49:56 · 285 阅读 · 0 评论 -
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}, reorder it to原创 2017-02-23 09:21:42 · 248 阅读 · 0 评论 -
binary-tree-preorder-traversal
题目描述Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note: Recursive solution is原创 2017-02-22 20:29:31 · 162 阅读 · 0 评论 -
binary-tree-postorder-traversal
题目描述Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].Note: Recursive soluti原创 2017-02-22 15:31:01 · 156 阅读 · 0 评论 -
insertion-sort-list
题目描述Sort a linked list using insertion sort.IDEA插入排序:将一个节点插入有序的链表。需要创立新的节点插入node=new ListNode(p->val);pre->next=node;CODE/** * Definition for singly-linked list. * struct ListNode {原创 2017-02-22 14:52:31 · 178 阅读 · 0 评论 -
sort-list
题目描述Sort a linked list in O(n log n) time using constant space complexity.IDEA要求空间复杂度为常数,时间复杂度O(nlogn)归并排序:空间复杂度O(n),时间复杂度O(nlogn)1.首先找到list的中点,用连个指针,一个慢,一个快,慢的每次走一个node,快的每次走两个node,当快的走到lis原创 2017-02-22 11:48:28 · 198 阅读 · 0 评论 -
max-points-on-a-line
题目描述Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.IDEA求出在一条直线上的最大点数。直线的可能情况:1.在一条垂直x轴的直线上(斜率无穷大),2.斜率相同CODE/** * Definition for a poin原创 2017-02-22 10:09:43 · 242 阅读 · 0 评论 -
evaluate-reverse-polish-notation
题目描述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", "+",原创 2017-02-22 09:18:12 · 179 阅读 · 0 评论 -
字符编码
题目描述请设计一个算法,给一个字符串进行二进制编码,使得编码后字符串的长度最短。IDEA考查的是哈夫曼编码。统计每个字符出现的频率,使得出现频率最小的字符编码长度最长,出现频率最大的字符,编码长度最小。设置一个小顶堆,用于每次取出对顶的最小的两个元素,两这两个元素相加再放入堆中,直到堆中只剩下一个元素位置。编码的长度即为此哈夫曼树非叶子节点数值和,即累加每次从堆中取出的最小两个原创 2017-03-30 11:40:36 · 449 阅读 · 0 评论