leetcode
文章平均质量分 61
nova0831
这个作者很懒,什么都没留下…
展开
-
【leetcode】237. Delete Node in a Linked List
题目要求: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 w原创 2016-04-14 21:58:25 · 184 阅读 · 0 评论 -
【leetcode】242. Valid Anagram
题目要求: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:原创 2016-06-18 17:53:59 · 180 阅读 · 0 评论 -
【leetcode】169. Majority Element
题目要求:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majori原创 2016-06-19 17:44:30 · 180 阅读 · 0 评论 -
【leetcode】217. Contains Duplicate
题目要求:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every e原创 2016-06-20 12:33:58 · 145 阅读 · 0 评论 -
【leetcode】191. Number of 1 Bits
题目要求:Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representa原创 2016-06-20 13:47:51 · 192 阅读 · 0 评论 -
【leetcode】121. Best Time to Buy and Sell Stock
题目要求:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of原创 2016-06-23 12:43:09 · 168 阅读 · 0 评论 -
【leetcode】111. 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.给定一颗二叉树,求最小深度/** * D原创 2016-11-02 21:46:31 · 150 阅读 · 0 评论 -
【leetcode】110.Balanced Binary Tree
题目要求: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原创 2016-11-02 22:14:17 · 153 阅读 · 0 评论 -
【leetcode】107. 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,原创 2016-11-03 15:22:17 · 212 阅读 · 0 评论 -
【leetcode】101. Symmetric Tree
题目要求:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \原创 2016-11-03 15:40:47 · 263 阅读 · 0 评论 -
【leetcode】257. Binary Tree Paths
题目要求: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"]原创 2016-11-03 15:52:50 · 245 阅读 · 0 评论 -
【leetcode】199. Binary Tree Right Side Viewti
题目要求:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tr原创 2016-11-04 17:20:20 · 229 阅读 · 0 评论 -
【leetcode】108. Convert Sorted Array to Binary Search Tree
题目要求:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.给定一个排好序的数组,要求转换成二叉搜索树思路:数组中间的元素作为树的根节点,左边的元素是左子树,右边的元素是右子树,递归建立左右子树/** * Definition for原创 2016-11-04 17:26:28 · 202 阅读 · 0 评论 -
【leetcode】109. Convert Sorted List to Binary Search Tree
题目要求:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.给定一个排好序的链表,转换成二叉搜索树思路:和108题类似,/** * Definition for singly-linked list. * p原创 2016-11-04 18:07:02 · 212 阅读 · 0 评论 -
【leetcode】103. Binary Tree Zigzag Level Order Traversal
题目要求: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:Gi原创 2016-11-04 18:48:00 · 192 阅读 · 0 评论 -
【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
题目要求:Given preorder and inorder traversal of a tree, construct the binary tree.通过先序遍历和中序遍历构造二叉树思路:先序遍历的第一个结点在中序遍历中找到位置i,然后在先序遍历的位置i左边是左子树,先序遍历的位置i右边是右子树,然后递归的构造左右子树/** * Definition原创 2016-11-04 19:14:01 · 337 阅读 · 0 评论 -
【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
题目要求:Given inorder and postorder traversal of a tree, construct the binary tree.给定二叉树后序和中序遍历,构造二叉树思路:后序的最后一个结点是根节点,在中序遍历中找到对应的位置,记录左子树的size和右子树的size,递归构造左右子树/** * Definition for a binar原创 2016-11-04 21:09:42 · 175 阅读 · 0 评论 -
【leetode】349. Intersection of Two Arrays
题目要求:Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note:Each element in the result must be uni原创 2016-06-17 15:35:21 · 171 阅读 · 0 评论 -
【leetcode】202. Happy Number
题目要求:Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of原创 2016-06-16 21:09:28 · 140 阅读 · 0 评论 -
【leetcode】263. Ugly Number
题目要求:Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is no原创 2016-06-16 20:54:25 · 206 阅读 · 0 评论 -
【leetcode】206. Reverse Linked List
题目要求:Reverse a singly linked list.即反转一个单向链表/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */p原创 2016-04-14 21:54:22 · 229 阅读 · 0 评论 -
【leetcode】232. Implement Queue using Stacks
题目要求: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原创 2016-04-14 22:01:41 · 245 阅读 · 0 评论 -
【leetcode】225. Implement Stack using Queues
题目要求: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原创 2016-04-14 22:12:10 · 266 阅读 · 0 评论 -
【leetcode】155. Min Stack
题目要求:Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top(原创 2016-04-16 22:46:57 · 299 阅读 · 0 评论 -
【leetcode】292. Nim Game
题目要求:You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone wi原创 2016-04-16 22:54:06 · 200 阅读 · 0 评论 -
【leetcode】258. Add Digits
题目要求:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since原创 2016-04-16 23:00:56 · 183 阅读 · 0 评论 -
【leetcode】283. Move Zeroes
题目要求:Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after ca原创 2016-05-01 19:33:51 · 164 阅读 · 0 评论 -
【leetcode】104. Maximum Depth of Binary Tree
题目要求: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.即给定一个二叉树,求它的最大深度思路:递归原创 2016-05-02 18:12:09 · 178 阅读 · 0 评论 -
【leetcode】226. Invert Binary Tree
题目要求:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1即将一颗二叉树进行左右翻转思路:递归的将左子树和右子树进行对调/** * Definition for a bin原创 2016-05-02 21:49:21 · 191 阅读 · 0 评论 -
【leetcode】125. Valid Palindrome
题目要求:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a c原创 2016-04-19 17:05:53 · 210 阅读 · 0 评论 -
【leetcode】9. Palindrome Number
题目要求:Determine whether an integer is a palindrome. Do this without extra space.即判断一个数x是不是回文数思路:首先获取这个整数的位数count,定义一个n等于10的count-1次幂,让一个临时变量number等于这个整数。number%10获取的是最后一位,x/n%10获取的是第一位,判断这两原创 2016-04-19 17:34:23 · 233 阅读 · 0 评论 -
【leetcode】102. Binary Tree Level Order Traversal
题目要求: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 / \原创 2016-05-03 20:39:18 · 260 阅读 · 0 评论 -
【leetcode】100. Same Tree
题目要求: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.即原创 2016-05-04 19:57:15 · 200 阅读 · 0 评论 -
【leetcode】141. Linked List Cycle
题目要求:Given a linked list, determine if it has a cycle in it.即给定一个链表,判断是否有环的存在思路:定义一个快指针,一个慢指针,若两个指针相遇,则说明有环存在/** * Definition for singly-linked list. * class ListNode { * int va原创 2016-05-04 20:28:41 · 229 阅读 · 0 评论 -
【leetcode】326. Power of Three 231. Power of Two 342. Power of Four
这三道题做法一致,以326.Power of Three 为例题目要求:Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?给定一个整数,判断是不是3原创 2016-05-05 15:19:23 · 228 阅读 · 0 评论 -
【leetcode】235. Lowest Common Ancestor of a Binary Search Tree
题目要求:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is def原创 2016-05-06 13:51:56 · 184 阅读 · 0 评论 -
【leetcode】83. Remove Duplicates from Sorted List
题目要求: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.即删除链表中重复的元素,使每原创 2016-11-04 23:15:43 · 194 阅读 · 0 评论