自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(29)
  • 收藏
  • 关注

原创 83. Remove Duplicates from Sorted List&82. Remove Duplicates from Sorted List II

83.Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.检查当前指针与下一个指针的值是否相同,

2016-11-30 10:00:31 125

原创 80. Remove Duplicates from Sorted Array II

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 the first five e

2016-11-29 11:21:28 135

原创 79. Word Search

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 "adjacent" cells are those horizontally or vertically n

2016-11-29 11:18:51 197

原创 78. Subsets

Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example,If nums = [1,2,3], a solution is:[ [3], [1], [2]

2016-11-28 17:12:47 139

原创 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,

2016-11-28 17:04:59 153

原创 69. Sqrt(x)

Implement int sqrt(int x).Compute and return the square root of x.用二叉检索在小于x的树中搜索x的根。class Solution {public: int mySqrt(int x) { if(x<=0) return 0; if(x==1) retu

2016-11-27 11:38:46 227

原创 73. Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.这题有很多方法,一开始想的是用O(m+n)的空间,用vector存储……不过要求用常数空间做。看了一个方法,可以用set做。set类似于vector,但是存取比vector快,删除元素等操作比vector慢

2016-11-27 11:36:16 372

原创 67. Add Binary

Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".很简单的思路,ab都从最后一位开始,初始化c=0为进位,三个数异或得到新的值存入res中,再判断一下c应该是1还是0. 当ab有一个走到尽头后把另一个剩下的部分

2016-11-27 11:28:35 260

原创 74. Search a 2D Matrix&240. Search a 2D Matrix II

74.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 sorted from left to right.The first integer of

2016-11-27 11:22:30 229

原创 71. Simplify Path

这题自己没想出来啥好解法。。。先设置一个list作为存储字串的空间,之后进入循环,每次找‘/’值作为尾端,将‘/’之前的字符串提取出来作为字串,字串若为“..”,则将list最后一个pop出来。若不是“.”或“..”,则将字串push进去。最后再新建一个string将list中没有加/的字串组合起来。最后返回的时候判断res是否为空,空的话则返回/class Solution {

2016-11-26 21:59:21 128

原创 66. Plus One

1、从后向前找,找到第一个不为9的数字后将它+1,之后的数字全都置0.2、如果全部数字都为9,那么首位置1,所有位置0,再压进去一个0.class Solution {public: vector plusOne(vector& digits) { int len=digits.size(); for(int i

2016-11-23 09:53:22 263

原创 62. Unique Paths& 63. Unique Paths II &64. Minimum Path Sum

这三道题很像……都需要通过动态规划来解决。一开始还想用backtrack穷举,然而超时惹……62.能够到达path[m-1][n-1]的路径数为能到达path[m-1][n-2]和path[m-2][n-1]的路径数之和class Solution {public: int uniquePaths(int m, int n) {

2016-11-23 09:50:53 191

原创 60. Permutation Sequence

class Solution {public: string getPermutation(int n, int k) { string ans=""; k-=1;//这里其实不太懂为啥k要-1……应该是为了配合后面的nums下标从0开始 vector nums; for(int i=0;

2016-11-19 11:49:10 177

原创 61. Rotate List

找到尾巴,找到断点,然后前后拼接起来……找尾巴的时候顺便找到链表长度,然后可以执行一下k%=n的操作简化运算。class Solution {public: ListNode* rotateRight(ListNode* head, int k) { if(head==NULL) return head; Lis

2016-11-19 11:46:55 141

原创 55. Jump Game

这题主流的算法是贪心的思想,不断计算i+num[i]的值并和reach比较,维护一个最大的reach值。最后reach值大于n-1就代表可以走到最后。class Solution {public: bool canJump(vector& nums) { int maxReach = 0, n = nums.size(); for(in

2016-11-18 20:56:30 151

原创 54. Spiral Matrix&59. Spiral Matrix II

这两道题其实很类似……一个是给定二维矩阵,逐个读取其中的数字,另一个是给定数字,逐个插入一个二维数组。其实套路就是设置四个指针,分别指向最前面和最后面的行,最前面和最后面的列。插入之后更新。54class Solution {public: vector spiralOrder(vector>& matrix) { if(matrix.emp

2016-11-18 20:52:36 206

原创 53. Maximum Subarray

1、用动态规划的思想,如果之前的sum>=0,那么之前的sum可以加到a[i]上,如果之前的sum2、自己想了一个是因为max_sum的左右肯定都是正数,那么只需要考虑正数就可以了,从第一个正数加到第二个、第三个……再从第二个正数加……不过这样很慢就是了。class Solution {public: int maxSubArray(vector& nums) {

2016-11-16 11:21:23 196

原创 50. Pow(x, n)

中心思想并不难,递归相乘就可以了,主要是对一些特殊情况的考虑。1、n=02、n=INT_MAX3、n=INT_MIN4、n递归的复杂度为ON,采用二分法可以把复杂度降到OlogNclass Solution {public: double myPow(double x, int n) { double res=1.0;/

2016-11-15 12:51:07 442

原创 Group Anagrams

先对字符串进行排序,然后利用哈希表存储排过序的字符串,key值是当时的res大小(也就是从0开始的键值)值得注意的是发现了一种很方便的push_back写法,就是注释的那句。class Solution {public: vector> groupAnagrams(vector& strs) { if(strs.size()==0)

2016-11-15 12:46:47 145

原创 48. Rotate Image

hin简单,先把矩阵以对角线为中心两两交换,最后每行reverse。需要注意的是一开始自己在没注意,写了一个i从0到n,j从0到n的双重循环。这很明显交换以后又交换回去了嘛。只需要遍历上三角或者下三角就好了。class Solution {public: void rotate(vector>& matrix) { int n=matrix.si

2016-11-11 13:58:11 149

原创 46. Permutations&&47. Permutations II&31. Next Permutation

一开始想用backtracking再加一个判断前面的值是否调用过的矩阵做,但是后来觉得很麻烦耶……看了discuss发现有一个如此机智的办法!大概就是一开始就各种递归调用,直接到vector最后一个值,然后push,return,到n-2的时候把n-2和n-1交换,push,return,然后到n-3,再分别把n-3和n-2、n-1交换,如此这般。直到第一个值。虽然我觉得很机智

2016-11-10 22:10:32 194

原创 backtrack:39. Combination Sum&40. Combination Sum II&77. Combinations

没啥说的……写完以后感觉自己以后都可以写backtrack的题惹!昨天看论坛上有一句话对于backtrack写的很好:你需要把算法穷举的结果想象成一棵树,然后对其进行剪枝,大概是这样子吧……主要一开始卡壳的原因是不知道如果return返回到上一层的话,上一层怎么返回到上上层,其实要把上一层的值pop出来就好了……就是说,尝试过第n层递归中的各种情形之后,发现此路不通,那么第n-1个push

2016-11-09 16:14:24 254

原创 38. Count and Say

这题题意巨难懂啊……大概就是1表示1,然后2的时候再对1进行解读:1个1,写作11,然后3的时候再对11进行解读:2个1,写作21……然后一直到n几个问题大概就是:1、string的使用问题,s=s+"1",还有s不能扩展int2、count还有res都要记得每次循环结束后清零class Solution {public: string countAn

2016-11-07 21:18:22 126

原创 36. Valid Sudoku

这个应该有办法让存储空间更小一点……不过leetcode没说我也没管了row是判断每行9个数字是否有重复的col是判断每列9个数字是否有重复的blocks是判断每个3*3里是否有重复的class Solution {public: bool isValidSudoku(vector>& board) { int row[9][9]={

2016-11-07 21:13:23 127

原创 35. Search Insert Position

简直感动!!!头一次20分钟写完leetcode medium的题!!!第一次就AC!!!AC!!!虽然是因为昨天写过类似的二叉搜索题!!!难以自拔!!!必须记录!!!class Solution {public: int searchInsert(vector& nums, int target) { if(nums.size(

2016-11-07 09:26:51 152

原创 34. Search for a Range

这道题一开始没看别人的算法,第一个想法是用递归做……自己憋了两个小时终于憋出来了,然而超时……递归的想法是先用二分法找到一个target,然后从target左右再用二分法找边界。然而……真的是……太慢了……class Solution {public: vector searchRange(vector& nums, int target) {

2016-11-07 09:05:48 176

原创 29. Divide Two Integers

题意很简单,不用乘除以及取余符号做两个数的除法。其实用减法就可以,用被除数不断减去除数,但是这样做我感觉会超时……然后我大概的思路就是先将被除数不断左移直到即将要比被除数大的时候,然后拿被除数减去除数,然后除数再左移至比新的被除数小,然后重复上述步骤直到被除数为0.需要注意的是有两个坑,一个是关于-2147483648,-1这个特殊用例,一开始没有排除这个特殊用例,后来发现不管输出

2016-11-06 17:14:29 214

原创 23. Merge k Sorted Lists

这题我没有用STL自带的堆,自己实现了堆的基本操作……上次面试实习的时候被问到堆竟然不会感觉到很丢人……总之还是学习到很多的!虽然不太快但是我满足了……下次我再学学怎么用STL的堆噗/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *

2016-11-06 16:25:42 206

原创 26. Remove Duplicates from Sorted Array

class Solution {public: int removeDuplicates(vector& nums) { if (nums.size()==0) return 0; int n=1;//这里一开始设成0,后来发现这样子会跳掉最后一个数,因为n代表的是有多少个不重复的数,而初始值至少也该有1

2016-11-02 08:58:11 141

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除