- 博客(43)
- 收藏
- 关注
原创 86. Partition List
Problem: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 no
2017-09-30 16:43:42 167
原创 92. Reverse Linked List II
Problem: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,
2017-09-29 18:49:43 138
原创 206. Reverse Linked List
Problem:Reverse a singly linked list.题目就一行,翻转一个链表。所以题目也是比较简单,我用了递归的方法实现。Code:(LeetCode运行6ms)/** * Definition for singly-linked list. * struct ListNode { * int val; * List
2017-09-29 17:53:18 205
原创 2. Add Two Numbers
Problem:You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two number
2017-09-29 16:58:29 193
原创 136. Single Number
Problem:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it wit
2017-09-28 19:13:36 184
原创 135. Candy
Problem:There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have a
2017-09-28 18:48:57 205
原创 134. Gas Station
Problem:There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from s
2017-09-27 19:16:18 176
原创 73. Set Matrix Zeroes
Problem:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up:Did you use extra space?A straight forward solution using O(mn) space is pr
2017-09-26 19:16:28 269
原创 89. Gray Code
Problem:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print th
2017-09-26 18:24:33 169
原创 48. Rotate Image
Problem:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the
2017-09-25 16:03:08 148
原创 104. Maximum Depth of Binary Tree
Problem: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.题意是求二叉树的最大深度。有两种方法,第
2017-09-25 08:45:31 188
原创 42. Trapping Rain Water
Problem:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,
2017-09-24 13:44:44 203
原创 36. Valid Sudoku
Problem:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A p
2017-09-23 16:01:09 188
原创 60. Permutation Sequence
Problem: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""13
2017-09-22 21:31:18 301
原创 31. Next Permutation
Problem: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 lowes
2017-09-20 19:47:08 195
原创 3Sum Closest
Problem: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 ha
2017-09-19 17:12:04 333
原创 15.3Sum
class Solution {public: vector> threeSum(vector& nums) { vector> result; if (nums.size() < 3) { return result; } const int target = 0;
2017-09-18 16:58:45 365
原创 128. Longest Consecutive Sequence
Problem: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 i
2017-09-16 11:34:26 192
原创 4. Median of Two Sorted Arrays
Problem:There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1:
2017-09-14 11:01:15 202
原创 81. Search in Rotated Sorted Array II
Problem: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).Write a function to determine if a give
2017-09-13 19:25:59 163
原创 33. Search in Rotated Sorted Array
Problem: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.
2017-09-13 19:09:44 172
原创 80. Remove Duplicates from Sorted Array II
Problem:Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with th
2017-09-12 16:51:12 164
原创 101. Symmetric Tree
Problem: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 / \ / \
2017-09-11 16:59:47 218
原创 100. Same Tree
Problem: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.
2017-09-11 16:26:54 183
原创 88. Merge Sorted Array
Problem: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-09-11 08:34:33 152
原创 83. Remove Duplicates from Sorted List
Problem:iven 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.题意是删除链表中每个节点
2017-09-10 14:17:51 172
原创 70. Climbing Stairs
Problem: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
2017-09-10 13:09:04 237
原创 69. Sqrt(x)
Problem:Implement int sqrt(int x).Compute and return the square root of x.题意是要我们自己写一个开方的函数。我一开始写直接用很朴素的算法,从count=0开始,每次+1,暴力算法。结果是对的,但是结果是超时了。后来我就开始想其他办法,后来看到处理的是int型的,MAX_INT的开方是46340,所
2017-09-10 01:58:28 270
原创 67. Add Binary
Problem:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".题意是给两个二进制的string,然后写一个函数返回两个string对应的二进制值的和。我的想法是:首先两个字符串长度不一样,所以得
2017-09-10 00:36:23 261
原创 66. Plus One
Problem: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.
2017-09-08 19:44:02 239
原创 58. Length of Last Word
Problem: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
2017-09-08 19:15:39 130
原创 53. Maximum Subarray
Problem: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-09-08 18:44:27 171
原创 35. Search Insert Position
Problem: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
2017-09-08 01:05:06 214
原创 28. Implement strStr()
Problem:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.这道题说简单,说难可能也会有一点点难度,如果case特别挑剔的话,可能会超时,需要用到KMP算法。我先写了一
2017-09-08 00:55:01 274
原创 27. Remove Element
Problem: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 m
2017-09-07 23:29:54 144
原创 26. Remove Duplicates from Sorted Array
Problem: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
2017-09-07 19:31:02 227
原创 21. Merge Two Sorted Lists
Problem: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.题目很简单,就是直接把两个已经排列的链表进行合并。吐槽一下题目真的不严谨,升序还是
2017-09-07 19:07:28 167
原创 20. Valid Parentheses
Problem: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
2017-09-07 12:59:06 179
原创 14. Longest Common Prefix
Problem:Write a function to find the longest common prefix string amongst an array of strings.Examples:input: ["ab", "abc"], output: "ab"input: [""], output: ""这道题有几个坑,我一开始都踩进去了,就是
2017-09-07 12:29:27 144
原创 13. Roman to Integer
Problems:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.Example:Ⅶ,7XCIII 93题目大意是把罗马数字转换成阿拉伯数字。(吐槽一下题目应该给一些罗马数字的表示
2017-09-06 23:18:36 185
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人