首页 > 编程语言 > 详细

C++求连续的子数组和

时间:2021-06-02 22:48:06      阅读:16      评论:0      收藏:0      [点我收藏+]

给你一个整数数组 nums 和一个整数 k ,编写一个函数来判断该数组是否含有同时满足下述条件的连续子数组:

子数组大小 至少为 2 ,且
子数组元素总和为 k 的倍数。
如果存在,返回 true ;否则,返回 false 。

如果存在一个整数 n ,令整数 x 符合 x = n * k ,则称 x 是 k 的一个倍数。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/continuous-subarray-sum

示例 1:

输入:nums = [23,2,4,6,7], k = 6
输出:true
解释:[2,4] 是一个大小为 2 的子数组,并且和为 6 。

技术分享图片
#include "sstream"
#include "cstdlib"
#include "iostream"
#include "vector"
#include "unordered_map"
using namespace std;


class Solution {
public:
    static bool checkSubarraySum(vector<int> &nums, int k) {
        // 连续的子数组和
        int m = nums.size();
        if (m<2) {
            return false;
        }
        unordered_map<int, int> mp;
        mp[0] = 1;
        int remainder = 0;
        for (int i=0; i<m; i++) {
            remainder = (remainder + nums[i]) % k;
            if (mp.count(remainder)) {
                int prevIndex = mp[remainder];
                if (i - prevIndex >= 2) {
                    return true;
                }
            } else {
                mp[remainder] = i;
            }
        }
        return false;
    }
};

vector<string> split(string str, const char  c) {
    // 不支持“, ”、“; ”等带空格的字符串,支持类似‘,‘、‘;‘等这样的字符
    vector<string> sstr;
    int pos = 0;
    while (pos < str.length()) {
        int end;
        // 记住这种for的方式
        for (end = pos; (end < str.length()) && (str[end] != c); end++);
        sstr.push_back(str.substr(pos, end-pos));
        pos = end+1;
    }
    return sstr;
}

vector<string> splitSpace(string str) {
    // 去除空格
    istringstream strs(str);
    vector<string> words;
    string word;
    while (strs >> word) {
        words.push_back(word);
    }
    return words;
}

vector<int> stringToInt(vector<string> &ss2) {
    vector<int> dest;
    for (int i=0; i<ss2.size(); i++) {
        dest.push_back(atoi(ss2[i].c_str()));
    }
    return dest;
}

int main() {
    string inputs;
    while (getline(cin, inputs)) {
        vector<string> ss1 = splitSpace(inputs);
        // nums
        string numString = ss1[2];
        // 从第一个[的位置开始的1个字符替换成""
        string numString1 = numString.replace(numString.find("["), 1, "");
        // 删除],
        string numString2 = numString1.replace(numString1.find("]"), 2, "");
        // k
        string kString = ss1[5];
        // #include <cstdlib>
        int k = atoi(kString.c_str());
        // 不支持“, ”、“; ”等带空格的字符串,支持类似‘,‘、‘;‘等这样的字符
        vector<string> ss2 = split(numString2, ,);
        vector<int> dest = stringToInt(ss2);
        // 把bool型输出成true或者false,而不是1,0
        cout << boolalpha << Solution::checkSubarraySum(dest, k) << endl;
    }
    return 0;
}
View Code

c++实现split()函数的功能:

vector<string> split(string str, const char  c) {
    // 不支持“, ”、“; ”等带空格的字符串,支持类似‘,‘、‘;‘等这样的字符
    vector<string> sstr;
    int pos = 0;
    while (pos < str.length()) {
        int end;
        // 记住这种for的方式
        for (end = pos; (end < str.length()) && (str[end] != c); end++);
        sstr.push_back(str.substr(pos, end-pos));
        pos = end+1;
    }
    return sstr;
}

去重空格:

vector<string> splitSpace(string str) {
    // 去除空格
    istringstream strs(str);
    vector<string> words;
    string word;
    while (strs >> word) {
        words.push_back(word);
    }
    return words;
}

string向量转换为int向量:

vector<int> stringToInt(vector<string> &ss2) {
    vector<int> dest;
    for (int i=0; i<ss2.size(); i++) {
        dest.push_back(atoi(ss2[i].c_str()));
    }
    return dest;
}

测试用例连续输入或一起输入:

int main() {
    string inputs;
    while (getline(cin, inputs)) {
        vector<string> ss1 = splitSpace(inputs);
        // nums
        string numString = ss1[2];
        // 从第一个[的位置开始的1个字符替换成""
        string numString1 = numString.replace(numString.find("["), 1, "");
        // 删除],
        string numString2 = numString1.replace(numString1.find("]"), 2, "");
        // k
        string kString = ss1[5];
        // #include <cstdlib>
        int k = atoi(kString.c_str());
        // 不支持“, ”、“; ”等带空格的字符串,支持类似‘,‘、‘;‘等这样的字符
        vector<string> ss2 = split(numString2, ,);
        vector<int> dest = stringToInt(ss2);
        // 把bool型输出成true或者false,而不是1,0
        cout << boolalpha << Solution::checkSubarraySum(dest, k) << endl;
    }
    return 0;
}

 

C++求连续的子数组和

原文:https://www.cnblogs.com/zhangwenzhi/p/14843015.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!