![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
struggler_1990
目前从事影视成像行业,熟悉相机相关业务。
展开
-
Leetcode之Reverse Integer
//这道题增加了新的溢出的测试点。原来的方法不能过。//这个方法参考了http://oj.leetcode.com/problems/reverse-integer/的方法,但是原出处有问题,这里有进行改正。class Solution {public: int reverse(int x) { long long r(0);//用于存储结果 bool isPos(t转载 2015-02-16 14:47:28 · 355 阅读 · 0 评论 -
Leetcode之Palindrome Number
/*本解法参照于《Leetcode题解》(网址:https://github.com/soulmachine/leetcode),或作修改,或增加注释等。*/class Solution {public: bool isPalindrome(int x) { if(x < 0) return false;//负数不是回文 int d(1); while(转载 2015-02-16 20:25:07 · 332 阅读 · 0 评论 -
LeetCode之Candy
/*本题给小孩分配糖果的规则:1.每个小孩至少一个糖果。2.高等级小孩的糖果比相邻的小孩的糖果多。题目要求最小的糖果数目。直接的想法是:1.先给每个小孩一个糖果。2.从左往右扫描等级数组,再从右往左扫描一遍等级数组,保证第2个条件成立。3.返回总糖果数目。参考:https://github.com/soulmachine/leetcode*/class Solution {pub转载 2015-03-25 08:38:30 · 345 阅读 · 0 评论 -
LeetCode之Single Number II
/*本题最直观的方法是将数字转化为二进制进行求解:用数组对整型数的每一比特位进行统计,然后对它取模3,最后用统计数不为0的比特位组成数字。参考自:https://github.com/soulmachine/leetcode*/ class Solution {public: int singleNumber(int A[], int n) { vector n转载 2015-03-25 09:29:14 · 306 阅读 · 0 评论 -
LeetCode之Add Two Numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /*此题是模拟数字相加,采用每位相加再进位的方法,由于链表存储数据的 顺序是反转载 2015-03-25 10:03:14 · 349 阅读 · 0 评论 -
LeetCode之Next Permutation
/*这道题目是一道找规律的问题:1.从最末尾寻找一对相邻的元素,满足num[i]<num[i+1];2.如果找到这样的一对相邻元素,再从末尾开始找一个元素num[j],满足num[j]>num[i];如果没有找到这样的相邻元素,转4;3.交换num[i]和num[j],然后颠倒从i+1一直到末尾的元素;4.表示当前序列已经是最大的,直接翻转整个序列即可。方法参考自:https://gi转载 2015-03-19 09:54:57 · 429 阅读 · 0 评论 -
LeetCode之Permutation Sequence
/*这道题的最直观想法是采用计数的方法,先确定第一个数字,同样不断地确定后面的数字即可。思路如下:1.排除第一个数后,其他n-1个数共有(n-1)!个排列;2.因此,所求排列的第一个数下标为(k-1)/((n-1)!); //下标从0开始,而k从1开始,所以为(k-1)3.删除确定的第一个元素,接下来求第(k-1)%((n-1)!)个排列。4.重复1~3,直到n个元素都已经确定。参转载 2015-03-19 10:57:01 · 352 阅读 · 0 评论 -
LeetCode之Merge Interval
/*这个问题可以直接采用Insert Interval问题中采用的方法,创建一个Interval集合,不断的将旧集合中的Interval合并进来,最终完成Merge Interval功能。解决Insert Interval问题的链接:http://blog.csdn.net/shiyimin1/article/details/44066195*/class Solution {privat转载 2015-03-04 21:49:53 · 550 阅读 · 0 评论 -
LeetCode之Insert Interval
/*采用二分法查找新的Interval应该插入的位置,然后根据接下来Interval的start和end值,不断更新newInterval的边界值和删除原有的Interval*/class Solution {private: static bool cmp(const Interval &a, const Interval &b) { return a.start < b.star转载 2015-03-04 21:19:52 · 414 阅读 · 0 评论 -
LeetCode之Multiply Strings
/*对于大整数相乘,可以将数的相乘分为4步(先用数组进行相乘和相加):1.每个数位的相乘;2.将每个数位的所有数字相加;3.根据每个数位的数字,产生进位;4.根据大整数,输出对应的string。转自:http://www.cnblogs.com/TenosDoIt/p/3735309.html*/class Solution {public: string multiply(str转载 2015-03-05 22:12:40 · 242 阅读 · 0 评论 -
LeetCode之Minimum Window Substring
/*该题采用的方法如下:维持一个指向S中字符的一对头尾指针s,e,这对指针指向区间的S的字符包括T中所有的字符,然后最大程度地缩小该区间(该区间包括T中所有字符),直到最小。穷尽S中所有的这样的最小区间,寻找到它们中的最小值,即可获得最终答案。具体步骤如下:1.维持一对头尾指针s,e(它们初始值都为0),e不断的往后增加,直到s,e区间包含T中所有字符;2.头指针s不断往后扫描,直到不能往转载 2015-03-05 18:24:05 · 401 阅读 · 0 评论 -
LeetCode之Median of Two Sorted Arrays
/*本题更一般化的形式为,查找两个数组中第k大的元素。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: double findMedianSortedArrays(int A[], int m, int B[], int n) { int total(m+n); if(total & 0x1转载 2015-03-16 21:10:42 · 290 阅读 · 0 评论 -
LeetCode之Pascal’s Triangle II
/*本题可参照Pascal’s Triangle的方法。只需在Pascal’s Triangle的解题方法中,将保存整个杨辉三角,改为保存当前行即可。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: vector getRow(int rowIndex) { vector result;转载 2015-03-06 15:04:51 · 302 阅读 · 0 评论 -
LeetCode之Reverse Linked List II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /*改变链表的顺序,采用头插法即可。*/class Solution {pu转载 2015-03-26 09:33:15 · 329 阅读 · 0 评论 -
LeetCode之Partition List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /*本题考查链表的遍历和尾插法(不改变相对顺序),具体方法如下: 从头开始遍历原创 2015-03-26 10:38:20 · 301 阅读 · 0 评论 -
LeetCode之Remove Nth Node From End of List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /*用两个指针p,q,最开始都指向头结点,让p先走n个结点,然后p,q一起 走转载 2015-03-27 10:17:27 · 331 阅读 · 0 评论 -
LeetCode之Rotate List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /*1.先遍历一遍链表找到链表的尾结点,计算链表的长度。 2.将链表的尾结点与转载 2015-03-27 09:54:54 · 331 阅读 · 0 评论 -
LeetCode之Remove Duplicates from Sorted List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /*本题思路简单,直接用两个指针pre和cur,分别指向当前指针的 前一个结点转载 2015-03-27 08:40:22 · 317 阅读 · 0 评论 -
LeetCode之Trapping Rain Water
/*方法一:对于每个柱子A[i],假设它左右边最高的柱子分别为A[l]和A[r],那么对于i柱子存储的水为max(A[l], A[r]) - A[i]。可以采用递归的方法求解每个柱子的左右最大值:1.从左往右扫描,获取每个柱子左边柱子中的最大值;2.从右往左扫描,获取每个柱子右边柱子中的最大值;方法参考自: https://github.com/soulmachine/leetcode*转载 2015-03-20 11:10:54 · 337 阅读 · 0 评论 -
LeetCode之Spiral Matrix
/*本题是一道模拟题,直接按照顺时针旋转在矩阵外围的四条边“行走”即可。当不在能“行走”时(不满足条件),即完成了SpiralOrder的输出。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: vector spiralOrder(vector > &matrix) { vector resu转载 2015-03-08 15:00:10 · 298 阅读 · 0 评论 -
LeetCode之Substring with Concatenation of All Words
/*本题和Minimum Window Substring题目很相似:都利用双指针在第一个数组中标志一个区间,让这个区间满足第二个数组表示的某种属性,目的是在第一个数组中寻找出满足这种属性的区间。源代码参考自:http://www.cnblogs.com/panda_lin/archive/2013/10/30/substring_with_concatenation_of_all_words转载 2015-03-06 13:52:43 · 375 阅读 · 0 评论 -
LeetCode之Pascal's Triangle
/*本题是移到模拟题,根据杨辉三角产生的方法可以求解该题。方法:第i行中间的元素等于第i-1行元素中对应左上角和右上角元素的和,然后在第i行数组的两端加上1.方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: vector > generate(int numRows) { std::vect转载 2015-03-06 14:52:21 · 297 阅读 · 0 评论 -
LeetCode之Longest Consecutive Sequence
/*由于数组是无序的,要在O(n)内求解问题,自然想到hash表。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: int longestConsecutive(vector &num) { unordered_map has_appeared;//用来记录对应的数字是否出现过 for(in转载 2015-03-16 22:13:41 · 304 阅读 · 0 评论 -
LeetCode之Valid Sudoku
/*模拟实现题,题意是只要满足以下情况即是九宫格:1.每一行里没有重复的数字;2.每一列里没有重复数字;3.每一个小九宫格里没有重复数字。不满足以上3个条件就不是九宫格。参考自:http://www.cnblogs.com/TenosDoIt/p/3800485.html*/class Solution {public: bool isValidSudoku(vector >转载 2015-03-20 10:36:55 · 360 阅读 · 0 评论 -
LeetCode之Remove Duplicates from Sorted List II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /*本题与Remove Duplicates from Sorted List相原创 2015-03-27 09:26:28 · 372 阅读 · 0 评论 -
LeetCode之Spiral Matrix II
/*这道题的可以参照Spiral Matrix的解法,将读取矩阵改为填充矩阵即可。由于这里是方阵,所以其代码应该会更简单。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: vector > generateMatrix(int n) { vector > result(n, vector(n)转载 2015-03-08 15:27:07 · 310 阅读 · 0 评论 -
LeetCode之Swap Nodes in Pairs
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ //模拟题,直接按照题意交换相邻结点即可class Solution {pu原创 2015-03-27 10:46:16 · 374 阅读 · 0 评论 -
LeetCode之3Sum
/*先对数组进行排序。然后用左右夹逼的方法,不断搜索结果。不过要注意避开重复元素。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: vector > threeSum(vector &num) { vector > result; if(num.size() < 3) return res转载 2015-03-17 11:13:11 · 321 阅读 · 0 评论 -
LeetCode之Divide Two Integers
/*本题不能使用乘、除、模运算,因此只能使用加、减以及位移运算。解决本题最直观的方法是:不断执行(被除数-除数)的运算,直到(被除数<=除数)。但是这种方法的比较耗时,因此可以选择将除数不断位移实现直到(2^k*除数)<= 被除数 < 2^(k+1)*除数。方法参考自:http://www.cnblogs.com/TenosDoIt/p/3795342.html/*/class Solu转载 2015-03-10 11:01:02 · 327 阅读 · 0 评论 -
LeetCode之Two Sum
/*本题最自然的想法是用暴力进行两轮搜索,容易想到,但是时间复杂度为O(n^2)。在此种情况下,可以选择用hash table存储数字对应的下标,从而求解问题,时间复杂度为O(n)。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: vector twoSum(vector &numbers, i转载 2015-03-17 10:23:37 · 297 阅读 · 0 评论 -
LeetCode之ZigZag Conversion
/*这道题是一道模拟题,只需要找出其中的规律即可。当n=4时,zagzag如下:n=40 6 121 5 7 11 132 4 8 10 143 9 15可以找出如下规律:1.每两个垂直列之间的下标差为2*(n - 1);2.倾斜列的元素的下标与它之前的垂直列的元素的下标差为2*(n - 1 - i),i转载 2015-03-10 09:16:14 · 286 阅读 · 0 评论 -
LeetCode之Text Justification
/*本题是一道模拟题,直接按照如下规则进行求解即可:1.每行中的单词之间都有空隙,每个空隙的字符数尽量相等(不能做到相等的,从左边开始增加空隙的空格数)。2.最后一行的空格全部放在右边。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: vector fullJustify(vector &wo转载 2015-03-11 11:29:58 · 338 阅读 · 0 评论 -
LeetCode之Max Points on a Line
/*为了确定在一条直线上的点,可以采用斜率进行统计。对于每个点i,遍历其他所有点,求这些点与i组成的直线的斜率,斜率相同的点都在一条直线,统计每条直线上点的数目,并获得他们的最大值。最后取在所有遍历点后,返回这些最大值中最大的数。方法参考自:https://github.com/soulmachine/leetcode*///struct Point {// int x;//转载 2015-03-11 21:06:25 · 307 阅读 · 0 评论 -
LeetCode之Remove Duplicates from Sorted Array
/*方法一:假定我们用一个新的数组B[]来记录结果,那么B[0]<B[1]<B[2]<......。因而我们可以采用如下办法吧:用i作为指针扫描数组A[],k作为指针扫描数组B[],当A[i]!=B[k]时,可获得B的k+1的元素值,即B[k+1]=A[i]。由于k<=i,因此直接用A数组即可记录结果。方法参考自:https://github.com/soulmachine/leetcod转载 2015-03-12 11:51:05 · 292 阅读 · 0 评论 -
LeetCode之Remove Duplicates from Sorted Array II
/*此题可以参考Remove Duplicates from Sorted Array的方法一,只要加入一种新的情况:当A[k]==A[i]&&A[k]!=A[k-1]时,即可加入新的元素到结果数组。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: int removeDuplicates(in转载 2015-03-12 23:17:46 · 278 阅读 · 0 评论 -
LeetCode之Search in Rotated Sorted Array
/*此题可采用暴力解法。但其实考查的其实二分查找:尽管整个数组并不不一定是全部有序的,但是当我们将数组及其字数组从中截断时,至少有一半的部分是有序的,我们可以根据这一半来采用二分查找的方式。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: int search(int A[], int n, i转载 2015-03-13 21:19:18 · 280 阅读 · 0 评论 -
LeetCode之Search in Rotated Sorted Array II
/*本题与的Search in Rotated Sorted Array差别在于可能有重复的元素,但还是可以采用与Search in Rotated Sorted Array类似的方法。但是在判断数组哪一半有序时会稍微不一样。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public: bool searc转载 2015-03-13 21:44:13 · 327 阅读 · 0 评论 -
LeetCode之Plus One
/*本题是一道模拟题,模拟数字的加法即可。方法参考自: https://github.com/soulmachine/leetcode*/class Solution {public: vector plusOne(vector &digits) { int carry(1); for(vector::reverse_iterato转载 2015-03-23 10:18:24 · 357 阅读 · 0 评论 -
LeetCode之Climbing Stairs
/*本题是模拟题,爬到第n层的步数为:f(n) = f(n-1) + f(n-2);即:到第n层的方法是从第n-1层跨一步和从第n-2步跨两步。方法参考自: https://github.com/soulmachine/leetcode*/class Solution {public: int climbStairs(int n) { int pre(0), c转载 2015-03-23 10:35:31 · 303 阅读 · 0 评论 -
LeetCode之Rotate Image
/*本题可以采用模拟的方法:按照方阵从最外围到最里围,一圈一圈的旋转,但是这种方法太慢。可以采用如下方法:1.矩阵先沿副对角线翻转;2.然后沿水平中轴进行翻转。例子:1 2 4 2 3 1 3 4 -> 3 1 -> 4 2 方法参考自: https://github.com/soulmachine/leetcode*/class转载 2015-03-23 10:06:00 · 350 阅读 · 0 评论