
LeetCode
holy_hao
一步一码。
展开
-
Easy 12 Count and Say(53)
Description 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,原创 2017-04-18 10:03:29 · 271 阅读 · 0 评论 -
Easy 7 Remove Duplicates from Sorted Array(26)
Description Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in p原创 2017-04-16 15:47:30 · 194 阅读 · 0 评论 -
Easy 8 Remove Element(27)
Description Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant me原创 2017-04-16 15:52:48 · 257 阅读 · 0 评论 -
Easy 9 Implement strStr()(28)
Description Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Solution 暴力法。class Solution { public: int strStr(string haystack, string ne原创 2017-04-17 09:35:13 · 238 阅读 · 0 评论 -
Easy 10 Search Insert Position(35)
Description Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in th原创 2017-04-17 09:47:45 · 209 阅读 · 0 评论 -
Easy 11 Count and Say(38)
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, … 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off原创 2017-04-17 10:32:49 · 329 阅读 · 0 评论 -
Easy 18 Remove Duplicates from Sorted List(83)
Description 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.Solution 逐项遍历,删除原创 2017-04-21 10:08:20 · 237 阅读 · 0 评论 -
Easy 19 Merge Sorted Array(88)
Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to原创 2017-04-21 11:02:48 · 267 阅读 · 0 评论 -
Easy 20 Same Tree(100)
Description 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.Solu原创 2017-04-24 16:52:27 · 256 阅读 · 0 评论 -
Easy 6 Merge Two Sorted Lists(21)
Description 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.Solution 将两个有序链表合并成一个链表。(定义一个头结点,方便返回)/** * D原创 2017-04-16 15:34:17 · 200 阅读 · 0 评论 -
Easy 5 Valid Parentheses(20)
Description 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 a原创 2017-04-15 20:51:47 · 374 阅读 · 0 评论 -
Easy 4 Roman to Integer(13)
Description Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.Solution 这个题只要知道转换规则,遍历一遍字符串就行。class Solution { public: int romanToInt(stri原创 2017-04-15 20:39:50 · 402 阅读 · 0 评论 -
Easy 13 Length of Last Word(58)
Description 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 w原创 2017-04-18 11:22:51 · 239 阅读 · 0 评论 -
Easy 14 Plus One(66)
Description Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any leading zero, except the number 0 itself. Th原创 2017-04-18 15:01:32 · 300 阅读 · 0 评论 -
Easy 15 Add Binary(67)
Description Given two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”. Solutionclass Solution { public: string addBinary(string a, string b原创 2017-04-18 15:15:34 · 331 阅读 · 0 评论 -
Easy 16 Sqrt(x)(69)
Description Implement int sqrt(int x). Compute and return the square root of x. Solution 返回整型的平方根,可以二分查找0-x。当然,还可以用牛顿迭代法求平方根,可以到任意精度。class Solution { public: int mySqrt(int x) { double n原创 2017-04-18 16:14:07 · 273 阅读 · 0 评论 -
Easy 17 Climbing Stairs(70)
Description 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?Solution 简单的动态规划的题目,d原创 2017-04-18 16:29:48 · 269 阅读 · 0 评论 -
Easy 1 Two Sum(1)
Description 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, and you may not us原创 2017-04-15 17:20:06 · 174 阅读 · 0 评论 -
Easy 2 Reverse Integer(7)
Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321click to show spoilers.Note: The input is assumed to be a 32-bit signed integer. Your function should return原创 2017-04-15 17:32:24 · 290 阅读 · 0 评论 -
Easy 3 Palindrome Number(9)
Description Determine whether an integer is a palindrome. Do this without extra space.Solution 判断一个整型数是否为回文。如果将integer全翻转,可能会导致整数溢出。但这并不影响判断,因为若是回文,则翻转必不会溢出。class Solution { public: bool isPalind原创 2017-04-15 20:34:28 · 175 阅读 · 0 评论 -
Easy 21 Symmetric Tree(101)
Description 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 / \ / \ 3 4原创 2017-04-24 17:03:40 · 331 阅读 · 0 评论