Blog


[leetcode] Plus One

Plus One Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. Show Tags 注意进位,从vector末尾扫向开头。 class Solution { public: vector<int> plusOne(vector<int> &digits) { if(digits.size() == 0) return digits; […]


[leetcode] Rotate List

Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 陷阱挺多,需要小心。 1. 如果链表为空或k==0,return head; 2. 如果k>链表长度count, k = k % count; /** * Definition for singly-linked list. * struct ListNode { * […]


[leetcode] Insert Interval

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9]. Example 2: Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and […]


[leetcode] Merge Intervals

Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 10/7/2015 update Runtime error, quite strange. I don’t figure out why yet. /** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) […]