Yearly Archives: 2015


[leetcode] Simplify Path

Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c” click to show corner cases. Corner Cases: Did you consider the case where path = “/../”? In this case, you should return “/”. Another corner case is the […]


[leetcode] Add Binary

Add Binary Given two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”. 二进制加法,注意进位。和字符串访问时的末尾对齐。 class Solution { public: string addBinary(string a, string b) { if(a.empty()) return b; else if(b.empty()) return a; int carry = 0; string sum; int i = […]


[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 { * […]