leetcode
文章平均质量分 51
superalsrk
这个作者很懒,什么都没留下…
展开
-
LeetCode之Swap Nodes in Pairs
http://oj.leetcode.com/problems/swap-nodes-in-pairs/交换相邻的节点,思路如下所示:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNo原创 2013-12-27 22:13:44 · 943 阅读 · 0 评论 -
LeetCode之Merge Two Sorted Lists
http://oj.leetcode.com/problems/merge-two-sorted-lists/合并两个有序链表,重点在于 头指针的建立 (新建一个头指针省时省力)/** * Definition for singly-linked list. * struct ListNode { * int val; *原创 2013-12-26 11:13:53 · 542 阅读 · 0 评论 -
LeetCode之Plus One
http://oj.leetcode.com/problems/plus-one/大数加法,但是只加了一个1, 注意进位class Solution {public: vector plusOne(vector &digits) { reverse(digits.begin(), digits.end()); int carry_d原创 2013-12-26 11:53:39 · 642 阅读 · 0 评论 -
LeetCode之Unique Binary Search Trees
class Solution {public: int numTrees(int n) { return findUniqueBST(1, n); } int findUniqueBST(int start, int end) { int total_count = 0; if(start >= end) retu原创 2013-12-25 20:26:13 · 637 阅读 · 0 评论 -
LeetCode之Single Number
题目是要一个 线性的算法,即O(n), 如果是两次for循环复杂度就变成O(n^2)了。算法复杂度是不考虑 O的参数的:并且:O(lg(n)) 故解题思路是: 先 快速排序( O(lg(n)) ) ,然后 线性验重( O(n) ),这样就把时间复杂度就控制在 linear runtime complexity 了class Solution {public: i原创 2013-12-24 22:49:49 · 602 阅读 · 0 评论 -
LeetCode之Reverse Integer
数字反转,不过点击那个 click to show spoilers. ,会出现一些面试的“剧透”,因为LeetCode是为面试服务的,所以即使题目AC掉了,还是有很多坑可以调的。比如这道题的坑就有: 末尾是0的反转结果是啥,如果反转后整型溢出了怎么办( 1000000003 ),如果需要处理这种溢出情况,怎么处理?是throw 一个exception还是返回extra pa原创 2013-12-25 12:04:54 · 580 阅读 · 0 评论 -
LeetCode之Best Time to Buy and Sell Stock
前几次时直接LTE了,原来的做法是两次循环,求得最大值与最小值。这种方法的第二个循环主要是求最小值的,而这个最小值是可以通过变量记录下来的。所以AC的方法是:从前往后,用当前价格减去此前最低价格,就是在当前点卖出股票能获得的最高利润。class Solution {public: int maxProfit(vector &prices) { i原创 2013-12-25 17:13:59 · 768 阅读 · 0 评论 -
LeetCode之Single Number II
http://oj.leetcode.com/problems/single-number-ii/Every element appears three times except for one,需要注意的特殊情况是 n == 1的情况class Solution {public: int singleNumber(int A[], int n) {原创 2013-12-26 21:54:19 · 594 阅读 · 0 评论 -
LeetCode之Linked List Cycle II
还是给定一个链表,查看是不是有环,有环的话返回 环的起始节点。为了 without using extra space, 遍历某节点后更改val用以表明该节点已遍历过/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *原创 2013-12-26 11:42:01 · 630 阅读 · 0 评论 -
LeetCode之Pascal's Triangle
http://oj.leetcode.com/problems/pascals-triangle/输入行数,输出一个杨辉三角(帕斯卡三角), 如果rowNum ==0, 则输出 [ ]class Solution {public: vector > generate(int numRows) { vector > result;原创 2013-12-26 22:23:06 · 633 阅读 · 0 评论 -
LeetCode之Sort Colors
http://oj.leetcode.com/problems/sort-colors/按颜色排序,不能用内置的sort, 算法复杂度需要为 O(n)思路: 一次遍历求出r,w,b个数,然后根据 r,w, b修改A数组即可class Solution {public: void sortColors(int A[], int n)原创 2013-12-26 12:34:01 · 649 阅读 · 0 评论 -
LeetCode之Merge Sorted Array
http://oj.leetcode.com/problems/merge-sorted-array/合并两个数组,因为A的空间足够大,把B加到A后快排即可class Solution {public: void merge(int A[], int m, int B[], int n) { for(int i = m; i原创 2013-12-26 14:29:23 · 534 阅读 · 0 评论 -
LeetCode之Merge k Sorted Lists
http://oj.leetcode.com/problems/merge-k-sorted-lists/合并K个排序数组,由于对引用,指针的的理解不够深入,AC的过程略曲折思路: 每次遍历,取出最小值的那个节点,进行链接操作后 该节点 指向它的下一个节点,遍历完成之后返回结果。注意: WA的代码中,我是新建了一个指针变量用来存最小值节点的,指向下一节点更改的也是该指针,而原创 2013-12-26 20:42:08 · 632 阅读 · 0 评论 -
LeetCode之Rotate Image
http://oj.leetcode.com/problems/rotate-image/in-space算法,先按对焦先对转,然后左右对转http://oj.leetcode.com/problems/rotate-image/class Solution {public: void rotate(vector > &matrix) {原创 2013-12-26 22:43:03 · 594 阅读 · 0 评论 -
LeetCode之Best Time to Buy and Sell Stock II
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/与 Best Time>一天能进行一次交易,需要注意的是一次交易包含 购买,售出 两种行为这样在每次上升自序列开始时 购买股票, 上升子序列结束时售出股票,这样就能获取到所有子序列的收益例如对于上升子序列1,2,3,4 ,有两种方案原创 2013-12-25 22:47:45 · 752 阅读 · 0 评论 -
LeetCode之Same Tree
思路与上一道求二叉树深度的一样:1. 检查左右子树是否相同,且p,q节点的值相同2. 左右子树一个为NULL一个不为NULL时,返回false/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *righ原创 2013-12-25 11:32:49 · 627 阅读 · 0 评论 -
LeetCode之Two Sum
一开始三次WA的原因有如下几点:1. 没有看清 Please note that your returned answers (both index1 and index2) are not zero-based. 这个只是指 Zero-based array2. 上一条错误的理解成了 都是大于0的,因此错过了 [0,1,2,0],0 这种testcaseclass Solutio原创 2013-12-24 22:35:10 · 499 阅读 · 0 评论 -
LeetCode之Pascal's Triangle II
http://oj.leetcode.com/problems/pascals-triangle-ii/依然是关于杨辉三角的题,核心的公式就是 A i,j = A i-1,j + A i-1,j-1注意,必须从后往前遍历,不能从前往后遍历。 对于一矢量 [1,2,1] 来讲。从后往前遍历的结果是: [1,3,3]从前往后遍历的结果是: [1,3,4]原创 2013-12-30 10:34:26 · 620 阅读 · 0 评论 -
LeetCode之Remove Duplicates from Sorted Array II
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/删除数组的重复元素,最多可以重复两次。NOTICE: 重复两次时,数组的指针在循环结束后向后移动 两位class Solution {public: int removeDuplicates(int原创 2013-12-30 15:54:44 · 593 阅读 · 0 评论 -
LeetCode之Remove Duplicates from Sorted Array
题目要求删除数组中的重复元素,AC的思路依然类似于 快慢双指针class Solution {public: int removeDuplicates(int A[], int n) { int start_pos = 0; for(int i = 0; i < n; i++) { if(i+1 >= n || A原创 2013-12-30 10:55:42 · 578 阅读 · 0 评论 -
LeetCode之Gas Station
http://oj.leetcode.com/problems/gas-station/最初的想法是 两次遍历,复杂度O(n2), 导致TLE不能通过测试。复杂度为O(n)的DP方法如下。class Solution {public: int canCompleteCircuit(vector &gas, vector &cost) { int t原创 2013-12-30 16:30:29 · 658 阅读 · 0 评论 -
LeetCode之Climbing Stairs
http://oj.leetcode.com/problems/climbing-stairs/WA: 递归方法导致TLE错误class Solution {public: int climbStairs(int n) { // Start typing your C/C++ solution below // DO NOT wri原创 2013-12-30 16:48:13 · 715 阅读 · 0 评论 -
LeetCode之Sum Root to Leaf Numbers
http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/计算每一条路径的数字之和,需要注意的是,对于二叉树 1 / 2 / 3 来讲,其只有一条路径,123,原创 2013-12-30 22:35:25 · 621 阅读 · 0 评论 -
LeetCode之Remove Element
http://oj.leetcode.com/problems/remove-element/采用快慢指针的思想,而且,这是一种 原地算法。class Solution {public: int removeElement(int A[], int n, int elem) { int start_pos = 0; for(int i原创 2013-12-30 21:05:28 · 593 阅读 · 0 评论 -
LeetCode之Length of Last Word
http://oj.leetcode.com/problems/length-of-last-word/返回最后一个单词的长度,使用java的split方法即可public class Solution { public int lengthOfLastWord(String s) { String[] arr = s.split原创 2013-12-30 23:56:51 · 818 阅读 · 0 评论 -
LeetCode之Minimum Depth of Binary Tree
http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/这道题用来求二叉树的最小深度,注意有一下两点:The minimum depth is defined as the number of nodes along the shortest path from the root node d原创 2013-12-30 23:04:13 · 676 阅读 · 0 评论 -
LeetCode之Maximum Depth of Binary Tree
这是一道求 二叉树深度的题,一开始用递归的思路有严重的问题: 单纯为了递归二递归,没有把问题分解成可以递归的子问题。一开始一直不能AC的做法是: 递归遍历所有的路径长度,然后在ector中取最大值。参考了:http://www.cnblogs.com/GoAhead/archive/2012/05/22/2513847.html 问题就变成了:为了求得树的深度,可以先求左右子原创 2013-12-25 10:39:47 · 559 阅读 · 0 评论 -
LeetCode之Linked List Cycle
http://oj.leetcode.com/problems/linked-list-cycle/检验一个链表是否有环,题目唯一的要求是 without using extra space!思路是,给节点的val赋一个特殊的值来表明节点已经被遍历过。/** * Definition for singly-linked list. * struct Li原创 2013-12-25 23:08:58 · 532 阅读 · 0 评论 -
LeetCode之Search Insert Position
http://oj.leetcode.com/problems/search-insert-position/查询Insert position, 时间复杂度为 O(n), 分四种情况讨论1. 比A[0] 小2. 比A[n] 大3. 恰巧存在与target相等的数4. target在两个数的区间内class Solution {publi原创 2013-12-25 23:29:05 · 533 阅读 · 0 评论 -
LeetCode之Remove Duplicates from Sorted List
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/链表去重。需要考虑的特殊情况是: 输入为空链表,应该如何处理?所以要用 while(head != NULL) 而不是 while(1)/** * Definition for singly-linked list.原创 2013-12-26 10:17:37 · 567 阅读 · 0 评论 -
ListCode之Remove Nth Node From End of List
http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/采用双指针的思路1. 定义head1, head2 两个指针, head2先向前n步2. head1, head2 同时向前,直到head2为NULL3. head1 即为所要删除的节点(为了便于处理,使用了一个头指针,具体算法略有不同但原创 2013-12-27 21:17:28 · 727 阅读 · 0 评论