首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C-2数组,检查其中是否为子数组,并返回索引

C-2数组,检查其中是否为子数组,并返回索引
EN

Stack Overflow用户
提问于 2020-12-03 20:25:16
回答 1查看 170关注 0票数 0

我有一个任务是编写一个函数,它将获得2个数组及其大小:

代码语言:javascript
复制
int contain(int big[], int size_b, int small[], int size_s) 

程序应该检查小数组是否是大数组的子数组,如果是,则返回小数组中第一个数字的索引号,否则返回-1。

示例:2 4 61 5 8 5 56 89 3 -2

5 56 89 3 -2

函数应返回5。

9 5 12 7 8 -2 4 32 900 13

9 5 12 8 7

函数应返回-1。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-04 00:46:56

查看评论以查看解释

代码语言:javascript
复制
int contain(int big[], int size_b, int small[], int size_s) {
    int contains, k;
    for(int i = 0; i < size_b; i++){
        // assume that the big array contains the small array
        contains = 1;
        // check if the element at index i in the big array is the same as 
        // the first element in the small array
       if(big[i] == small[0]){
           // if yes, then we start form k = 1, because we already know that
           // the first element in the small array is the same as the element 
           // at index i in the big array
           k = 1;
           // we start to check if the next elements in the big array
           // are the same as the elements in the small array
           // (we start from i+1 position because we already know that the element 
           // at the position i is the same as the first element in the small array)
           for(int j = i + 1; j < size_b; j++){
               // range for k must be from 1 to size_s-1
               // if we reached the end of the small array or if the small 
               // array contains only one element then break the for loop
               if(k >= size_s - 1) {
                   break;
               }
               // if the element at the position j in the big array is different
               // from the element at the position k in the small array then we
               // flag that we did not find the sequence we were looking for (contains=0)
               if(big[j] != small[k]){
                   contains = 0;
                   break;
               }
               // increment k because we want the next element in the small array
               k++;
           }
           // if contains flag is not 0 that means we found the sequence we were looking
           // for and that sequence starts from index i in the big array
           if(contains) {
               return i;
           }
       }
    }
    // if the sequence we were looking for was not found
    // then -1 is returned
    return -1;
}

以下是不带注释的代码

代码语言:javascript
复制
int contain(int big[], int size_b, int small[], int size_s) {
    int contains, k;
    for(int i = 0; i < size_b; i++){
        contains = 1;
       if(big[i] == small[0]){
           k = 1;
           for(int j = i + 1; j < size_b; j++){
               if(k >= size_s - 1) {
                   break;
               }
               if(big[j] != small[k]){
                   contains = 0;
                   break;
               }
               k++;
           }
           if(contains) {
               return i;
           }
       }
    }
    return -1;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65125933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档