Commit 03aba8e4 by anqiwa 😲

### 2021/11/30 update

parent 2175f768
 /* * @lc app=leetcode id=238 lang=cpp * * [238] Product of Array Except Self */ // @lc code=start class Solution { public: vector productExceptSelf(vector &nums) { // first loop: from left to right // second loop: from right to left // 每个pass：=*res【i】，再 curr与nums中对应位置相乘 // idea: from left to right, the res[i] = 所有他右边数的乘积 // from right to left, the res[i] 补上所有他右边数的乘积 int curr = 1; vector res(nums.begin(), nums.end()); // -> for (int i = 0; i < nums.size(); ++i) { if (i == nums.size() - 1) { res[i] = curr; } else if (i == 0) { curr *= nums[i]; } else { res[i] = curr; curr *= nums[i]; } } // first pass // <- curr = 1; for (int i = nums.size() - 1; i >= 0; --i) { if (i == 0) { res[i] = curr; } else if (i == nums.size() - 1) { curr *= nums[i]; } else { res[i] *= curr; curr *= nums[i]; } } // second pass return res; } }; // @lc code=end //[1,2,3,4] // from left to right // curr = 6; // [1,1,2,6] // from right to left // curr = 124 //[1,2,3,4] // [24,12,8,6] // \ No newline at end of file
 /* * @lc app=leetcode id=26 lang=cpp * * [26] Remove Duplicates from Sorted Array */ // @lc code=start class Solution { public: int removeDuplicates(vector &nums) { if (nums.size() == 1) return 1; if (nums.size() == 0) return 0; int slow = 0; for (int fast = 1; fast < nums.size(); ++fast) { if (nums[fast] != nums[slow]) { nums[++slow] = nums[fast]; } } return slow + 1; } }; // @lc code=end // s //[0,1,2,3,4,2,2,3,3,4] // f \ No newline at end of file
50.pow-x-n.cpp 0 → 100644
 /* * @lc app=leetcode id=50 lang=cpp * * [50] Pow(x, n) */ // @lc code=start class Solution { public: double myPow(double x, int n) { // base case: if (x == 1 || n == 0) return 1; if (n == 1) return x; // use -(pow+1) to avoid MIN_Val case if (n < 0) return 1 / (x * myPow(x, -(n + 1))); double res = 1; while (n != 0) { if (n % 2 == 1) res *= x; x *= x; n /= 2; } return res; } }; // @lc code=end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!