- 博客(61)
- 收藏
- 关注
原创 [Array]-189. Rotate Array(@python)解题报告
原题链接:https://leetcode.com/problems/rotate-array/description/思路分析:将一个数组右旋转k位,将最后的结果和最初的数组对比,找出坐标对应关系,替换元素即可Time Complexity:O(n)Space Complexity:O(k)题解: length = len(nums) k = k
2017-11-24 11:29:31
377
原创 [Array]-119. Pascal's Triangle II(@python)解题报告
原题链接:https://leetcode.com/problems/pascals-triangle-ii/description/题意:这道题和118题类似,但是这道题要求的输出是第k行的输出即可。思路:可以利用杨辉三角一个很重要的特性快速得到结果,那就是一行的数组可以有上一行的数组前后分别添加0 相加得到题解:class Solution(object
2017-11-19 18:17:08
309
原创 [Array]-118. Pascal's Triangle(@python)解题报告
原题链接:https://leetcode.com/problems/pascals-triangle/description/题目大意:输入一个整数k,生成一个高度为k的杨辉三角思路:依次生成每一层的数组即可,注意层次关系是res[row][i] = res[row-1][i-1] + res[row][i]题解:class Solution(object
2017-11-19 17:47:07
331
原创 [Array]-643. Maximum Average Subarray I (@python) 解题报告
原题链接:https://leetcode.com/problems/maximum-average-subarray-i/description/题目大意:给定一个整型数组,和一个整数k, 找到平均值最大的个数为k的子数组。解题思路:常规思路:使用滑动窗口。 窗口大小为k题解:class Solution(object): def findMa
2017-11-18 21:45:32
388
原创 leetcode 349. Intersection of Two Arrays-[Java]
原题链接:Intersection of Two Arrays思路:利用set去除重复,查找两个集合中都有的元素添加进结果集;public class Solution { public int[] intersection(int[] nums1, int[] nums2) { /* Time complexity:O(m
2017-08-14 19:58:52
249
原创 数组-leetcode 414. Third Maximum Number
原题链接:Third Maximum Number分析:找出第三大的数,这是找出第K大的数这道题的一个简易版,当然可以使用找第K大的数的做法(快排,partition,O(N))。这里我们使用的是简单的找最大值的方法,找出第一个的数,找出第二大的数,找出第三大的数,就是结果。题解:public class Solution { public int thirdM
2017-07-23 14:46:50
295
原创 数组-leetcode 283. Move Zeroes
原题链接:Move Zeros思路分析:将元素0移动到数组末端并保持非0元素相对顺序不变,而且要in-place。使用双指针,一个指针遍历,另一个指针始终保持指向数组前段非0元素的最后一个遍历指针遍历完,将指向指针之后的元素都置0即可;题解:public class Solution { public void moveZeroes(int[] nums
2017-07-17 20:19:01
329
原创 位操作-leetcode 371. Sum of Two Integers
原题链接:Sum of Two Integer分析:不使用加号的加法运算,考虑如下情况:1)a与b之间没有进位,那么a^b就是结果2)a与b之间有进位,即a&b不等与0,那么结果就是a&b 考虑循环进位,使用递归处理sum;题解:public class Solution { public int getSum(int a, int b) {
2017-07-16 11:31:23
303
原创 栈-leetcode 496. Next Greater Element I
原题链接:Next Greater Element I题解:public class Solution { public int[] nextGreaterElement(int[] findNums, int[] nums) { /* Time Complexity:O(N) Space Complexity
2017-07-16 09:59:53
542
原创 字符串-leetcode 541. Reverse String II
原题链接:Reverse String题解:public class Solution { public String reverseStr(String s, int k) { /* Time Complexity:O(N) Space Complexity:O(N) */ c
2017-07-14 16:31:28
379
原创 字符串- leetcode 344. Reverse String
原题链接:Reverse String题解:public class Solution { public String reverseString(String s) { /* Time Complexity:O(N) Space Complexity:O(N) */ char[
2017-07-14 11:59:21
293
原创 数组-leetcode 1. Two Sum
原题链接:Two Sum题解:public class Solution { public int[] twoSum(int[] nums, int target) { /* Time Complexity:O(n) Space Complexity:O(n) */ int[]
2017-07-14 00:45:57
350
原创 Array-leetcode 566 Reshape the Matrix
原题链接:Reshape the Matrix题解:public class Solution { public int[][] matrixReshape(int[][] nums, int r, int c) { /* Time Complexity:O(M*N) Space Complexity:O(M*
2017-07-11 00:08:18
332
原创 字符串 leetcode 387. First Unique Character in a String
原题链接:First Unique Character in a String题解:class Solution {public: int firstUniqChar(string s) { mapmm; for(int i=0;i<s.size();i++)mm[s[i]]++; for(int i=0;i<s.size(
2017-07-05 21:29:15
251
原创 位操作-leetcode 342 Power of Four
原题链接:Power of Four分析:这道题和power of two的区别是,这道题不仅要求1的个数只有一个(power of two是这样),而且还要求1后面的0是偶数个。题解:class Solution {public: bool isPowerOfFour(int num) { /* Time Comple
2017-07-04 00:51:03
257
原创 位操作 leetcode-190. Reverse Bits
原题链接:Reverse Bits分析:倒转比特位,仔细想想,如何将一个二进制位倒转,我们平常计数从个位到十位再到百位,最低位是个位。现在倒转可不可以将权重倒转,最高位是个位,最低位是原来的最高位,这样就实现了二进制位倒转。题解:class Solution {public: uint32_t reverseBits(uint32_t n) {
2017-07-04 00:22:38
293
原创 位操作-leetcode 78. Subsets
原题链接:Subsets分析:求一个数组的全部子集,假设数组的长度为n,可以确定的是子集个数为2^n,如果要编号的话从0到2^n-1,这似乎对应着n位二进制的不同情况,那么思路就来了,可以模拟0-2^n-1二进制对应这数组中不同位置的数,依次添加进结果数组中,就是结果。代码如下:题解:class Solution {public: vector> subset
2017-07-03 21:35:58
274
原创 位操作-leetcode 231 Power of Two
原题链接:Power of Two分析:判断一个数是不是2的次方,显然,n题解:class Solution {public: bool isPowerOfTwo(int n) { /* Time Complexity:O(1) */ if(n<=0)return false;
2017-07-03 21:01:20
283
原创 位操作-leetcode-405. Convert a Number to Hexadecimal
原题链接:Convert a Number to Hexadecimal比较简单,代码如下:class Solution {public: string toHex(int num) { vectorivec={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; i
2017-07-03 19:52:40
302
原创 位操作-leetcode 401. Binary Watch
原题链接:Binary Watch题解:class Solution {public: //计算1的位数 int countBits(int n){ if(n==0)return 0; int count=1; while(n=n&(n-1))count++; return count; }
2017-07-03 19:27:56
293
原创 位操作-leetcode 268 Missing Number
原题链接:Missing Number思路:这道题我的做法是,判断边界条件,求最大值,最小值,如果最小值不是0,直接返回0。否则,从0开始到最大值累积异或。然后将异或值和nums中的每一个元素异或,得出结果。题解:class Solution {public: int missingNumber(vector& nums) { /*
2017-07-03 12:26:00
241
原创 leetcode 169. Majority Element
原题链接:Majority Element题解(二逼版):class Solution {public: int majorityElement(vector& nums) { /* Time Complexity:O(nlogn) Space Complexity:O(1) */
2017-07-02 20:38:40
299
原创 位操作 leetcode 260. Single Number III
原题链接:SIngle Number III题解:class Solution {public: vector singleNumber(vector& nums) { /* 这道题唯一的关键点在于:确定一个可以区分a b的元素,a^b的情况下,这里找出了最右边的1,也就是a 和 b不同的一位 采用的是 re
2017-07-02 20:07:06
246
原创 位操作-leetcode 371 Sum or Two Integers
原题链接:Sum of Two Integers分析:实现加号运算符,采用的是位操作,分三步:第一步a^b得到一个值res第二步(a&b)第三步,如果有进位表示res还需要加上进位的值,否则res就是最终结果,注意res加上进位值也有可能有进位,所以递归getSum()操作。题解:class Solution {public: int getSum
2017-07-02 12:08:41
195
原创 位操作-leetcode 389 Find the Difference
原题链接:Find the Difference题解:class Solution {public: char findTheDifference(string s, string t) { /* Time Complexity:O(m+n) Space Complexity:O(1) */
2017-07-02 11:34:20
248
原创 位操作-leetcode 136. Single Number
原题链接:Single Number分析:每个元素都出现了两次除了一个(好凄惨,就像一对一对的情侣中只有一条单身狗),现在要找出这条单身狗,哦不,这一个单独的数Single Number. 注意到,任何两个相同的整数 异或 的结果为0,和连连看一样,一对一对就消掉了,剩下一只消不掉的就是我们要的结果。题解:class Solution {public: i
2017-07-02 10:54:05
263
原创 位操作-leetcode 338 Counting Bits
原题链接:Counting Bits题解:class Solution {public: vector countBits(int num) { /* i的1的个数等于i>>1+i&1的,然而i>>1在已经计算出来 Time Complexity:O(N) Space Complexity:
2017-07-01 15:25:51
207
原创 位操作-leetcode 476. Number Complement
原题链接:Number Complement题解:class Solution {public: int findComplement(int num) { long int flag=1; long int res=0; while(flag && flag<=num){ int t=flag# if(t==0){ res |
2017-07-01 11:05:15
229
原创 位操作-leetcode 461 Hamming Distance
原题链接:Hamming Distance题解:class Solution {public: int hammingDistance(int x, int y) { /* Time Complexity:O(1) Space Complexity:O(1) */ x^=y;
2017-07-01 10:37:58
246
原创 链表-leetcode 138. Copy List with Random Pointer
原题链接:Copy List With Random Pointer题解:/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * RandomListNode *next, *random; * R
2017-07-01 00:39:51
357
原创 字符串令牌解析
这部分内容我觉得一般情况下用不到,只有在高级字符解析,有特定需求的时候才可能有用,所以并没有花太多时间去揣摩。中文版原文链接在此:字符串令牌解析假如我们有这样一段字符串s='jack + me = money ?' 在这个字符串中,像jack,me,money这些就是一类词语,姑且称作名词,像+,=,?这样的那就是标点符号了,当然还有我们无处不在的whitespace了。
2017-06-30 21:37:10
1327
原创 链表-leetcode 86 Partition List
原题链接:Partition List题解:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solut
2017-06-30 21:34:45
314
原创 链表- leetcode 147. Insertion Sort List
原题链接:Insertion Sort List题解:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class
2017-06-30 16:30:32
382
原创 链表-python-leetcode 83 Remove Duplicates from Sorted List
原题链接:Remove Duplicates from Sorted List代码如下:(python)# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next =
2017-06-29 15:29:41
400
原创 链表-leetcode 83 Remove Duplicates from Sorted List
原题链接:Remove Duplicates from Sorted List分析:因为链表已经是有序的,所以重复的值都会集中在一起,所以直接遍历,删除重复值就行。题解:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;
2017-06-29 15:26:19
274
原创 链表-leetcode 25 Reverse Nodes in k-Group
原题链接:Reverse Nodes in K-Group分析:这道题其实并不难,只是题目限制了很多空间,限制不能交换值,只能交换节点,限制了只能使用constant的空间,所以使用辅助栈的方式的做法不符合题意,当然用了也是可以Accept的,另当别论了。这道题其实类似于一道,不使用栈的单链表反转,只是这里是每k个节点反转一次。代码如下:/** * Definition
2017-06-29 15:08:29
348
原创 数组-leetcode 561 Array Partition I
原题链接:Array Partition I分析:这道题觉得没什么意思,关键是要明白所谓的pair的最小值的sum要最大,是不是让每个pair的min发挥的价值要大一点,换句话说就是我的最小值是不是要同时抹掉另一个很小的值,这样其他的pair的min就会大一点。这样一来,问题就迎刃而解了。排序,两两组队,取偶数位的sum就是结果。代码如下:class Solution
2017-06-28 20:35:40
403
原创 字符串-leetcode 72 Edit Distance
原题链接:Edit Distance分析:这是一个编辑距离的问题,在编程之美上也有这道题,做法比较简单的可以想到递归的思路,然而会发现递归的思路复杂度太高,是指数级,转而也比较容易想到动态规划的思想优化复杂度,具体代码如下:class Solution {public: int minDistance(string word1, string word2) {
2017-06-28 14:22:16
296
原创 leetcode 434- Number of Segments in a String
原题链接:Number of Segments in a String分析:题目比较简单,求非空字符的片段数,只需要判断s[i]!=' '&&s[i+1]==' '就可以;代码如下:class Solution {public: int countSegments(string s) { /* Time Complexity:O(N
2017-06-27 20:12:42
432
原创 字符串-leetcode 521 Longest Uncommon Subsequence I
原题链接:Longest Uncommon Subsequence I题解如下:class Solution {public: int findLUSlength(string a, string b) { /* 这道题是一道流氓题,看题意看半天,解题一分钟; 最长不公共子序列,显然,如果a!=b,那么长度最
2017-06-27 17:15:10
229
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人