Leetcode


[leetcode] Remove Duplicates from Sorted Array II

Remove Duplicates from Sorted Array II Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 很奇怪的题,和此题的第一代没有本质区别。 只要增加一个变量count记录当前已经出现的相同数字的个数。 如果count>2,不移动start,如果小于等于2,copy A[i] to A[start++]。 class Solution { public: int […]


[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; […]