- 博客(17)
- 收藏
- 关注
原创 89. Gray Code #Medium
leetcode 89. Gray Code #Medium 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
2016-06-08 10:46:31 324
原创 53. Maximum Subarray #Medium
leetcode 53. Maximum Subarray #Medium 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
2016-06-06 09:26:05 281
转载 287. Find the Duplicate Number #Hard
leetcode 287. Find the Duplicate Number #Hard Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assu
2016-06-06 08:35:51 280
原创 35. Search Insert Position #Medium
leetcode 35. Search Insert Position #Medium题意:给定一个升序数组和一个数字,如果这个数字出现在数组中,则求出它的位置(数组下标);否则,在保证数组有序的条件下,应该将数字插入到数组的哪个位置?(假定数组元素不重复)例子:[1,3,5,6], 5 → 2 [1,3,5,6], 2 → 1 [1,3,5,6], 7 → 4 [
2016-06-02 16:43:11 263
原创 108. Convert Sorted Array to Binary Search Tree #Medium
leetcode 108. Convert Sorted Array to Binary Search Tree #Medium题意:给定升序排列的数组,将其转换为BST二叉搜索树分析:在数组区间[0,n-1]首先取数组中间元素mid=(n-1)/2作为根,从而得到两个子区间,取左边子区间[0,mid-1]中间元素根的左子树的根,取右边子区间[mid+1,n-1]的中间元素作为根的右子
2016-05-25 14:59:30 343
原创 328. Odd Even Linked List #Medium
leetcode 328. Odd Even Linked List #Medium题意:给定一个有n个节点的单链表,调整节点位置,使原奇数位置节点在偶数位置节点之前,奇(偶)数节点之间相对位置不变,Example:Given 1->2->3->4->5->NULL,return 1->3->5->2->4->NULL.要求:空间复杂度O(1),时间复杂度O(n)分析:
2016-05-25 14:44:43 364
原创 94. Binary Tree Inorder Traversal #Medium
leetcode 94. Binary Tree Inorder Traversal #Medium题意:输出给定二叉树的中序遍历序列分析:按左、根、右的方式递归遍历每个节点,但是为了提高效率,用循环来实现,代码如下:/** * Definition for a binary tree node. * struct TreeNode { * int val;
2016-05-25 14:38:06 162
原创 144. Binary Tree Preorder Traversal #Medium
leetcode 144. Binary Tree Preorder Traversal #Medium题意:给定一个二叉树,输出二叉树的前序遍历序列。分析:比较基础的题目,递归写法非常简单,但是效率不高。前序遍历迭代实现代码:/** * Definition for a binary tree node. * struct TreeNode { * int v
2016-05-25 11:19:57 207
原创 343. Integer Break #Medium
leetcode 343. Integer Break #Medium题目大意:给定一个大于1的正整数n,将n分解为至少2个正整数的和。怎样使分解所得数字之积最大,求出这个最大积分析:先找下规律,以下分解积最大,7=2+2+3 ,8=2+3+3,9=3+3+3,10=2+2+3+3。即要尽量多分解出3,但是不能有1(2*2>3*1),代码如下:class Solution {p
2016-05-25 10:54:15 302
原创 268. Missing Number #Medium
leetcode 268. Missing Number #Medium题目大意:给定一个包含n个数的数组,数组中的数来自于{ 0,1,2,3,..,n} 之一,并且不重复出现,求数组中不包含0~n中的哪一个数字?解法:可以用Single Numbe这一题r的思路,但有所变化。在所给数组中,每个数在数组中出现一次,并在序列{ 0,1,2,3,..,n}中出现一次,只有所求数字只在后面序列中
2016-05-25 10:26:23 236
原创 319. Bulb Switcher #Medium
leetcode 319. Bulb Switcher#Medium题目大意:有编号从1到n共n盏灯,开始都是灭的。第1轮,改变每盏灯的状态;第2轮,改变编号是2的倍数的灯;..;第n轮,改变编号是n的倍数的灯。问最后又多少灯是亮的?解法:灯在改变奇数次状态后是亮的,偶数次是灭的。每盏灯状态变换的次数其实是其编号的因数个数,而除了平方数外,所有正整数都有偶数个因数例如: 8
2016-05-25 09:37:29 363
原创 338. Counting Bits #Medium
leetcode 338. Counting Bits #MediumGiven 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
2016-05-10 18:39:27 230
原创 238. Product of Array Except Self #Medium
leetcode 238. Product of Array Except Self #MediumGiven an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums excep
2016-05-09 20:04:39 285
原创 258. Add Digits #Easy
leetcode 258. Add Digits #EasyGiven 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 +
2016-05-09 19:01:14 298
原创 292. Nim Game #Easy
leetcode 292. Nim Game #EasyYou 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 remo
2016-05-09 18:26:04 332
原创 344. Reverse String #Easy
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh"C++ string有反向迭代器rbegin,rend,利用反向迭代器可以实现反转字符串。class Solution {public:
2016-05-09 18:17:25 239
原创 217. Contains Duplicate #Easy
https://leetcode.com/problems/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 ar
2016-05-04 10:27:46 453 2
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人