叶尔孤柏 2023-01-01 16:30 采纳率: 0%
浏览 27

LeetCode数组越界,但在自己电脑上OK

C语言在自己电脑上运行正确,但是LeetCode提交就会出现下列错误

Line 17: Char 27: runtime error: index -33 out of bounds for type 'char [9]' [solution.c]

是leetcode 36题
这是我的代码

// 36. Valid Sudoku
// Created by ear on 1/1/23.
//  char a[] = "string"; // the compiler puts {'s','t','r','i','n','g', 0} onto STACK
//  char *a = "string"; // the compiler puts just the pointer onto STACK
//                    // and {'s','t','r','i','n','g',0} in static memory area
#include <stdio.h>
#include <stdbool.h>
bool isValidSudoku(char board[9][9], int boardSize, int* boardColSize){ 
    char row[9][9] = {{'\0'},{'\0'},{'\0'},
                     {'\0'},{'\0'},{'\0'},
                     {'\0'},{'\0'},{'\0'}};
    char col[9][9] = {{'\0'},{'\0'},{'\0'},
                     {'\0'},{'\0'},{'\0'},
                     {'\0'},{'\0'},{'\0'}};
    char subbox[9][9] = {{'\0'},{'\0'},{'\0'},
                       {'\0'},{'\0'},{'\0'},
                       {'\0'},{'\0'},{'\0'}};
    int i, j, number, which_subbox;
    for(i = 0; i < boardSize; ++i){
        for(j = 0; j < *boardColSize; ++j){
            if(board[i][j] != '.'){
                number = board[i][j] - '0' - 1;
                which_subbox = i/3*3 + j/3;
                if((row[i][number] == 1) || (col[j][number] == 1) || (subbox[which_subbox][number] == 1))// 错误提示在这里
                    return false;
                else{
                    row[i][number] = 1;
                    col[j][number] = 1;
                    subbox[which_subbox][number] = 1;
                }
            }
        }
    }
    return true;
}
int main(){
    int boardSize = 9;
    int boardColSize = 9;
    char board[9][9] = {
            {'5','3','.','.','7','.','.','.','.'},
            {'6','.','.','1','9','5','.','.','.'},
            {'.','9','8','.','.','.','.','6','.'},
            {'8','.','.','.','6','.','.','.','3'},
            {'4','.','.','8','.','3','.','.','1'},
            {'7','.','.','.','2','.','.','.','6'},
            {'.','6','.','.','.','.','2','8','.'},
            {'.','.','.','4','1','9','.','.','5'},
            {'.','.','.','.','8','.','.','7','9'}
    };
  /*  // test board;
    for(int i = 0; i < 9; ++i){
        for(int j = 0; j < 9; ++j){
            printf("%c,", board[i][j]);
        }
        printf("\n");
    }*/
    bool ret = isValidSudoku(board, boardSize, &boardColSize);
    printf("%s\n", ret ? "true" : "false");
}

我自己在电脑上运行没有问题

img

提示错误应该就是数组越界,但是我完全不知道哪里越界了??谢谢你的帮助

  • 写回答

2条回答 默认 最新

  • 「已注销」 2023-01-01 17:05
    关注

    远程服务器的版本跟你的不一样

    评论

报告相同问题?

问题事件

  • 创建了问题 1月1日

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并