- 博客(29)
- 收藏
- 关注
原创 LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
题目:Sort a linked list using insertion sort.题解:插入排序就是先对一部分进行排序 排序好后将未排序的插入到已经排序好的队列中 在插入的时候 如果是数组的话可以从前往后 也可以从后往前 对于链表就只能是前者了代码:public static ListNode insertionSortList(ListNode
2015-07-30 16:38:54 4335 1
原创 LeetCode108_Convert SortedArray to BinarySearchTree(将有序数组转成二叉排序树) Java题解
题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题解:和我上面一篇将有序链表转成二叉排序树中用哈希表解的方法是一样的,基本思路:链表中间那个节点为树的根节点,根节点的左子树节点应该是根节点左边那部分的中间节点,根节点的右节点应该是根
2015-07-30 09:19:54 1509
原创 LeetCode109_Convert Sorted List to Binary Search t题目tiTree(将链表转成二叉排序树) Java题解
题目:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.题解:将一个有序链表转成二叉排序树,如果是一棵相对平衡的排序树,应该是这样的,链表中间那个节点为树的根节点,根节点的左子树节点应该是根节点左边那部分的中间节点,根节点的
2015-07-30 08:56:04 829
原创 LeetCode234_PalindromeLinkedList (判断是否为回文链表) Java题解
题目:Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?题解:判断一个链表是不是回文的,这里要求O(n)时间复杂度和O(1)的空间时间复杂度,总共想了三种办法,三种办法都用到了两个指针,符合
2015-07-29 11:40:29 6735 2
原创 LeetCode237_Delete Node in a Linked List(删除链表中的节点) Java题解
题目:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node
2015-07-29 09:02:05 853
原创 LeetCode201 Bitwise AND of Numbers Range Java 题解
题目:Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.解答:假如说5:101 7:111 连续几个数相与的规律:一,只要是相同的位置的数字不相同最后那个位置的结果一定是0 。二,如果高位不相同,从不相同的那位到最低位都会为0,例如5和7虽然第0
2015-07-16 20:29:37 868
原创 LeetCode222 Count CompleteTree Nodes(计算完全二叉树的节点数) Java 题解
题目:Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completel
2015-07-09 20:51:26 1926
原创 LeetCode103 BinaryTreeZigzagLevelOrderTraversal(二叉树Z形层次遍历) Java题解
题目:Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Give
2015-07-09 17:08:19 1154
原创 LeetCode232 Implement Queue using Stacks Java 题解
题目:Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front eleme
2015-07-09 12:03:51 2861
原创 LeetCode225 Implemet Stack using Queues Java题解
题目:Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Re
2015-07-09 11:58:05 828
原创 LeetCode145 Binary Tree Postorder Traversal Java题解(递归 迭代)
题目: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].解题:递归的还是和前面中
2015-07-09 10:34:20 1625
原创 LeetCode144 Binary Tree PreOrder Traversal Java题解(递归 迭代)
题目: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].解题:先序遍历二叉树 和
2015-07-08 16:18:46 725
原创 LeetCode94 BinaryTreeInorderTraversal Java题解(递归 迭代)
题目:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].解题:中序遍历一颗二叉树,如
2015-07-08 09:57:06 1495
原创 LeetCode110 Blanced Binary Tree Java 题解
题目:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node ne
2015-07-06 22:01:02 545
原创 LeetCode107 Binary Tree Level Order Traversal II
题目:Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,
2015-07-06 21:29:24 496
原创 LeetCode102 Binary Tree Level Order Traversal Java题解
题解:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9
2015-07-06 21:13:28 882
原创 LeetCode100 SameTree java题解
题目:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.解题:
2015-07-06 19:55:23 891
原创 LeetCode101 SynmetricTree Java题解
题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3
2015-07-06 19:43:06 523
原创 LeetCode226 InvertBinaryTree Java题解
题目:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1解答:遍历每一个节点 直接交换他们的左右节点代码:public static TreeNode invertTr
2015-07-06 16:42:58 553
原创 LeetCode151_Reverse Words in a String
题目:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".注意的地方:What constitutes a word?A sequence of non-space
2015-07-06 15:58:06 492
原创 LeetCode104_MaximumDepthofBinaryTree Java题解
题目:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.解题:求最大深度 和前面一题类似 用递归遍历就
2015-07-06 15:14:38 595
原创 LeetCode111 MinmumDepthofBinaryTree java 题解
题目: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.解题:求根节点到最近的叶子节点之间的距离用
2015-07-06 15:03:31 587
原创 LeetCode112 PathSum Java题解
题目:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree
2015-07-06 10:58:52 965
原创 LeetCode172_FactorialTrailingZeroes java题解
题目:Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.解答:求N!后面有多少个零,这题考的主要是数学,举一个简单例子102!=1*2*3*4*5 *6*7*8*9*10
2015-07-03 11:30:36 574
原创 LeetCode224 BasicCalculator java题解
题目:Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers
2015-07-02 21:52:24 668
原创 LeetCode219 ContainsDuplicateII java题解
题目:Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is a
2015-07-02 17:50:06 668
原创 LeetCode229 MajorityElementII java题解
题目:Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.求出现次数大于三分之一数组的长度,所以最多就只有2个这样的元素,题目要求线性时间复杂度和常数的空
2015-07-01 20:39:56 679
原创 求最长有效括号长度 java
题目:输入只包含圆括号的字符串,找出最长的合法括号子串的长度比如:“(()”——》2,最长的有效子串是“()”“)()())”——》4,最长的有效子串是“()()”解题思路:如果输入是左括号就直接入栈,如果是右括号,如果此时栈为空或此时的栈顶不为左括号就不作处理同时把记录有效长度的临时变量置为0,如果栈顶为左括号则出栈并把记录有效长度的临时变量加2.最后返回最大的记录长
2015-07-01 08:45:39 2405
原创 LeetCode71 Simplify Path java题解
题目:Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"题解:解题思路:这题是简化目录,每一级目录前面都有一个斜杠,我可以首先对斜杠进行分割,分割之后
2015-07-01 08:11:03 1857
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人