无题
数组理论
数组是存放在连续内存空间上的相同类型数据的集合
数组的下标是从0开始的
数组内存空间的地址是连续的
数组的元素是不能删除的,只能覆盖
Leetcode704 二分查找 Binary Search左闭右闭 [left,right]
while(left <= right)
if (nums[middle] > target) right要赋值为middle - 1. 因为nums[middle] 一定不是target所以查找的就是middle - 1
1234567891011121314151617class Solution {public: int search(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while (left <= right){ int middle = left + ((right - left) >> 1); ...
无题
Leetcode977 有序数组的平方 Square of a Sorted Array暴力解法
刚看到题想到的是暴力解法, 时间复杂度是O(nlogn)12345678910class Solution { public: vector<int> sortedSquares(vector<int>& nums) { for (int i = 0; i < nums.size(); i++) { nums[i] *= nums[i]; } sort(nums.begin(),nums.end()); return nums; } };
双指针法
因为数组有负数, 所以负数的平方很可能比正数的平方大. 所以我们只要比对快慢指针的平方对比谁大就放进result的数组里.
时间复杂度O(n)123456789101112131415161718class Solution ...
Hello World
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.
Quick StartCreate a new post1$ hexo new "My New Post"
More info: Writing
Run server1$ hexo server
More info: Server
Generate static files1$ hexo generate
More info: Generating
Deploy to remote sites1$ hexo deploy
More info: Deployment