Leetcode Array(27,26)

27 Remove Element

给定一个数组nums和一个val值,移除数组中所有的val值,且保证最后数组中的元素在正确的位置,并返回这个新数组的长度。
要求不能另外开辟一个新数组,要求空间复杂度是 O ( 1 ) O(1) O(1)
元素的位置可以改变,超出新数组长度的部分是什么都没关系。
例子:

Given nums = [0,1,2,2,3,0,4,2], val = 2,

Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.

Note that the order of those five elements can be arbitrary.

It doesn't matter what values are set beyond the returned length.

思路:
遍历数组,维护一个变量cnt。若当前元素和val不同cnt增加;若相同,则将后面的元素统一向前移动一个位置(当前位置被后面的元素覆盖)。此时让遍历长度减一,从当前位置继续遍历。

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int len=nums.size();
        int cnt=0;
        int i=0;
        
        for(i=0;i<len;++i){
            if(nums[i]!=val) cnt++;
            else{
                for(int j=i;j<len-1;j++){
                    nums[j]=nums[j+1];
                }
                len--;
                i--;
            } 
        }
        return cnt;
    }
};

当点开这道题时,发现accepted才44.7%,看了一下题,觉得很easy啊,大家是怎么肥事,然鹅,最后我第四遍提交才accepted。。。。。对不起给大家拖后腿了!

26 Remove Duplicates from Sorted Array

给定一个有序数组,要求移除重复的元素,且要求空间复杂度也为 O ( 1 ) O(1) O(1)
例子:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

思路:同27题一样,只是val就是数组中的,判断当前元素是否和它后面的元素相等,相等就把后面的元素往前移,不等就把cnt增加1.

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        
        int len=nums.size();
        if(len<=1) return len;
        int cnt=1;
        int i,j;
        
        for(i=0;i<len-1;++i){
            if(nums[i]!=nums[i+1]) cnt++;
            else{
                for(j=i;j<len-1;++j){
                    nums[j]=nums[j+1];
                }
                i--;
                len--;
            }
        }
        return cnt;
    }
};

需要注意的是要记得判断数组长度,一开始我没有判断,就报了下面的错:

reference binding to null pointer of type 'const value_type'

查询了一下,可能是因为没有做边界判断或者for循环那里的边界有问题等,加上判断之后就通过了。

80 Remove Duplicates from Sorted Array II

277 Find the Celebrity

189 Rotate Array

41 First Missing Positive

299 Bulls and Cows

134 Gas Station

118 Pascal’s Triangle

119 Pascal’s Triangle II

169 Majority Element

229 Majority Element II

274 H-Index

275 H-Index II Binary Search

243 Shortest Word Distance

244 Shortest Word Distance II

245 Shortest Word Distance III

217 Contains Duplicate

219 Contains Duplicate II 很少考

220 Contains Duplicate III 很少考

55 Jump Game

45 Jump Game II

121 Best Time to Buy and Sell Stock

122 Best Time to Buy and Sell Stock II

123 Best Time to Buy and Sell Stock III

188 Best Time to Buy and Sell Stock IV

309 Best Time to Buy and Sell Stock with Cooldown

11 Container With Most Water

42 Trapping Rain Water

334 Increasing Triplet Subsequence

128 Longest Consecutive Sequence

164 Maximum Gap Bucket

287 Find the Duplicate Number

135 Candy 很少考

330 Patching Array 很少考

这也太多题了吧!而且这还只是数组里比较基础的题!加油填坑!
P.S 这个做题顺序参考了https://cspiration.com/leetcodeClassification#10311

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值