leetcode第二题26题怎么做

用户名:wcyz666
访问量:228
注册日期:
阅读量:1297
阅读量:3317
阅读量:580916
阅读量:464585
51CTO推荐博文
LeetCode26:给定一个有序序列,求不同的元素个数并且返回不同序列,要求原地返回,O(1)空间(26, easy)15分钟,第一次就AC了略开心,最好记录406ms貌似是前1%!虽然这个时间不靠谱没啥可优化的了,感觉几乎没有废代码public&int&removeDuplicates(int[]&A)&{
&&&&int&i&=&1,&j&=&0;
&&&&for&(;&i&&&A.&i++){
&&&&&&&&if&(A[i]&!=&A[j])&{
&&&&&&&&&&&&j++;
&&&&&&&&&&&&if&(i&!=&j)
&&&&&&&&&&&&&&&&A[j]&=&A[i];
&&&&return&j&+&1;
}经验?:真的会有公司考这么简单?&括号匹配。(20, easy)最好记录430ms,前10%。稍微用了点小聪明,不过不好(使用异常做判断)经验8:使用Stack比使用数组效率高很多,对这个题而言public&boolean&isValid(String&s)&
&&&&if&(s.length()&%&2&==&1)
&&&&&&&&return&
&&&&Stack&Character&&sCharacters&=&new&Stack&&();
&&&&sCharacters.push(s.charAt(0));
&&&&for&(int&i&=&1;&i&&&s.length();&i++){
&&&&&&&&switch&(s.charAt(i))&{
&&&&&&&&case&'(':
&&&&&&&&case&'{':
&&&&&&&&case&'[':
&&&&&&&&&&&&sCharacters.push(s.charAt(i));
&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&case&')':
&&&&&&&&case&']':
&&&&&&&&case&'}':
&&&&&&&&&&&&try&{
&&&&&&&&&&&&&&&&char&c&=&sCharacters.pop();
&&&&&&&&&&&&&&&&if&(s.charAt(i)&-&c&&&2)
&&&&&&&&&&&&&&&&&&&&return&
&&&&&&&&&&&&}&catch&(Exception&e)&{
&&&&&&&&&&&&&&&&//&TODO:&handle&exception
&&&&&&&&&&&&&&&&return&
&&&&&&&&&&&&}
&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&default:
&&&&&&&&&&&&return&
&&&&return&sCharacters.isEmpty();
}经验?:真的会有公司考这么简单?&判断一个数是不是回文数,不能用额外空间(这点好奇怪,不用额外空间连循环都没法跑了)(9, easy)(投机取巧法)public&boolean&isPalindrome(int&x)&{
&&&&if&(x&&&0)
&&&&&&&&return&
&&&&StringBuilder&stringBuilder&=&new&StringBuilder(x&+&"");
&&&&if&(stringBuilder.reverse().toString().equals(x+""))
&&&&&&&&return&
&&&&&&&&return&
}正规做法:少少优化。大概能跑到前50%,最好记录713ms
public&boolean&isPalindrome(int&x)&{
&&&&//negative&numbers&are&not&palindrome
&&&&if&(x&&&0)
&&&&&&&&return&
&&&&//&initialize&how&many&zeros
&&&&int&div&=&1;
&&&&while&(x&/&div&&=&10)&{
&&&&&&&&div&*=&10;
&&&&while&(x&!=&0)&{
&&&&&&&&if&(x&/&div&!=&x&%&10)
&&&&&&&&&&&&return&
&&&&&&&&x&=&(x&%&div)&/&10;
&&&&&&&&div&/=&100;
&&&&return&
}经验?:真的会有公司考这么简单?经验??:话说LeetCode的虚拟机性能优化过啊。我半年前跑713,现在跑了412本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:未分类┆阅读(0)┆评论(0)LeetCode算法题目解答汇总 | 四火的唠叨
LeetCode算法题目解答汇总
[Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于tricky的方法。我没有为了这个再更新,就让它们去吧。请读者知道这一点。
只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少。我的初衷就是练习,因为一方面我本身算法基础并不好,再一方面是因为工作以后传统意义上所谓算法的东西接触还是太少。为了题目查找方便起见,我把之前几篇陆陆续续贴出来的我对LeetCode上面算法题的解答汇总在下面,CTRL+F就可以比较方便地找到。由于LeetCode上的题在不断更新,因此我也会不定期地更新。下面表格里面的Acceptance和Difficulty的Easy/Medium/Hard的分类都是来自LeetCode上面的数据,而Difficulty括号里面的数值和Frequency则来自。另外,LeetCode数据库的那十道题(截止到目前)我。
总体来说,做LeetCode的题目收获还是很大的。而这种可以很快获得代码正确性和性能即使的反馈的方式,对算法提高也是很有帮助的。有的题目困难,主要包括两方面的原因,有的是思路比较怪异,用常规思路去解题往往时间复杂度或者空间复杂度过高,还有的则是需要考虑的情况比较复杂,case众多,很容易遗漏。
Medium (4)
Medium (3)
Medium (3)
Medium (3)
Medium (3)
Medium (3)
Medium (4)
Medium (3)
Medium (3)
Medium (3)
Medium (3)
Medium (3)
Medium (3)
Medium (3)
Medium (3)
Medium (2)
Medium (3)
Medium (4)
Medium (3)
Medium (2)
Medium (4)
Medium (3)
Medium (2)
Medium (4)
Medium (3)
Medium (3)
Medium (3)
Medium (2)
Medium (2)
Medium (3)
Medium (3)
Medium (4)
Medium (4)
Medium (2)
Medium (4)
Medium (5)
Medium (3)
Medium (3)
Medium (3)
Medium (3)
Medium (3)
Medium (4)
Medium (2)
Medium (5)
Medium (3)
Medium (4)
Medium (3)
Medium (3)
Medium (3)
Medium (3)
Medium (4)
Medium (2)
Medium (4)
Medium (3)
Medium (4)
Medium (3)
Medium (3)
Medium (3)
Medium (4)
Medium (3)
Medium (4)
Medium (3)
Medium (3)
Medium (4)
Medium (2)
Medium (5)
【】从上次贴出来我的解答以后,最新更新的题目,除了一些需要买书只能在电子书上面看解答的没法验证以外,凡是题目能够在线解答和验证答案的,我把我的全部和分析放在了下面。欢迎指正。
【Updated:12/15/到310题
【Updated:1/19/到371题
文章未经特殊标明皆为本人原创,未经许可不得用于任何商业用途,转载请保持完整性并注明来源链接
真的要好好感谢一下楼主!多谢多谢!26. Remove Duplicates from Sorted Array
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 place with constant memory.
For example,
Given input array&nums&=&[1,1,2],
Your function should return length =&2, with the first two elements of&nums&being&1&and&2&respectively.
It doesn't matter what you leave beyond the new length.
26.从有序数组中移除重复元素
给定一个有序数组,就地移除重复元素使得每个元素只出现一次并且返回新的数组长度。
不要分配额外的内存来创建另外的数组,你必须使用固定内存就地完成。
给定数组nums=[1,1,2]
你的函数应该返回length=2,而且nums的前两个元素分别应该是1和2。超过新数组长度的部分你就不用管了。
思路:因为是有序数组,所以每遇到一个新值时暂存然后继续向后遍历,如果等于这个值就跳过,不等于时就要更新新值。所有的新值依次保存在原数组中,因为新值的数量肯定小于等于原数组元素的数组,所以这样覆盖保存并不会出问题。
int removeDuplicates(int* nums, int numsSize) {
if (numsSize == 0) return 0;
int len = numsS
int index = 1;
int t = nums[0];
for (int i = 1; i & numsS i++){
if (nums[i] != t){
nums[index++] = nums[i];
t = nums[i];
class Solution(object):
def removeDuplicates(self, nums):
:type nums: List[int]
:rtype: int
if not nums: return 0
length,index,t = len(nums),1,nums[0]
for i in nums[1:]:
if i != t:
nums[index] = i
index += 1
length -= 1
return length
本文已收录于以下专栏:
相关文章推荐
292. Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones o...
100. Same Tree
Given two binary trees, write a function to check if they are equal or not.
Given an array of integers, return indices of the two numbers such that they add up to...
70. Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n resp...
24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head....
21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list ...
36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
223. Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane.
110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.
For this ...
他的最新文章
讲师:王哲涵
讲师:韦玮
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
相关代码已经上传到github:
文中代码为了不动官网提供的初始几行代码内容,有一些不规范的地方,比如函数名大小写问题等等;更合理的代码实现参考我的github repo
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 place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn&t matter what you leave beyond the new length.
一个有序数字,要求去掉重复的元素然后返回新的长度,不要申请额外空间。
举个例子,给定数组:[1,1,2],程序需要处理成[1,2,…]然后返回2,也就是前2个元素是去重之后得到的结果,超过2的部分是什么无所谓,是1也行是2也行。
这类题想明白了其实解题很简单,重复的需要去掉,无非就是遍历数组,发现重复,就把后面的往前移,把重复值覆盖掉。具体说,可以维护2个指针,慢指针开始指向数组第一个元素,快指针指向第二个元素,然后快指针不断判断自己当前元素和前一个元素是否相同,相同则快指针后移,不相同则将当前值赋值给慢指针的后一个元素,慢指针后移。最后慢指针指向的元素及前面所有元素都是不重复的。具体过程参考如下代码和注释:
class Solution(object):
def removeDuplicates(self, nums):
:type nums: List[int]
:rtype: int
# 删除列表中的重复项,返回操作后的长度
# [1,1,1,2,3,4,4,4,5] -& [1,2,3,4,5] 5
# 维护2个索引,慢的s,快的f;s指向第一个元素,f的指向第二个元素;
# 判断f和f前一个元素是否相等,相等则f后移;不等则s后移一个,值给s,然后f也后移
if len(nums) &= 1:
return len(nums)
for f in range(1, len(nums)):
if nums[s] != nums[f]:
nums[s] = nums[f]
return s + 1
PHP开发框架
开发工具/编程工具
服务器环境随笔分类 - leetcode每题整理
摘要: 题目:Suppose a sorted array 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).Find the minimum elemen...
爱做饭的小莹子 阅读(2234) |
摘要: 题目:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique...
爱做饭的小莹子 阅读(3309) |
摘要: 题目: 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 be...
爱做饭的小莹子 阅读(5592) |
摘要: 题目:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.题解:其实我觉得这题。。为啥不给个更明确...
爱做饭的小莹子 阅读(6392) |
摘要: 题目:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not pos...
爱做饭的小莹子 阅读(3623) |
摘要: 题目: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 seque...
爱做饭的小莹子 阅读(3922) |
摘要: 题目:Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You ...
爱做饭的小莹子 阅读(3868) |
摘要: 题目:Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You ha...
爱做饭的小莹子 阅读(3776) |
摘要: 题目:Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible represent...
爱做饭的小莹子 阅读(2112) |
摘要: 题目:A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -& 1'B' -& 2...'Z' -& 26Given an encoded message...
爱做饭的小莹子 阅读(4561) |
摘要: 题目:Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = &aabcc&,s2 = &dbbca&,When s3 = &aadbbcbcac&, r...
爱做饭的小莹子 阅读(1881) |
摘要: 题目:Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed fro...
爱做饭的小莹子 阅读(4079) |
摘要: 题目:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.题解:这道题就是给你一个2D平面,然后给你的数据结构是由横纵坐标表示的点,然后看哪条直线上的点...
爱做饭的小莹子 阅读(1981) |
摘要: 题目:Validate if a given string is numeric.Some examples:&0& =& true& 0.1 & =& true&abc& =& false&1 a& =& false&2e10& =& trueNote: It is intended fo...
爱做饭的小莹子 阅读(2700) |
摘要: 题目:Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (includin...
爱做饭的小莹子 阅读(2072) |
摘要: 题目:Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding elem...
爱做饭的小莹子 阅读(3382) |
摘要: 题目:Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can b...
爱做饭的小莹子 阅读(2870) |
摘要: 题目:Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter...
爱做饭的小莹子 阅读(3100) |
摘要: 题目:Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partition...
爱做饭的小莹子 阅读(3924) |
摘要: 题目: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 ...
爱做饭的小莹子 阅读(2132) |
摘要: 题目: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).F...
爱做饭的小莹子 阅读(1310) |
摘要: 题目: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...
爱做饭的小莹子 阅读(2098) |
摘要: 题目:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space...
爱做饭的小莹子 阅读(2026) |
摘要: 题目:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains on...
爱做饭的小莹子 阅读(2320) |
摘要: 题目:Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-...
爱做饭的小莹子 阅读(3877) |
摘要: 题目:Follow up for &Remove Duplicates&:What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function shou...
爱做饭的小莹子 阅读(1042) |
摘要: 题目: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...
爱做饭的小莹子 阅读(1627) |
摘要: 题目: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 f...
爱做饭的小莹子 阅读(8650) |
摘要: 题目:Follow up for problem &Populating Next Right Pointers in Each Node&.What if the given tree could be any binary tree? Would your previous solution ...
爱做饭的小莹子 阅读(1097) |
摘要: 题目:Given two binary strings, return their sum (also a binary string).For example,a = &11&b = &1&Return &100&.题解:二进制加法都是从最低位(从右加到左)。所以对两个字符串要从最后一位开始加,...
爱做饭的小莹子 阅读(3438) |
摘要: 题目: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 ...
爱做饭的小莹子 阅读(4037) |
摘要: 题目:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, , ...1 is read off as &one 1& or 11.11 is read ...
爱做饭的小莹子 阅读(3769) |
摘要: 题目:Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes...
爱做饭的小莹子 阅读(2542) |
摘要: 题目:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique lo...
爱做饭的小莹子 阅读(3832) |
摘要: 题目:Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,...
爱做饭的小莹子 阅读(3562) |
摘要: 题目: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 c...
爱做饭的小莹子 阅读(1534) |
摘要: 题目:Given an input string, reverse the string word by word.For example,Given s = &the sky is blue&,return &blue is sky the&.click to show clarificatio...
爱做饭的小莹子 阅读(931) |
摘要: 题目:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra s...
爱做饭的小莹子 阅读(2371) |
摘要: 题目:Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit i...
爱做饭的小莹子 阅读(3823) |
摘要: 题目:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to ...
爱做饭的小莹子 阅读(1746) |
摘要: 题目:Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1...
爱做饭的小莹子 阅读(2050) |
摘要: 题目:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the...
爱做饭的小莹子 阅读(2966) |
摘要: 题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the foll...
爱做饭的小莹子 阅读(1478) |
摘要: 题目:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[[ ...
爱做饭的小莹子 阅读(2077) |
摘要: 题目:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its pa...
爱做饭的小莹子 阅读(1160) |
摘要: 题目:Given a binary tree struct TreeLinkNode { TreeLinkNode * TreeLinkNode * TreeLinkNode * }Populate each next po...
爱做饭的小莹子 阅读(1005) |
摘要: 题目:Follow up for &Unique Paths&:Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty spa...
爱做饭的小莹子 阅读(2187) |
摘要: 题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at a...
爱做饭的小莹子 阅读(2607) |
摘要: 题目: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...
爱做饭的小莹子 阅读(3781) |
摘要: 题目:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is...
爱做饭的小莹子 阅读(4563) |
摘要: 题目: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?题解:这道题就是考察很...
爱做饭的小莹子 阅读(2195) |
摘要: 题目:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999. 题解:这道题跟interger to roman一样都得先熟悉罗马数字的规则。罗...
爱做饭的小莹子 阅读(1522) |
摘要: 题目:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.题解:这道题。。还有哪个roman to integer。。第一件事 就是先把r...
爱做饭的小莹子 阅读(3844) |
摘要: 题目:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here...
爱做饭的小莹子 阅读(1619) |
摘要: 题目:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1...
爱做饭的小莹子 阅读(1126) |
摘要: 题目:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given &&,return [...
爱做饭的小莹子 阅读(2769) |
摘要: 题目:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each ...
爱做饭的小莹子 阅读(1868) |
摘要: 题目:Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same re...
爱做饭的小莹子 阅读(3619) |
摘要: 题目:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will ...
爱做饭的小莹子 阅读(2921) |
摘要: 题目:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled ...
爱做饭的小莹子 阅读(3047) |
摘要: 题目:Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For exam...
爱做饭的小莹子 阅读(2132) |
摘要: 题目:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,&A man, a plan, a canal: ...
爱做饭的小莹子 阅读(2418) |
摘要: 题目: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 w...
爱做饭的小莹子 阅读(1166) |
摘要: 题目:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.题解:这道题跟pre...
爱做饭的小莹子 阅读(699) |
摘要: 题目:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.题解: 1 ...
爱做饭的小莹子 阅读(2241) |
摘要: 题目:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.题解:之前做过一道是从sorted array转换到BinarySearc...
爱做饭的小莹子 阅读(2083) |
摘要: 题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return...
爱做饭的小莹子 阅读(3248) |
摘要: 题目:Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique B...
爱做饭的小莹子 阅读(2771) |
摘要: 题目:Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where &ad...
爱做饭的小莹子 阅读(3410) |
摘要: 题目:Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending o...
爱做饭的小莹子 阅读(2130) |
摘要: 题目:Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must n...
爱做饭的小莹子 阅读(3057) |
摘要: 题目:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the tele...
爱做饭的小莹子 阅读(1843) |
摘要: 题目: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 ...
爱做饭的小莹子 阅读(1946) |
摘要: 题目: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 su...
爱做饭的小莹子 阅读(2035) |
摘要: 题目: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 d...
爱做饭的小莹子 阅读(1174) |
摘要: 题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题解:先复习下什么是二叉搜索树(引自Wikipedia):二叉查找树(Binary Search ...
爱做饭的小莹子 阅读(1493) |
摘要: 题目: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 neares...
爱做饭的小莹子 阅读(2006) |
摘要: 题目: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 farthes...
爱做饭的小莹子 阅读(1508) |
摘要: 题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 /...
爱做饭的小莹子 阅读(2255) |
摘要: 题目: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 identica...
爱做饭的小莹子 阅读(1404) |
摘要: 题目:Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3...
爱做饭的小莹子 阅读(768) |
摘要: 题目:Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,...
爱做饭的小莹子 阅读(988) |
摘要: 题目:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3...
爱做饭的小莹子 阅读(1523) |
摘要: 题目:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], ...
爱做饭的小莹子 阅读(3104) |
摘要: 题目: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 followin...
爱做饭的小莹子 阅读(902) |
摘要: 题目: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 rain...
爱做饭的小莹子 阅读(2709) |
摘要: 题目:Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may com...
爱做饭的小莹子 阅读(1863) |
摘要: 题目:Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may com...
爱做饭的小莹子 阅读(1504) |
摘要: 题目: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 transact...
爱做饭的小莹子 阅读(1007) |
摘要: 题目: 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...
爱做饭的小莹子 阅读(3861) |
摘要: 题目:Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all suc...
爱做饭的小莹子 阅读(2068) |
摘要: 题目:Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.F...
爱做饭的小莹子 阅读(1201) |
摘要: 题目:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.题解:这道题看所给的字符串数组里面有多少个是同一个变形词变的。这道...
爱做饭的小莹子 阅读(2280) |
摘要: 题目:Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are label...
爱做饭的小莹子 阅读(7210) |
摘要: 题目:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or e...
爱做饭的小莹子 阅读(878) |
摘要: 题目:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = &ADOBE...
爱做饭的小莹子 阅读(3324) |
摘要: 题目:You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a conca...
爱做饭的小莹子 阅读(3440) |
摘要: 题目:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were ini...
爱做饭的小莹子 阅读(1889) |
摘要: 题目:Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].题解:这道题主要...
爱做饭的小莹子 阅读(1576) |
摘要: 题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letter...
爱做饭的小莹子 阅读(1187) |
摘要: 题目: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 w...
爱做饭的小莹子 阅读(1635) |
摘要: 题目:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your ma...
爱做饭的小莹子 阅读(1803) |
摘要: 题目:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your ma...
爱做饭的小莹子 阅读(1477) |
摘要: 题目:Write a function to find the longest common prefix string amongst an array of strings.题解:解题思路是,先对整个String数组预处理一下,求一个最小长度(最长前缀肯定不能大于最小长度)。然后以第0个字符串...
爱做饭的小莹子 阅读(4575) |
摘要: 题目: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 r...
爱做饭的小莹子 阅读(2709) |
摘要: 题目:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't...
爱做饭的小莹子 阅读(1255) |
摘要: 题目: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...
爱做饭的小莹子 阅读(1718) |
摘要: 题目:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two e...
爱做饭的小莹子 阅读(1258) |
摘要: 题目:Divide two integers without using multiplication, division and mod operator.题解:这道题我自己没想出来。。。乘除取模都不让用。。那只有加减了。。。我参考的http://blog.csdn.net/perfect888...
爱做饭的小莹子 阅读(2569) |
摘要: 题目:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.题解:这道题跟NQueens的解法完全一样(具...
爱做饭的小莹子 阅读(1679) |
摘要: 题目:The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return a...
爱做饭的小莹子 阅读(4121) |
摘要: 题目:Implement pow(x, n).题解:pow(x,n)就是求x的n次方。x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2)。所以利用递归求解,当n==1的时候,x^n=x。当然n是可以小于0的,2^(-3) = 1/(2^3)。按照上面那个规律就可以解决了...
爱做饭的小莹子 阅读(3815) |
摘要: 题目:Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtim...
爱做饭的小莹子 阅读(2095) |
摘要: 题目:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime comp...
爱做饭的小莹子 阅读(1522) |
摘要: 题目: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 lon...
爱做饭的小莹子 阅读(1678) |
摘要: 题目:Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surroun...
爱做饭的小莹子 阅读(2321) |
摘要: 题目:Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or anothe...
爱做饭的小莹子 阅读(536) |
摘要: 题目:Given an absolute path for a file (Unix-style), simplify it.For example,path = &/home/&, =& &/home&path = &/a/./b/../../c/&, =& &/c&Corner Cases:D...
爱做饭的小莹子 阅读(4181) |
摘要: 题目:Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For &(()&, the...
爱做饭的小莹子 阅读(1989) |
摘要: 题目:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.题解:这道题可以应用之前解过的Largetst Recta...
爱做饭的小莹子 阅读(1350) |
摘要: 题目:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the ...
爱做饭的小莹子 阅读(1057) |
摘要: 题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in t...
爱做饭的小莹子 阅读(3946) |
摘要: 题目:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get ...
爱做饭的小莹子 阅读(4196) |
摘要: 题目:Sort a linked list in O(n log n) time using constant space complexity.题解:考虑到要求用O(nlogn)的时间复杂度和constant space complexity来sort list,自然而然想到了merge sor...
爱做饭的小莹子 阅读(3105) |
摘要: 题目:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.F...
爱做饭的小莹子 阅读(1177) |
摘要: 题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.题解:Merge k sorted linked list就是merge 2 sorted li...
爱做饭的小莹子 阅读(2272) |
摘要: 题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k the...
爱做饭的小莹子 阅读(1273) |
摘要: 题目: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 sin...
爱做饭的小莹子 阅读(3283) |
摘要: 题目:A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep c...
爱做饭的小莹子 阅读(2659) |
摘要: 题目:Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1-&2-&3-&4-&5-&NULL and k = 2,return 4-&5-&1-&2-...
爱做饭的小莹子 阅读(3169) |
摘要: 题目:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flatten...
爱做饭的小莹子 阅读(2868) |
摘要: 题目: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-&...
爱做饭的小莹子 阅读(1424) |
摘要: 题目:Sort a linked list using insertion sort.题解:Insertion Sort就是把一个一个元素往已排好序的list中插入的过程。初始时,sorted list是空,把一个元素插入sorted list中。然后,在每一次插入过程中,都是找到最合适位置进行插...
爱做饭的小莹子 阅读(2935) |
摘要: 题目: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 t...
爱做饭的小莹子 阅读(2829) |
摘要: 题目:Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1-&2-&3-&4-&5, and n = 2. Af...
爱做饭的小莹子 阅读(2869) |
摘要: 题目:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra spac...
爱做饭的小莹子 阅读(1082) |
摘要: 题目:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?题解:这道题连带着II是很经典的,在看CC150时候,纠结这个问题纠结了很久...
爱做饭的小莹子 阅读(887) |
摘要: 题目:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1...
爱做饭的小莹子 阅读(1728) |
摘要: 题目: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-...
爱做饭的小莹子 阅读(534) |
摘要: 题目: 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. 题解:...
爱做饭的小莹子 阅读(2184) |
摘要: 题目:Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1-&2-&3-&4, you should return the list as 2-&1-&4-&3.http...
爱做饭的小莹子 阅读(3855) |
摘要: 题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should ...
爱做饭的小莹子 阅读(6881) |
摘要: 题目: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 integ...
爱做饭的小莹子 阅读(2690) |
摘要: 题目: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 w...
爱做饭的小莹子 阅读(2035) |
摘要: 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum...
爱做饭的小莹子 阅读(3438) |
摘要: 题目:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the tw...
爱做饭的小莹子 阅读(4314) |
摘要: 题目:Follow up for &Search in Rotated Sorted Array&:What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a funct...
爱做饭的小莹子 阅读(1735) |
摘要: 题目:Suppose a sorted array 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 ...
爱做饭的小莹子 阅读(1703) |
摘要: 题目:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorte...
爱做饭的小莹子 阅读(2625) |
摘要: Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735题目:Implement int sqrt(int x).Compute and return the square root of x.题解: 这道题很巧妙的运用了二...
爱做饭的小莹子 阅读(1914) |
摘要: 题目:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the...
爱做饭的小莹子 阅读(3016) |
摘要: 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 or...
爱做饭的小莹子 阅读(1265) |

我要回帖

更多关于 leetcode第一题 的文章

 

随机推荐