
LeetCode
算法学习之地
薛离子
假如我年少有为不自卑
展开
-
LeetCode 50. Pow(x, n)
描述 Implement pow(x, n).分析 二分法,xn = xn/2 × xn/2 × xn%2代码class Solution {public: double myPow(double x, int n) { if (n < 0) return 1.0 / power(x, -n); else return power(x, n);原创 2017-02-07 00:41:36 · 393 阅读 · 0 评论 -
LeetCode 342. Power of Four
描述 Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example: Given num = 16, return true. Given num = 5, return false.Follow up: Could you solve it without loop原创 2017-02-07 00:33:15 · 360 阅读 · 0 评论 -
LeetCode 231. Power of Two
描述 Given an integer, write a function to determine if it is a power of two.分析 如果是power of two, 则2进制表达中,有且仅有一个1. 可以通过移位来数1的个数, 这里用了一个巧妙的办法, 即判断 N & (N-1) 是否为0. 代码class Solution {public: bool i原创 2017-02-07 00:17:37 · 220 阅读 · 0 评论 -
LeetCode 75. Sort Colors
描述 Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0原创 2017-02-06 23:52:37 · 330 阅读 · 0 评论 -
LeetCode 387. First Unique Character in a String
描述 Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.Note: You may assume原创 2017-02-06 23:24:32 · 289 阅读 · 0 评论 -
LeetCode 41. First Missing Positive
描述 Given an unsorted integer array, find the first missing positive integer.For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant spac原创 2017-02-06 23:05:45 · 303 阅读 · 0 评论 -
LeetCode 18. 4Sum
描述 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: The solution se原创 2017-02-06 22:22:34 · 371 阅读 · 0 评论 -
LeetCode 16. 3Sum Closest
描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exac原创 2017-02-06 21:05:32 · 304 阅读 · 0 评论 -
LeetCode 344. Reverse String
描述 Write a function that takes a string as input and returns the string reversed.Example: Given s = “hello”, return “olleh”.分析 翻转字符串。 取字符串长度的一半进行循环,首尾交换。代码class Solution {public: string revers原创 2017-02-05 23:04:01 · 307 阅读 · 0 评论 -
LeetCode 58. Length of Last Word
描述 Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Note: A word is define原创 2017-02-05 22:54:47 · 319 阅读 · 0 评论 -
LeetCode 67. Add Binary
描述 Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” Return “100”.分析 翻转a和b,逐位相加求值,结果存入string中。 注意最高位的情况。代码class Solution {public: string addBina原创 2017-02-05 22:38:03 · 284 阅读 · 0 评论 -
LeetCode 12. Integer to Roman
描述 Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.分析 整数转换为罗马数字。 此题因为题目中明确了范围1到3999,所以用每位对应一个数组的方式较为简单。附罗马数字的规则:罗马数字共有7个,即I(1)、V(5)、X(10)、L(原创 2017-02-05 22:20:34 · 327 阅读 · 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.分析 罗马数字转换为整数。附罗马数字的规则:罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。 1、重复数次:一个罗马数原创 2017-02-05 22:07:43 · 309 阅读 · 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 car” is not a p原创 2017-02-05 19:28:44 · 275 阅读 · 0 评论 -
LeetCode 48. Rotate Image
描述 You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up: Could you do this in-place?分析 首先想到,纯模拟,从外到内一圈一圈的转,但这个方法太慢。 如下图,首先沿着副对角线翻转一次,然后沿着水平中原创 2017-02-05 18:24:51 · 297 阅读 · 0 评论 -
LeetCode 60. Permutation Sequence
描述 The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3):“123” “132” “213” “231” “原创 2017-02-05 17:55:56 · 331 阅读 · 0 评论 -
LeetCode 46. Permutations
描述 Given a collection of distinct numbers, return all possible permutations.For example, [1,2,3] have the following permutations:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]分析原创 2017-02-05 17:29:53 · 321 阅读 · 0 评论 -
LeetCode 31. Next Permutation
描述 Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2017-02-05 16:42:36 · 686 阅读 · 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?Note: Given n will be a positive in原创 2017-02-05 12:14:48 · 410 阅读 · 0 评论 -
LeetCode 33. Search in Rotated Sorted Array
描述 Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in th原创 2017-02-03 23:48:26 · 261 阅读 · 0 评论 -
LeetCode 81. Search in Rotated Sorted Array II
描述Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose an array sorted in ascending order is rotated at some piv原创 2017-02-04 00:00:32 · 285 阅读 · 0 评论 -
LeetCode 128. Longest Consecutive Sequence
描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3,原创 2017-02-03 23:31:41 · 436 阅读 · 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 nums = [2, 7,原创 2017-01-21 10:01:50 · 299 阅读 · 0 评论 -
LeetCode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consecutive 1s.原创 2017-01-20 22:02:23 · 620 阅读 · 0 评论 -
LeetCode 6. ZigZag Conversion
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H NA P L S I I G原创 2017-01-20 15:49:16 · 271 阅读 · 0 评论 -
LeetCode 9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.分析: 1. 如果将数字转化为字符串,再判断是否为回文字符串,要使用额外空间,不满足要求。 2. 考虑将数字翻转,(LeetCode 7. Reverse Integer )[http://blog.youkuaiyun.com/teffi/article/d原创 2017-01-20 14:19:06 · 246 阅读 · 0 评论 -
LeetCode 8. String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.N原创 2017-01-20 10:57:41 · 448 阅读 · 0 评论 -
LeetCode 7. Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321分析:注意溢出情况。class Solution {public: int reverse(int x) { int digit = 0; int sign = 1;原创 2017-01-19 22:33:07 · 352 阅读 · 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 linked list.原创 2016-12-13 15:15:17 · 294 阅读 · 0 评论 -
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 solution is trivial, could y原创 2016-12-27 19:14:17 · 257 阅读 · 0 评论 -
450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.Basically, the deletion can be divided into t原创 2016-12-27 19:06:57 · 281 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,3,2].Note: Recursive solution is trivial, could原创 2016-12-27 19:16:04 · 235 阅读 · 0 评论 -
145. 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 solution is trivial, could原创 2016-12-27 19:17:27 · 258 阅读 · 0 评论 -
437. Path Sum III
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it must go原创 2016-12-27 20:10:38 · 260 阅读 · 0 评论 -
112. Path Sum
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 and sum =原创 2016-12-27 20:28:06 · 326 阅读 · 0 评论 -
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 between two原创 2016-12-28 09:21:17 · 224 阅读 · 0 评论 -
113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \原创 2016-12-28 09:24:15 · 287 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and原创 2016-12-28 09:26:35 · 223 阅读 · 0 评论 -
404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree.Example: 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.LeetCod原创 2016-12-28 09:30:25 · 226 阅读 · 0 评论 -
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"]LeetCode AC代码:/** * Definition原创 2016-12-28 09:47:04 · 251 阅读 · 0 评论