![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 80
yvanbu
应用软件开发工程师,熟悉java,python
展开
-
LeetCode242_Valid Anagram(判断两个字符串是不是由完全一样字符组成) Java题解
题目:Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:Yo原创 2015-08-20 14:33:59 · 1824 阅读 · 0 评论 -
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 · 556 阅读 · 0 评论 -
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 · 552 阅读 · 0 评论 -
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 · 500 阅读 · 0 评论 -
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 · 901 阅读 · 0 评论 -
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 · 600 阅读 · 0 评论 -
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 · 974 阅读 · 0 评论 -
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 · 581 阅读 · 0 评论 -
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 · 669 阅读 · 0 评论 -
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 · 1954 阅读 · 0 评论 -
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 · 1159 阅读 · 0 评论 -
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 · 896 阅读 · 0 评论 -
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 · 498 阅读 · 0 评论 -
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 · 590 阅读 · 0 评论 -
LeetCode257 BinaryTreePaths(打印根节点到叶子节点的左右路径) Java题解
题目:Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]原创 2015-08-20 21:44:37 · 1859 阅读 · 0 评论 -
LeetCode96_Unique Binary Search Trees(求1到n这些节点可以组成多少种不同的二叉查找树) Java题解
题目:Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2原创 2015-08-20 14:19:07 · 1537 阅读 · 0 评论 -
LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
题目:Sort a linked list using insertion sort.题解:插入排序就是先对一部分进行排序 排序好后将未排序的插入到已经排序好的队列中 在插入的时候 如果是数组的话可以从前往后 也可以从后往前 对于链表就只能是前者了代码:public static ListNode insertionSortList(ListNode原创 2015-07-30 16:38:54 · 4345 阅读 · 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 · 1522 阅读 · 0 评论 -
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 · 836 阅读 · 0 评论 -
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 · 860 阅读 · 0 评论 -
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 · 6771 阅读 · 2 评论 -
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 · 1511 阅读 · 0 评论 -
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 · 530 阅读 · 0 评论 -
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 · 1644 阅读 · 0 评论 -
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 · 672 阅读 · 0 评论 -
LeetCode86之PartionList的java题解
题目: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原创 2015-05-07 12:10:52 · 485 阅读 · 0 评论 -
LeetCode 148之Sort List的java题解
转自:http://blog.csdn.net/worldwindjp/article/details/18986737题目:Sort a linked list in O(n log n) time using constant space complexity.解答:解题报告:就是对一个链表进行归并排序。主要考察3个知识点,知识点1:归并排序的整体思想知识点转载 2015-05-07 10:00:07 · 2066 阅读 · 0 评论 -
LeetCode2之AddTwoNumbers的java题解
题目:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it原创 2015-05-07 11:14:08 · 466 阅读 · 0 评论 -
LeetCode 82之Remove Duplicates from Sorted List II的Java题解
题目:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Gi原创 2015-05-06 17:23:34 · 1066 阅读 · 0 评论 -
LeetCode 160之Intersection of Two Linked Lists 的Java题解
题目:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2015-05-06 21:31:51 · 776 阅读 · 0 评论 -
LeetCode83之Remove Duplicates from Sorted List的Java题解
题目: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.解答:因为是有序链表,所以去原创 2015-05-06 09:40:49 · 1048 阅读 · 0 评论 -
LeetCode 19之Remove Nth Node From End of List的Java题解(三种解法)
题目:Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the原创 2015-05-06 11:35:54 · 550 阅读 · 0 评论 -
LeetCode92之Reverse Linked ListII的 Java题解
题目:Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n sa原创 2015-05-05 18:06:05 · 593 阅读 · 1 评论 -
LeetCode206之Reverse Linked List Java题解
题目:Reverse a singly linked list.解题:采用最直接的思路,从链表的第二个节点开始向后遍历,将每一个遍历的节点插入作为当前的第一个节点,为了方便操作,我们定义一个fakeNode节点指向第一个节点,后面需要插入的节点需要做两件事:一,使需要插入的节点的next指向当前链表中的第一个节点(即fakeNode的下一个节点);二,让fakeNode的next指原创 2015-05-05 16:15:03 · 1791 阅读 · 0 评论 -
LeetCode141之Linked List Cycle java题解
之前我的一个同学在面试腾讯的时候问到一个问题:如何判断一个链表存在环?当时我同学说了两种思路:一,记录某一个节点,遍历链表看最终是否能回到这个节点;二,设置一个快一个慢的指针(即一个一次走一步,一个一次走两步)如果他们能够相遇就说明有环。看到Leetcode这题判断是否有环的时候,发现如果链表本身如果不是一个标准环的话是无法用第一种思路判断出来的。相对来说,一快一慢这种解法更有通用性。下面就是原创 2015-05-05 10:04:25 · 1383 阅读 · 0 评论 -
LeetCode143之ReorderList的Java题解
题目: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}, reord原创 2015-05-07 20:57:31 · 492 阅读 · 0 评论 -
LeetCode24之SwapNodesInPairs的Java 题解
题目:Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant原创 2015-05-07 16:28:59 · 657 阅读 · 0 评论 -
LeetCode20 Valid Parentheses的java 题解
题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are原创 2015-06-17 16:36:04 · 419 阅读 · 0 评论 -
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 · 685 阅读 · 0 评论 -
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 · 2887 阅读 · 0 评论