c++编程练习(2)

7、左旋转字符串

在这里插入图片描述

class Solution {
public:
    string reverseLeftWords(string s, int n) {
        n %= s.length();
        string str3 = s.substr(n,(s.length())) +s.substr(0,n);
        return str3;
        
    }
};

笔记:

int main(int argc, char *argv[])
{
    //string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串
    string str = "hahahah";

    cout << str.substr(2,10) <<endl;//如果长度超出字符串原有长度,则有多少输出多少。
    //字符串的插入和删除
    //string& insert(int pos, const char* s); //插入字符串
    //string& insert(int pos, const string& str); //插入字符串
    //string& insert(int pos, int n, char c);//在指定位置插入n个字符c
    //string& erase(int pos, int n = npos);//删除从Pos开始的n个字符
    str.insert(0,"ds");
    cout << str << endl;
    str.erase(0,2);
    cout << str << endl;

    //string和c风格的字符串转换
    string str1;//对象
    char *str2 ="hello str";
    //将char * 转成 string (直接完成)
    str1 = str2;
    cout<<str1<<endl;//hello str
    string str3="hello str3";
    //不能直接将string 转换成 char * 必须借助string中的c_str方法完成
    //char *str4 = str3;//err
    char *str4 = (char *)(str3.c_str());
    cout<<str4<<endl;//"hello str3
    return 0;
}

8、数组中重复的数字

在这里插入图片描述

#include <vector>
#include<algorithm>
 
class Solution {
public:
    int findRepeatNumber(vector<int>& nums) {
        sort(nums.begin(),nums.end(),[](int a,int b){
            return a>b;
        });
        int i = 0;
        while(i < nums.size()-1){
            if(nums[i] == nums[i+1]){
                break;
            }else{
                i = i + 1;
            }
        }
        return nums[i];
    }
};

9、在排序数组中查找数字I

在这里插入图片描述

#include <vector>
#include<algorithm>

class Solution {
public:
    int search(vector<int>& nums, int target) {
        vector<int>::iterator it = find(nums.begin(),nums.end(),target);
        if(it == nums.end()){
            return 0;
        }
        int n = &*it - &nums[0];
        int i = 1;
        while(n+i<nums.size() && nums[n+i] == nums[n]){
            i += 1;
        }
        return i; 
    }
};

笔记:

#include <iostream>
#include <vector>

#include<algorithm>
using namespace std;

class Solution2 {
public:
    int search(vector<int>& nums, int target) {
        vector<int>::iterator it = find(nums.begin(),nums.end(),target);//利用find函数找出target的迭代器的位置
        if(it == nums.end()){
            return 0;
        }
        //算出迭代器所在的下标
        //可以使用两种方式 (1) int n = &*it - &nums[0];  (2)  int n = it - nums.begin();
        //两种方式不可以混用,因为类型不同 &nums[0]的类型为int*,而nums.begin()的类型为vector<int>::iterator
        int n = it - nums.begin();
        cout << n << endl;
        cout << &nums[0] << endl;
        //cout << nums.begin() << endl;该方式是错的,但vector的迭代器是随机访问迭代器,因此迭代器+n可以通过编译,就是随机访问迭代器,因此int n = it - nums.begin();是可以的
        int i = 1;
        int res = 1;
        while(n+i < nums.size() && nums[n+i] == nums[n]){
            res += 1;
            i += 1;
        }
        return res;
    }
};



int main(int argc, char *argv[])
{

    vector<int> v;
    v.push_back(5);
    v.push_back(7);
    v.push_back(7);
    v.push_back(8);
    v.push_back(8);
    v.push_back(10);
    Solution2 s = Solution2();
    int a = s.search(v,8);
    cout << a << endl;
    return 0;
}

10、0~n-1中缺失的数字

在这里插入图片描述
方法一 遍历

#include <vector>
class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int i = 0;
        for(vector<int>::iterator it = nums.begin();it < nums.end();it++){
            if(*it == i){
                i++;
            }else{
                break;
            }
        }
        return i;
    }
};

方法二 位运算
思路:数组 nums 中有 n−1 个数,在这 n−1 个数的后面添加从 0 到 n−1 的每个整数,则添加了 n 个整数,共有 2n−1 个整数。在 2n - 1 个整数中,缺失的数字只在后面 n 个整数中出现一次,其余的数字在前面 n−1 个整数中(即数组中)和后面 n个整数中各出现一次,即其余的数字都出现了两次。根据出现的次数的奇偶性,可以使用按位异或运算得到缺失的数字。由于上述 2n−1 个整数中,缺失的数字出现了一次,其余的数字都出现了两次,因此对上述2n−1 个整数进行按位异或运算,结果即为缺失的数字。

#include <vector>
class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int res = 0;
        int n = nums.size();
        for(int i = 0; i < n; i++){
            res ^= nums[i];
        }
        for(int i = 0; i<=n; i++){
            res ^= i;
        }
        return res;
    }
};

11、二维数组中查找

在这里插入图片描述
解题思路:
在这里插入图片描述
注意:从右上角找不论值是比要找的值大还是小,总会有一行被跳过,和遍历情况比速度要快得多。

#include <vector>
class Solution {
public:
    bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {

        if(matrix.size() == 0){
            return false;
        }
        
        int y = matrix[0].size();
        int x = matrix.size();
        int val1 = 0;
        int val2 = y-1;
        while(val1 < x && val2 >=0){
            if(matrix[val1][val2] == target){
                return true;
            }
            if(matrix[val1][val2] > target){
                val2--;
            }else{
                val1++;
            }
        }
        return false;
        
    }
};

12 、二维数组中的查找

在这里插入图片描述

class Solution {
public:
    int minArray(vector<int>& numbers) {
        int low = 0;
        int high = numbers.size() - 1;
        while (low < high) {
            int pivot = low + (high - low) / 2;
            if (numbers[pivot] < numbers[high]) {
                high = pivot;
            }
            else if (numbers[pivot] > numbers[high]) {
                low = pivot + 1;
            }
            else {
                high -= 1;
            }
        }
        return numbers[low];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值