Leetcode刷题顺序记录(C++)——数组篇

27.要求 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。

int removeElement(vector<int>& nums, int val) {
        int len = 0;
        for(int i = 0;i < nums.size();i++){
            if(nums[i]!=val){
                nums[len]=nums[i];
                len++;
            }
        }
        return len;
    }

26.删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度

int removeDuplicates(vector<int>& nums) {
        if(nums.size()<2) return nums.size();
        int j = 0;
        for(int i = 1;i < nums.size();i++){
            if(nums[i]!=nums[j]){
                j++;
                nums[j]=nums[i];
            } 
        }
        return ++j;
    }

过程中有遇错: Line 941: Char 9: runtime error: reference binding to null pointer of type ‘const int’ (stl_vector.h)
原因是:没有考虑到数组为空的情况。

80.原地 删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度

int removeDuplicates(vector<int>& nums) {
        if(nums.size() < 3) return nums.size();
        int j = 1;
        for(int i = 2;i < nums.size();i++){
            if(nums[i] != nums[j-1])
                nums[++j]=nums[i];
        }
        return ++j;
    }

189.给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负03数。
方法一:利用额外数组

void rotate(vector<int>& nums, int k) {
    int j = nums.size();
    k = k % j;
    int s = k;
    int *temp = new int [k];
    while(k > 0){
        temp[--k] = nums[--j];
    }
    for(int i = nums.size() - 1;i >= s;i--) 
        nums[i]=nums[--j];
    while(k < s){
        nums[k++]=temp[k];
    }
}

方法二:(参考解法)环状替换

 void rotate(vector<int>& nums, int k) {
      int n = nums.size();
      k = k % n;
      int temp;
      int count = 0;//记录替换次数,总共需要n次
      for(int i = 0;count < n;i++){
          int next = i;
          int pre = nums[i];
          do{
              next = (next + k) % n;
              temp = nums[next];
              nums[next] = pre;
              pre = temp;
              count++;
          }while(next != i);
      }
    }``

方法三:反转

 void rotate(vector<int>& nums, int k) {
        int n = nums.size() - 1;
        k = k % (n+1);
        ro(nums,0,n);
        ro(nums,0,k - 1);
        ro(nums,k,n);
    }
    void ro(vector<int>& nums,int begin,int end){
        int temp;
        while(begin < end){
            temp = nums[begin];
            nums[begin] = nums[end];
            nums[end] = temp;
            begin++;
            end--;
    }
 }

41.一个未排序的整数数组,找出其中没有出现的最小的正整数。

  int firstMissingPositive(vector<int>& nums) {
        int len = nums.size();
        int i = 0;
        int *arr = new int [len+1]();//初始化为0
        for(;i < len;i++){
            if(nums[i] > 0 && nums[i]<len+1)
                arr[nums[i]-1]++;
        }
        for(i = 0;i < len+1;i++){
            if(arr[i]==0)
                break;
        }
        return i+1;
    }

299.猜数字游戏,该游戏规则如下:
你写出一个秘密数字,并请朋友猜这个数字是多少。
朋友每猜测一次,你就会给他一个提示,告诉他的猜测数字中有多少位属于数字和确切位置都猜对了(称为“Bulls”, 公牛),有多少位属于数字猜对了但是位置不对(称为“Cows”, 奶牛)。
朋友根据提示继续猜,直到猜出秘密数字。
请写出一个根据秘密数字和朋友的猜测数返回提示的函数,返回字符串的格式为 xAyB ,x 和 y 都是数字,A 表示公牛,用 B 表示奶牛。

xA 表示有 x 位数字出现在秘密数字中,且位置都与秘密数字一致。
yB 表示有 y 位数字出现在秘密数字中,但位置与秘密数字不一致。
请注意秘密数字和朋友的猜测数都可能含有重复数字,每位数字只能统计一次。

public :
    string getHint(string secret, string guess) {
        int s[10] = {};
        int g[10] = {};
        int bulls = 0;
        int cows = 0;
        for(int i = 0;i < secret.size();i++){
            if(secret[i] == guess[i])
                bulls++;
        }
        for(int i = 0;i < secret.size();i++){
            s[secret[i]-'0']++;
        }
        for(int i = 0;i < guess.size();i++){
            g[guess[i]-'0']++;
        }
        for(int i = 0;i < 10;i++){
            if(g[i] < s[i])
                cows += g[i];
            else
                cows += s[i];
        }
    return to_string(bulls) + "A" + to_string(cows-bulls) + "B";
    }

134.There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station’s index if you can travel around the circuit once in the clockwise direction, otherwise return -1.

Note:

If there exists a solution, it is guaranteed to be unique.
Both input arrays are non-empty and have the same length.
Each element in the input arrays is a non-negative integer.

int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
       int curgas = 0,curcost = 0;
       int sum = 0;//目前总的汽油值
       int start = 0;//起始站
       for(int i = 0;i < gas.size(); i++){
           curgas = gas[i] - cost[i];
           sum += curgas;
           curcost += curgas;
           if(curcost < 0){
               start = i + 1;
               curcost = 0;
           }
       }
       if(sum >= 0)//只有最后总值是大于0的 才有返回结果
           return start;
       else
           return -1;
   } 

118.Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
主要关注vector的用法

   vector<vector<int>> generate(int numRows) {
        vector<vector<int>>pas;
        for(int i = 0;i < numRows;i++){
                pas.push_back(vector<int>(i+1,1));//每行加一个,每个都赋值为1
            for(int j = 1;j < i;j++){
                pas[i][j] = pas[i-1][j] + pas[i-1][j-1];
            }
        }
        return pas;
    }

119.Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal’s triangle.

Note that the row index starts from 0.

 vector<int> getRow(int rowIndex) {//利用一行与前一行之间的关系,保留下一行的结果
        vector<int> pas(rowIndex + 1);
        for(int i = 0;i <= rowIndex;i++){
            pas[i] = 1;
            for(int j = i - 1;j > 0;j--){
                pas[j] = pas[j] + pas[j - 1];
            }
        }
        return pas;
    }

时间 空间优化

vector<int> getRow(int rowIndex) {//利用一行与前一行之间的关系,保留下一行的结果
        vector<int> pas(rowIndex + 1 , 0);
        pas[0] = 1;
        for(int i = 1;i <= rowIndex;i++){
            for(int j = i;j > 0;j--){
                pas[j] = pas[j] + pas[j - 1];
            }
        }
        return pas;
    }

题源 力扣(LeetCode).
刷题顺序来源.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值