天池 在线编程 滑动数独(滑动窗口)
生活随笔
收集整理的這篇文章主要介紹了
天池 在线编程 滑动数独(滑动窗口)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
描述
給定一個 3xn的矩陣 number,并且該矩陣只含有1到9的正整數。
考慮有一個大小為 3x3 滑動窗口,從左到右遍歷該矩陣 number, 那么該滑動窗口在遍歷整個矩陣的過程中會有n-2個。
現在你的任務是找出這些滑動窗口是否含有1到9的所有正整數
請返回一個長度為n-2的答案數組,如果第 i 個滑動窗口含有1到9的所有正整數,那么答案數組的第 i 個元素為true,否則為false
示例
輸入:[[1,2,3,2,5,7],[4,5,6,1,7,6],[7,8,9,4,8,3]] 輸出:[true,false,true,false] 解釋:第一個和第三個滑動窗口含有1到9所有數字,其他的滑動窗口不含有1到9的所有數字來源:https://tianchi.aliyun.com/oj/141744976105873771/153295019687743571
2. 解題
class Solution { public:/*** @param number: an only contains from 1 to 9 array* @return: return whether or not each sliding window position contains all the numbers for 1 to 9 */vector<bool> SlidingWindows(vector<vector<int>> &number) {// write your code hereint n = number[0].size();vector<bool> ans(n-2, false);vector<int> flag(10, 0);for(int i = 0; i < 3; i++){for(int k = 0; k < 2; k++){ //前3行,2列的數字的個數flag[number[i][k]]++;}}for(int j = 2; j < n; j++) { //右端點 的列加入for(int i = 0; i < 3; i++){flag[number[i][j]]++;}bool res = true;for(int x = 1; x <= 9; x++){ //檢查1-9 是不是都是1個if(flag[x] != 1){res = false;break;}}ans[j-2] = res;for(int i = 0; i < 3; i++){ // 左端點的列 j-2列 計數減去flag[number[i][j-2]]--;}}return ans;} };603ms C++
我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的天池 在线编程 滑动数独(滑动窗口)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 1701. 平均等待时
- 下一篇: 天池 在线编程 木材加工(二分查找)