刷题成习惯以后感觉挺好的
Num 27 移除元素 Remove Element
跟那个排序去掉相同的一样,len标记然后新的不重复的直接放到len
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: int removeElement(vector<int>& nums, int val) { int len=0; for(int i=0;i<nums.capacity();i++) { if(nums[i]!=val) { nums[len++]=nums[i]; } } return len; } };
Num 35 搜索插入位置 Search Insert Position
注意一下 if (target>a[count++]),即使不成立,count也是会++的emm
另外,这道题可以用二分法。
我现在写出来的二分和非二分都需要特判,回头看一下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: int searchInsert(vector<int>& nums, int target) { if(nums.capacity()==0) return 0; if(target<=nums[0]) return 0; int l=0; int r=nums.capacity()-1; int mid=(l+r)/2; while(l<=r) { if(target==nums[mid]) return mid; else if(target<nums[mid]) r=mid-1; else l=mid+1; mid=(l+r)/2; } return mid+1; } };
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: int searchInsert(vector<int>& nums, int target) { if(nums.capacity()==0) return 0; if(nums[nums.capacity()-1]<target) return nums.capacity(); int count=0; while(count<nums.capacity() && target>nums[count++]); return count-1; } };
时间才能证明一切,选好了就尽力去做吧!