Remove Duplicates from Sorted Array

Description https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Difficulty: 1.5/5.0 star related: Remove Duplicates from Sorted List Remove Element Analysis class Solution { public: int removeDuplicates(int A[], int n) { int currentPosition = 1; for (int i = 1; i < n; ++ i) if (A[i]...

Plus One

Description https://oj.leetcode.com/problems/plus-one/ Difficulty: 0.5/5.0 star Analysis class Solution { public: vector<int> plusOne(vector<int> &digits) { bool toPlus = true; for (int i = digits.size()-1; i >= 0 && toPlus; i -- ){ digits[i] = (digits[i] + 1)%10; toPlus = (digits[i] ==...

Best Time to Buy and Sell Stock I, II and III

Description https://oj.leetcode.com/problems/pascals-triangle-ii/ Difficulty: 2.0/5.0 star Related: https://oj.leetcode.com/problems/pascals-triangle/ Analysis and solution This is actually a subprocess of pascal-trangle i. However, as we want to reuse the space, we have more restrictions, such as we have to iterate from the right end...

Pascal's Triangle

Description https://oj.leetcode.com/submissions/detail/12004124/ Difficulty: 2.0/5.0 star Related: https://oj.leetcode.com/problems/pascals-triangle-ii/ Solution Here we use pascals-triangle-ii as a subprocess which speeds up the compution. It finishes in 4ms in leetcode OJ. class Solution { public: vector<vector<int> > generate(int numRows) { vector<vector<int> > pascalTriangle; if...

Merge Sorted Array

Description https://oj.leetcode.com/problems/merge-sorted-array/ Difficulty: 1.5/5.0 Analysis Unlike list, you cannot insert element in array A in constant time, but you have extra space at the end of A. So we start from putting the largest element at the end. class Solution...