- 博客(177)
- 收藏
- 关注
原创 【leetcode】393. UTF-8 Validation
题目:思路:规律很明显,比较简单。代码实现:自己实现的代码,有点繁琐:class Solution {public: int type(int i){ int mask = 128; while (mask & i){ mask >>= 1; } if (mask == 128){ return 0; }else if
2020-05-31 01:33:28 127
原创 【leetcode】392. Is Subsequence
题目:Given a string s and a string t, check if s is subsequence of t.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remain
2020-05-31 00:55:25 141
原创 【leetcode】390. Elimination Game
题目:There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.Repeat the previous step again, but this time from right to left, remove the righ
2020-05-31 00:39:49 189
原创 【leetcode】389. Find the Difference
题目:Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was added in t.思路:ans初始值为0,然后对s和t的所有元素进行异或操作后就是答案。代码实现:c
2020-05-31 00:24:29 146
原创 【leetcode】388. Longest Absolute File Path
题目:思路:将"\n"将整个string分割成strings数组,数组中每一个元素都是一个folder名字或file名字,决定其所在层级的是名字最前面的"\t"的数量,"\t"的数量加1就是其所在的层级。剩下的就是利用stack不断地进行探索与回溯就可以了。代码实现:class Solution {public: void splitString(vector<string>& strings, const string& org_string, cons
2020-05-31 00:19:18 203
原创 【leetcode】387. First Unique Character in a String
题目:Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.思路:扫描两遍数组即可。代码实现:class Solution {public: int firstUniqChar(string s) { vector<int> hash(26, 0);
2020-05-30 00:27:00 133
原创 【leetcode】386. Lexicographical Numbers
题目:思路:抄不懂。手算一下+脑补一下。代码实现:class Solution {public: vector<int> lexicalOrder(int n) { vector<int> res(n); int cur = 1; for (int i = 0; i < n; ++i){ res[i] = cur; if (cur * 10
2020-05-30 00:20:25 123
原创 【leetcode】385. Mini Parser
题目:思路:‘[’ 新建NestedInteger,’,‘跳过,’]’ 出栈并建立好前后关联。代码实现:class Solution {public: NestedInteger deserialize(string s) { stack<NestedInteger*> my_stack; my_stack.push(new NestedInteger()); for (int i = 0; i < s.size();
2020-05-29 23:34:37 171
原创 【leetcode】384. Shuffle an Array
题目:Shuffle a set of numbers without duplicates.思路:vector<int> &nums用来保存原数组。reset函数: 直接返回原数组。shuffle函数: 当前元素为nums[j],在此之前的元素中随机抽一个元素——nums[i](可能i==j),然后swap这两个数。代码实现:class Solution {public: Solution(vector<int>& nums):
2020-05-29 18:58:01 99
原创 【leetcode】383. Ransom Note
题目:Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.Each letter in the ma
2020-05-29 18:22:36 121
原创 【leetcode】382. Linked List Random Node
题目:Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen.Follow up:What if the linked list is extremely large and its length is unknown to you? Could you solve this effici
2020-05-29 17:41:58 159
原创 【leetcode】380. Insert Delete GetRandom O(1)
题目:思路:vector<int> nums保存val,unordered_map<int, int> my_map保存val到数组index之间的映射关系(val是唯一的)。insert函数: 向数组nums尾部添加新val,同时my_map保存val到index的映射。remove函数: 将被删除val与数组nums最后一个元素swap一下,然后将数组尾元素pop掉即可。代码实现:class RandomizedSet {public: /** Ini
2020-05-29 17:08:32 645
原创 【leetcode】378. Kth Smallest Element in a Sorted Matrix
题目:Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, not the kth distinct element.思路:抄作业https://www.cnblogs.
2020-05-29 16:27:07 132
原创 【leetcode】377. Combination Sum IV
题目:Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.思路:动态规划公式为dp[i]+=dp[i-x], x属于nums中的每个元素,dp[i]表示和为i的所有组合总数。代码实现:class Solution {public: int
2020-05-29 12:33:12 125
原创 【leetcode】376. Wiggle Subsequence
题目:思路:首先求出整个nums的diff数组。然后就只有两种情况——负数开头的diff数组和正数开头的diff数组。然后取其中的最大值即可。代码实现:class Solution {public: int wiggleMaxLength(vector<int>& nums) { if (nums.empty()){ return 0; } vector<int>
2020-05-29 11:51:44 134
原创 【leetcode】375. Guess Number Higher or Lower II
题目:We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I’ll tell you whether the number I picked is higher or lower.However, when you guess a particular
2020-05-29 02:24:37 136
原创 【leetcode】374. Guess Number Higher or Lower
题目:思路:二分法。代码实现:/** * Forward declaration of guess API. * @param num your guess * @return -1 if num is lower than the guess number * 1 if num is higher than the guess number * otherwise return 0 * int guess(int n
2020-05-28 17:58:29 121
原创 【leetcode】373. Find K Pairs with Smallest Sums
题目:思路:抄作业https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/84607/Clean-16ms-C%2B%2B-O(N)-Space-O(KlogN)-Time-Solution-using-Priority-queue。实在是没有理解。代码实现:class Solution {public: vector<vector<int>> kSmallestPairs(v
2020-05-28 17:32:10 143
原创 【leetcode】372. Super Pow
题目:思路:抄作业https://leetcode.com/problems/super-pow/discuss/84472/C%2B%2B-Clean-and-Short-Solution引用博文https://blog.csdn.net/qq_27437197/article/details/86720491:需用到的数学知识:a^b % 1337 = (a%1337)^b % 1337xy % 1337 = ((x%1337) * (y%1337)) % 1337引用Discus
2020-05-28 15:35:10 144
原创 【leetcode】371. Sum of Two Integers
题目:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.思路:博文https://www.cnblogs.com/dyzhao-blog/p/5662891.html中的解释是针对十进制数运算的,推广到二进制数依然是正确的,所以我们的二进制运算就运用这个规律。a&b: 可以求出二进制数a和b相加后的进位。a^b: 可以求出相加后余下的数字。注意有符
2020-05-28 12:27:00 133
原创 【leetcode】368. Largest Divisible Subset
题目:思路:直接引用博文https://www.cnblogs.com/godlei/p/5621990.html的解释:如果a%b==0,则a=mb,所以如果把数组排序后如果a%b==0,且b%c==0则a%c==0。这就为用动态规划实现提供了可能性。设置一个数组result,result[i]表示i出包含的满足条件的子集个数。则如果nums[i]%nums[j]==0,则result[i]=result[j]+1;同时由于函数要返回的是一个List,所以我们要保存最长集合的路径。这个功能可以
2020-05-28 11:52:53 142
原创 【leetcode】367. Valid Perfect Square
题目:Given a positive integer num, write a function which returns True if num is a perfect square else False.Follow up: Do not use any built-in library function such as sqrt.思路:我是用的最笨的方法,实际上其背后蕴含着一个数学规律。代码实现:class Solution {public: bool isPerfec
2020-05-28 10:47:18 125
原创 【leetcode】365. Water and Jug Problem
题目:思路:直接看博文https://www.cnblogs.com/grandyang/p/5628836.html。代码实现:class Solution {public: int GCD(int a, int b){ while (b){ int t = b; b = a % b; a = t; } return a; } bool
2020-05-28 01:11:53 150
原创 【leetcode】357. Count Numbers with Unique Digits
题目:思路:规律如下:f(0)=1f(1)=10f(2)=9*9+f(1)f(3)=9*9*8+f(2)f(4)=9*9*8*7+f(3)f(5)=9*9*8*7*6+f(4)f(6)=9*9*8*7*6*5+f(5)f(7)=9*9*8*7*6*5*4+f(6)f(8)=9*9*8*7*6*5*4*3+f(7)f(9)=9*9*8*7*6*5*4*3*2+f(8)f(10)=9*9*8*7*6*5*4*3*2*1+f(9)大于10的情况全为0,因为就10个数字,肯定都会重复
2020-05-28 00:47:59 115
原创 【leetcode】355. Design Twitter
题目:思路:数据结构解释如下:Tweet: 推特消息,以list的形式存放。一个Tweet就是一个list node,包含id,time以及next指针。User: 用户,实体对象。其中包含用户id、存放订阅用户的unordered_set<int> follows以及存放自己发送推特消息的Tweet *tweets。follow函数就是把订阅用户id存放到unordered_set<int> follows中;unfollow函数正好相反。post函数就是把对应的推特消
2020-05-28 00:10:49 139
原创 【leetcode】350. Intersection of Two Arrays II
题目:Given two arrays, write a function to compute their intersection.思路:直接看代码。代码实现:class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { unordered_map<int, int> my_map;
2020-05-27 21:57:34 127
原创 【leetcode】349. Intersection of Two Arrays
题目:Given two arrays, write a function to compute their intersection.思路:利用unordered_map<int, bool> my_map保存nums1所有元素,然后遍历nums2。如果某个元素已经遍历过了,则将其在my_map中的值置为true。代码实现:class Solution {public: vector<int> intersection(vector<int>&a
2020-05-27 19:38:36 111
原创 【leetcode】347. Top K Frequent Elements
题目:Given a non-empty array of integers, return the k most frequent elements.思路:抄作业https://leetcode.com/problems/top-k-frequent-elements/discuss/81676/C%2B%2B-O(nlogk)-and-O(n)-solutions。思路1:用unordered_map对各个元素进行计数,用priority_queue找到第k大的元素,然后从头到尾遍历unord
2020-05-27 19:29:41 209
原创 【leetcode】345. Reverse Vowels of a String
题目:Write a function that takes a string as input and reverse only the vowels of a string.思路:很简单,直接看代码。代码实现:class Solution {public: bool isVowel(char c){ return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c ==
2020-05-27 18:16:04 107
原创 【leetcode】343. Integer Break
题目:Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.思路:我是用的动态规划,但实际上背后存在一个数学问题,参考https://leetcode.com/problems/integer-break/discuss/8
2020-05-27 17:45:24 193
原创 【leetcode】342. Power of Four
题目:Given an integer (signed 32 bits), write a function to check whether it is a power of 4.思路:(num&(num-1))==0保证整个二进制中只有一个1;(num-1) % 3 == 0保证1位于二进制中的偶数位上。代码实现:class Solution {public: bool isPowerOfFour(int num) { return num > 0
2020-05-27 17:04:48 124
原创 【leetcode】341. Flatten Nested List Iterator
题目:Given a nested list of integers, implement an iterator to flatten it.Each element is either an integer, or a list – whose elements may also be integers or other lists.思路:抄作业https://leetcode.com/problems/flatten-nested-list-iterator/discuss/80146/Re
2020-05-27 16:44:29 95
原创 【leetcode】338. Counting Bits
题目:Given 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 as an array.思路:抄作业https://blog.csdn.net/lym940928/article/details/81370242,关键部分引用:i&am
2020-05-27 15:57:28 151
原创 【leetcode】337. House Robber III
题目:The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that “all houses in this
2020-05-27 13:18:59 138
原创 【leetcode】334. Increasing Triplet Subsequence
题目:思路:抄作业https://www.cnblogs.com/warmland/p/5735208.html。用x和y保存序列中所有nums[i]<nums[j]中nums[j]最小的那一对。两个变量的解释如下:x:此值尽量压低。y:此值也尽量压低。其意义是:如果此值为INT_MAX,则说明之前还没有出现过nums[i]<nums[j]的情况;反之则说明已经出现过了nums[i]<nums[j],y就代表之前最小的那一对nums[i]<nums[j](比如,之前序列
2020-05-27 12:18:22 114
原创 【leetcode】332. Reconstruct Itinerary
题目:思路:有向图的深度优先搜索。tickets里实际上保存的是图的有向边。我用unordered_map保存每个from节点(出发地)到其所有邻接to节点(目的地,用一个链表将所有邻接to节点按照字符串从小到大的顺序串起来)的映射关系。每次深度优先遍历邻接点时,都临时将节点从对应链表中删除掉,继续进行更深度的递归。如果能成功,即所有地点都串起来了(总数为边的数量加1),则ok;如果不成功,则将删除的节点再添加回去,继续尝试下一个邻接点。如此往复,直到成功。代码实现:代码搞了好几个小时,真就一
2020-05-22 02:57:04 194
原创 【leetcode】331. Verify Preorder Serialization of a Binary Tree
题目:思路:思路1:引用博文https://blog.csdn.net/fuxuemingzhu/article/details/79537797的解释:用一个栈,从字符串的左侧向右依次进栈,如果满足栈的后三位是数字,#,#的模式时,说明可以构成合法的叶子节点,把这个叶子节点换成#号,代表空节点,然后继续遍历。最后应该只剩下一个#,那么就是一个合法的二叉树。思路2:引用博文https://blog.csdn.net/fuxuemingzhu/article/details/79537797
2020-05-22 00:13:11 127
原创 【leetcode】328. Odd Even Linked List
题目:Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in place. The program should run in O(1) space complexity
2020-05-21 23:00:50 150
原创 【leetcode】326. Power of Three
题目:Given an integer, write a function to determine if it is a power of three.思路:引用博文https://blog.csdn.net/ebowtang/article/details/50485622的解释:任何一个3的x次方一定能被int型里最大的3的x次方整除。代码实现:class Solution {public: bool isPowerOfThree(int n) { if
2020-05-21 22:49:03 112
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人