LeetCode----Linear
文章平均质量分 74
sumhhhh1234
这个作者很懒,什么都没留下…
展开
-
Remove the duplicates from sorted array
其实这个题目有一个关键就是sorted array 如果不是sorted的话 可以用array index bit的类似方法解决这个问题Remove duplicates from sorted array 11. Do not allocate extra space for another array, you must do this in place with constant原创 2016-04-18 04:42:21 · 247 阅读 · 0 评论 -
Merge two sorted linked list
method 1 dummy node/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {pub原创 2017-10-02 11:33:49 · 264 阅读 · 0 评论 -
2 sum
method 2. O(n2)的方法class Solution {public: vector twoSum(vector& nums, int target) { vector result; for (int i = 0; i < nums.size(); ++i) { for (int j = i+1;原创 2017-10-02 08:17:28 · 221 阅读 · 0 评论 -
Palindrome 系列
1. Palindrome linked list这个解法很不错呀 利用了reverse linked list的方法 + 两个指针class Solution {public: ListNode* reverse(ListNode* node) { ListNode* prev = NULL; while (node != NULL)原创 2016-03-13 05:31:07 · 315 阅读 · 0 评论 -
Reverse Integer
又是一个简单的但是都没一次通过的题目, 第一遍刷题就这么差么自己丑陋的代码 so uglyclass Solution {public: int reverse(int x) { if ((x >= 2147483647) || (x <= -2147483648) || x == 0) { return 0;原创 2016-02-25 12:53:21 · 331 阅读 · 0 评论 -
remove duplicate from sort array 2
这个貌似复杂了点丑陋的代码class Solution {public: int removeDuplicates(vector& nums) { if (nums.size() < 3) { return nums.size(); } int c = 0; int i = 1原创 2016-04-18 06:09:36 · 231 阅读 · 0 评论 -
Odd even Linked list
第一次出现了两次错误class Solution {public: ListNode* oddEvenList(ListNode* head) { bool flag = false; if (head == NULL || head->next == NULL) { return head; }原创 2016-03-09 13:23:50 · 278 阅读 · 0 评论 -
Reverse Linked List 2
1.dummy Node 的使用感觉不是简单的就是 new一个dummy node 然后 dummy->next = head; 就好了, 这样是虚的最关键的是 dummy->next = head; 之后 head 要再从dummy 开始 head = dummy; 这样的dummy才是有效的 并且连续的2. reverse linked list 2 其实和1 很像 核心部原创 2016-01-31 12:45:49 · 238 阅读 · 0 评论 -
Linked List Cycle
This question is just something like if you know the trick then you can do it, otherwise just no way to solve it.两个指针Note:1. The faster one move 2 steps and slower one move one step each time2原创 2016-02-01 09:34:59 · 251 阅读 · 0 评论 -
Remove duplicate in sorted linked list 1
Remove duplicate in linked list 1Note1. when you want access n->next you need to make sure thatn is not NULL, but when you want to access n->next->val, you need to make sure thatn->next is not原创 2016-02-01 10:34:32 · 320 阅读 · 0 评论 -
Remove duplicates from sorted listed 2
这次又没有能一次通过....so sad.....solution 1:test case 最基本的[1,2,3,4,5,6] 没有能通过 然后想到了加一个flag class Solution {public: ListNode* deleteDuplicates(ListNode* head) { if (head == NULL || head-原创 2016-02-03 12:26:06 · 229 阅读 · 0 评论 -
Reorder Linked List
方法1 一次AC了class Solution {public: void reorderList(ListNode* head) { if (head == NULL || head->next == NULL || head->next->next == NULL) { return; } L原创 2016-03-14 03:30:50 · 288 阅读 · 0 评论 -
Remove Linked List Elements
dummy node的妙用1.切记 用了dummy node以后 要head 从dummy开始,把他们连接起来ListNode* dummy = new ListNode(0);dummy->next = head;head = dummy;2. 用temp然后delete temp 完成删除class Solution {public: ListNode* remov原创 2016-03-09 13:30:17 · 267 阅读 · 0 评论