
LeetCode
文章平均质量分 64
treeshy
In any relationship it's important to have boundaries that are respeceted by all parties involved.
展开
-
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 will b原创 2016-04-01 09:21:28 · 215 阅读 · 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 2 has on原创 2016-04-01 11:08:57 · 169 阅读 · 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-04-01 11:25:33 · 209 阅读 · 0 评论 -
LeetCode *** 226. Invert Binary Tree
题目:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1分析:使用递归是一种很简单的解决这个问题的方式,但是同样可以和队列结合使用非递归算法。代码:递归:原创 2016-04-05 09:24:50 · 216 阅读 · 0 评论 -
LeetCode *** 1. Two Sum
题目:Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example:Given num原创 2016-04-05 10:27:37 · 204 阅读 · 0 评论 -
LeetCode *** 2. Add Two Numbers
题目: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 as a原创 2016-04-05 11:47:05 · 219 阅读 · 0 评论 -
LeetCode *** 338. Counting Bits
题目:Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For原创 2016-04-05 16:17:18 · 277 阅读 · 0 评论 -
LeetCode *** 26. Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.Do not allocate extra space for another array, you must do this in place原创 2016-04-05 18:52:01 · 144 阅读 · 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.分析:求二叉树的最小深度,其中注意细节:题原创 2016-04-05 19:13:01 · 160 阅读 · 0 评论 -
LeetCode *** 198. House Robber
题目:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacen原创 2016-04-05 21:16:08 · 187 阅读 · 0 评论 -
LeetCode *** 8. String to Integer (atoi)
题目:Implement atoi to convert a string to an integer.分析:题目并没有说清楚具体有哪些string的输入方式,导致错误次数过多。首先就是从正负号出现之前的空格要被去掉。其次是出现正负号之后开始进行转换数,并且紧挨正负号的数/数与数之间不能有任意符号才能被计入,否则切断。最后是int的范围问题,如果string为正数且原创 2016-04-05 21:27:10 · 178 阅读 · 0 评论 -
LeetCode *** 231. Power of Two
题目:Given an integer, write a function to determine if it is a power of two.分析:利用cpp函数log即可,注意当integer==0时的特殊情况。代码:class Solution {public: bool isPowerOfTwo(int n) { if (原创 2016-04-06 08:07:01 · 199 阅读 · 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 the sq原创 2016-04-06 08:12:30 · 177 阅读 · 0 评论 -
LeetCode *** 53. Maximum Subarray
题目:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,原创 2016-04-06 15:33:25 · 190 阅读 · 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 calling原创 2016-04-06 15:46:20 · 150 阅读 · 0 评论 -
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 with原创 2016-04-06 15:55:10 · 172 阅读 · 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-04-06 16:00:20 · 204 阅读 · 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:You m原创 2016-04-06 17:35:13 · 170 阅读 · 0 评论 -
LeetCode *** 171. Excel Sheet Column Number
题目:Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ...原创 2016-04-06 17:40:07 · 174 阅读 · 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 eleme原创 2016-04-06 19:12:50 · 222 阅读 · 0 评论 -
LeetCode *** 238. Product of Array Except Self
题目:Given an array of n integers where n > 1, nums, return an arrayoutput such that output[i] is equal to the product of all the elements ofnums except nums[i].Solve it without division and in原创 2016-04-06 19:31:08 · 161 阅读 · 0 评论 -
LeetCode *** 13. Roman to Integer
题目:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.分析:首先是对于罗马字母转换成数字:罗马数字I1V5X10L50原创 2016-04-06 20:03:43 · 204 阅读 · 0 评论 -
LeetCode *** 206. Reverse Linked List
题目:Reverse a singly linked list.分析:反转链表。设置一个tail链表,对于所求链表的每个到来的节点都利用next将tail连起来。代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *ne原创 2016-04-06 20:23:23 · 229 阅读 · 0 评论 -
LeetCode *** 129. Sum Root to Leaf Numbers
题目:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number123.Find the to原创 2016-04-06 20:57:15 · 499 阅读 · 0 评论 -
LeetCode *** 169. Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that appearsmore than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority elem原创 2016-04-06 20:59:41 · 201 阅读 · 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 theHamming weight).For example, the 32-bit integer ’11' has binary representation 00原创 2016-04-06 23:30:29 · 191 阅读 · 0 评论 -
LeetCode *** 83. Remove Duplicates from Sorted List
题目:Given a sorted linked list, delete all duplicates such that each element appear onlyonce. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 分析:由于已经排序好了,所原创 2016-04-06 23:33:30 · 197 阅读 · 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 defined原创 2016-04-06 23:41:30 · 204 阅读 · 0 评论 -
LeetCode *** 70. Climbing Stairs
题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?分析:爬楼梯问题,一开始用了递归,原创 2016-04-06 23:56:46 · 189 阅读 · 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 not ugly s原创 2016-04-06 23:59:51 · 232 阅读 · 0 评论 -
LeetCode *** 319. Bulb Switcher
题目:There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning原创 2016-04-07 08:17:28 · 196 阅读 · 0 评论 -
LeetCode *** 268. Missing Number
题目:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm sho原创 2016-04-07 08:28:30 · 181 阅读 · 0 评论 -
LeetCode *** 144. 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 so原创 2016-04-07 08:47:47 · 200 阅读 · 0 评论 -
LeetCode *** 228. Summary Ranges
题目:Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].分析:我觉得主要是在于把int转换成string型,同时还有就是最末位的处理,其原创 2016-04-07 21:42:09 · 214 阅读 · 0 评论 -
LeetCode *** 227. Basic Calculator II
题目:Implement a basic calculator to evaluate a simple expression string.The expression string contains only non-negative integers, +, -, *, / operators and empty spaces. The integer division原创 2016-04-07 22:41:34 · 209 阅读 · 0 评论 -
LeetCode *** 86. Partition List
题目:Given a linked list and a value x, partition it such that all nodes less thanx come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in原创 2016-04-07 23:19:17 · 219 阅读 · 0 评论 -
LeetCode *** 337. House Robber III
题目:The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a原创 2016-04-12 08:01:05 · 216 阅读 · 0 评论 -
LeetCode *** 122. Best Time to Buy and Sell Stock II
题目:Say you have an array for which the ith element is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie,原创 2016-04-12 08:55:52 · 267 阅读 · 0 评论 -
LeetCode *** 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? 分析:一开始用了log(n)/log(3),但是在n=243的时候出现了错误,于是用了一个小原创 2016-04-08 09:38:35 · 219 阅读 · 0 评论 -
LeetCode *** 21. Merge Two Sorted Lists
题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.分析:连接两个链表。分别比较链表大小即可。代码:/** * Definit原创 2016-04-08 09:46:05 · 170 阅读 · 0 评论