随笔分类 -  LeetCode OJ

上一页 1 2 3 下一页

LeetCode --- Insertion Sort List
摘要:题目链接实现链表的插入排序附上代码: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(i... 阅读全文

posted @ 2014-05-29 01:24 Stomach_ache 阅读(131) 评论(0) 推荐(0)

LeetCode --- Reverse Integer
摘要:题目链接从代码的健壮性考虑, 应该顾及到把数字翻转后发生溢出的情况,但是此题中并没有这种测试数据。附上代码: 1 class Solution { 2 public: 3 int reverse(int x) { 4 int tmp = abs(x); 5 i... 阅读全文

posted @ 2014-05-27 09:59 Stomach_ache 阅读(104) 评论(0) 推荐(0)

LeetCode --- Add Two Numbers
摘要:题目链接说来算是个基础题目,可是还是各种CE,各种WA,基础还是不扎实。附上代码: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *ne... 阅读全文

posted @ 2014-05-27 09:29 Stomach_ache 阅读(138) 评论(0) 推荐(0)

LeetCode --- Climbing Stairs
摘要:题目链接简单递推附上代码: 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 int f0 = 1, f1 = 1, f2 = 1; 5 for (int i = 2; i <= n; i++)... 阅读全文

posted @ 2014-05-27 08:28 Stomach_ache 阅读(156) 评论(0) 推荐(0)

LeetCode --- Best Time to Buy and Sell Stock II
摘要:题目链接附上代码: 1 class Solution { 2 public: 3 int maxProfit(vector &prices) { 4 unsigned int len = prices.size(); 5 if (len == 0) retur... 阅读全文

posted @ 2014-05-26 23:50 Stomach_ache 阅读(111) 评论(0) 推荐(0)

LeedCode --- Best Time to Buy and Sell Stock
摘要:题目链接题意: find the maximum positive difference between the price on the ith day and the jth day附上代码: 1 class Solution { 2 public: 3 int maxProfit(ve... 阅读全文

posted @ 2014-05-26 17:19 Stomach_ache 阅读(156) 评论(0) 推荐(0)

LeetCode --- Jump Game II
摘要:题目链接题目描述:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents ... 阅读全文

posted @ 2014-05-21 13:34 Stomach_ache 阅读(112) 评论(0) 推荐(0)

Leetcode ---- Swap Nodes in Pairs
摘要:题目链接题意:给出单链表头指针,要求交换链表中每一对相邻的结点.注意:不可以改变链表中结点的值,只可以使用常量空间.附上代码: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val;... 阅读全文

posted @ 2014-05-19 18:24 Stomach_ache 阅读(138) 评论(0) 推荐(0)

LeetCode --- Linked List Cycle II
摘要:题目链接题意: 给出单链表,判断是否存在环,如果存在要求输出环开始的结点.思路及证明:It is a famous known problem Hare and Tortoise.Length of head to cycle started node: xLength of the cycle: ... 阅读全文

posted @ 2014-05-18 11:51 Stomach_ache 阅读(111) 评论(0) 推荐(0)

LeetCode -- Linked List Cycle
摘要:题目链接题意: 给出单链表, 判断是否存在环.方法就是大步小步...附上代码: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next... 阅读全文

posted @ 2014-05-15 22:54 Stomach_ache 阅读(111) 评论(0) 推荐(0)

LeetCode---Remove Nth Node From End of List
摘要:题目链接题意: 给出单链表头指针head和整数n, 要求删除链表的倒数第n个结点, 这里n保证是合法的输入.我的思路....其实我没大明白题目建议的one pass是什么意思, 可能就是遍历一遍链表的, 不过我还是秉着能A掉就万岁的心态...我还是首先记录了链表的长度, 然后删除第len - n +... 阅读全文

posted @ 2014-05-13 19:28 Stomach_ache 阅读(248) 评论(0) 推荐(0)

LeetCode---Remove Duplicates from Sorted List II
摘要:题目链接题意: 给出单链表的head指针, 要求去除链表中所有出现重复的元素, 如1->2->3->3->4->4->5, 返回1->2->5这题纠结了有两天, 重要的是把思路想清楚然后就可以痛苦的A掉, 不然老是会绕来绕去...我的大体思路是这样的: 使用三个指针 pre保存链表中前一个没有出现重... 阅读全文

posted @ 2014-05-12 22:04 Stomach_ache 阅读(124) 评论(0) 推荐(0)

LeetCode --- Plus One
摘要:题目链接题意:给出一个以数组形式表示的数, 求该数加1后的结果,同样以数组形式返回。附上代码: 1 class Solution { 2 public: 3 vector plusOne(vector &digits) { 4 unsigned int len = digit... 阅读全文

posted @ 2014-05-10 11:14 Stomach_ache 阅读(151) 评论(0) 推荐(0)

LeetCode --- Pascal's Triangle II
摘要:题目链接题意:在杨辉三角中,给定整数k,输出第k行。附上代码: 1 class Solution { 2 public: 3 vector getRow(int rowIndex) { 4 vector ans(rowIndex+1); 5 // 注意第0行为... 阅读全文

posted @ 2014-05-09 12:24 Stomach_ache 阅读(117) 评论(0) 推荐(0)

LeetCode --- Gray Code
摘要:题目链接一道关于格雷码的题目, 给出n, 输出n位的格雷码。 关于格雷码的详细说明和计算方法请右键:Wiki附上代码: 1 class Solution { 2 public: 3 vector grayCode(int n) { 4 vector ans(1); 5 ... 阅读全文

posted @ 2014-05-08 18:50 Stomach_ache 阅读(124) 评论(0) 推荐(0)

LeetCode --- Two Sum
摘要:题目链接附上代码: 1 #include 2 #include 3 #include 4 using namespace std; 5 6 class Solution { 7 public: 8 vector twoSum(vector &numbers, int target) ... 阅读全文

posted @ 2014-05-08 11:33 Stomach_ache 阅读(83) 评论(0) 推荐(0)

LeetCode --- Pascal's Triangle
摘要:题目链接杨辉三角附上代码: 1 class Solution { 2 public: 3 vector > generate(int numRows) { 4 vector > ans(numRows); 5 for (int i = 0; i 0) {11... 阅读全文

posted @ 2014-05-08 11:28 Stomach_ache 阅读(89) 评论(0) 推荐(0)

LeetCode -- Remove Duplicates from Sorted List
摘要:题目链接简单题,就是从单链表中删除重复元素。附上代码: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * L... 阅读全文

posted @ 2014-05-08 10:44 Stomach_ache 阅读(116) 评论(0) 推荐(0)

LeetCode --Binary Tree Level Order Traversal II
摘要:题目链接在这题各种RE和WA。 方法上就是BFS, 还是基础不扎实的原因,很明显的一点就是这里使用二维vector, 开始的时候我竟然没有给ans分配空间,然后直接push_back, 导致RE到死。这点是必须引起注意的!附上代码: 1 /** 2 * Definition for binary ... 阅读全文

posted @ 2014-05-08 10:28 Stomach_ache 阅读(195) 评论(0) 推荐(0)

LeetCode---Gas Station
摘要:明知道明早要考试,可还是忍不住打开电脑想水一题。不过还是在这题上WA了好几次....看见自己有多渣!有个道理发现了很久,直到今天才意识到它的严重性,反复的做题只能让你不断的巩固已有的知识,而对于新知识的探索是少只又少,所以必须要通过多读书来获取新的知识,这样才能够扩充知识面。而之前的我就是忽略了读书... 阅读全文

posted @ 2014-05-03 18:30 Stomach_ache 阅读(166) 评论(0) 推荐(0)

上一页 1 2 3 下一页

导航