随笔分类 -  刷题

摘要:There are some chips, and the i-th chip is at position chips[i]. You can perform any of the two following types of moves any number of times (possibly 阅读全文
posted @ 2019-10-07 20:09 rarecu 阅读(262) 评论(0) 推荐(0)
摘要:Given an integer array arr and an integer difference, return the length of the longest subsequence in arr which is an arithmetic sequence such that th 阅读全文
posted @ 2019-10-07 20:03 rarecu 阅读(167) 评论(0) 推荐(0)
摘要:In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the ma 阅读全文
posted @ 2019-10-07 19:51 rarecu 阅读(157) 评论(0) 推荐(0)
摘要:1. 贪心,回溯。从最后一个位置往前找,如果有一个i,它走得最远能够到达或超过我们现在所在的位置,那就把它当作上一步,直到找到最前面。如果最前面是第一步,说明可以沿着刚才的路径到达最后,否则不行。 2. 贪心思想,递归做法。从第一个位置往后找,当前位置是now,如何确定next在哪一个? 在当前所有 阅读全文
posted @ 2019-09-15 16:41 rarecu 阅读(172) 评论(0) 推荐(0)
摘要:思路: 是否合法,最简单判断方法是用栈。只有一种括号,栈中存放元素只需要左括号的下标。 每一次遇到),就把栈顶的一个左括号下标和当前下标一起压入vector中,将这个vector排序,就是所有合法子串的下标。遍历这个vector,找连续的最长值就可以。排序复杂度O(nlogn),除此之外遍历一次字符 阅读全文
posted @ 2019-09-15 15:59 rarecu 阅读(536) 评论(0) 推荐(0)
摘要:// left是当前可用的左括号数,right是当前可用的右括号数,思路就是从左到右地找每一种可能。 // 当前,如果left==right的话,下一个字符一定只能是“(” //如果left> threeSum(vector& nums) { sort(nums.begin(),nums.end()); vector> result; for(in... 阅读全文
posted @ 2019-09-15 01:01 rarecu 阅读(258) 评论(0) 推荐(0)
摘要://解法一,map将数组分成n,0,p三组,寻找9(0,0,0),(n,0,p)(n,n,p)和(p,p,n)的组合,但是因为不可重复,所以最后两种组合比较麻烦,复杂度是O(n^2),遍历多次,最后两个case TLE class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { map<int,int> ne 阅读全文
posted @ 2019-09-15 00:30 rarecu 阅读(499) 评论(0) 推荐(0)
摘要://第一种解法,遍历一次数长度,然后把头尾相连,再遍历一次 class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* tem=head; int len=0; while(tem->next){ //这里得到的长度是真实... 阅读全文
posted @ 2019-09-14 22:00 rarecu 阅读(145) 评论(0) 推荐(0)
摘要:题目描述 有一颗二叉树,最大深度为D,且所有叶子的深度都相同。所有结点从左到右从上到下的编号为1,2,3,·····,2的D次方减1。在结点1处放一个小猴子,它会往下跑。每个内结点上都有一个开关,初始全部关闭,当每次有小猴子跑到一个开关上时,它的状态都会改变,当到达一个内结点时,如果开关关闭,小猴子 阅读全文
posted @ 2019-09-06 22:32 rarecu 阅读(225) 评论(0) 推荐(0)