Leetcode Weekly Contest 86

Weekly Contest 86

A:840. 矩阵中的幻方

3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等。

给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 “幻方” 子矩阵?(每个子矩阵都是连续的)。

直接模拟即可,本来是签到题,由于粗心,浪费了时间。

 1 class Solution {
 2 public:
 3     int numMagicSquaresInside(vector<vector<int>>& grid) {
 4         vector<vector<int> >a = grid;
 5         int ans = 0;
 6         for(int i = 0; i + 2 < a.size(); i++)
 7         {
 8             for(int j = 0; j + 2 < a[0].size(); j++)
 9             {
10                 set<int>s;
11                 bool flag = 1;
12                 for(int ii = i; ii <= i + 2; ii++)
13                 {
14                     for(int jj = j; jj <= j + 2; jj++)
15                     {
16                         if(a[ii][jj] >= 10 || a[ii][jj] <= 0)
17                         {
18                             flag = 0;
19                         }
20                         s.insert(a[ii][jj]);
21                     }
22                 }
23                 if(flag && s.size() == 9)
24                 {
25                     int a1 = a[i][j] + a[i + 1][j + 1] + a[i + 2][j + 2];
26                     int a2 = a[i][j + 2] + a[i + 1][j + 1] + a[i + 2][j];
27                     //cout<<a1<<" "<<a2<<endl;
28                     if(a1 == a2 && a1 == 15)
29                     {
30                         for(int ii = i; ii <= i + 2; ii++)
31                         {
32                             int a3 = a[ii][j] + a[ii][j + 1] + a[ii][j + 2];
33                             if(a3 != 15)flag = 0;
34                         }
35                         for(int ii = j; ii <= j + 2; ii++)
36                         {
37                             int a3 = a[i][ii] + a[i + 1][ii] + a[i + 2][ii];
38                             if(a3 != 15)flag = 0;
39                         }
40                         if(flag)ans++;
41                     }
42                 }
43             }
44         }
45         return ans;
46     }
47 };

B:841. 钥匙和房间

有 N 个房间,开始时你位于 0 号房间。每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间。

在形式上,对于每个房间 i 都有一个钥匙列表 rooms[i],每个钥匙 rooms[i][j] 由 [0,1,...,N-1] 中的一个整数表示,其中 N = rooms.length。 钥匙 rooms[i][j] = v 可以打开编号为 v 的房间。

最初,除 0 号房间外的其余所有房间都被锁住。

你可以自由地在房间之间来回走动。

如果能进入每个房间返回 true,否则返回 false

直接用BFS即可,到达每一点,比赛的时候用并查集也过了,但是后来想这是有向边,并查集合并的是无向边,应该是数据水的原因

 1 class Solution {
 2 public:
 3     bool canVisitAllRooms(vector<vector<int> >& rooms) {
 4         queue<int>q;
 5         q.push(0);
 6         int vis[1005] = {0};
 7         while(!q.empty())
 8         {
 9             int now = q.front();
10             q.pop();
11             for(int i = 0; i < rooms[now].size(); i++)
12             {
13                 int next = rooms[now][i];
14                 if(!vis[next])
15                 {
16                     vis[next] = 1;
17                     q.push(next);
18                 }
19             }
20         }
21         for(int i = 1; i < rooms.size(); i++)
22         {
23             if(vis[i] == 0)return false;
24         }
25         return true;
26     }
27 };

C:842. 将数组拆分成斐波那契序列

给定一个数字字符串 S,比如 S = "123456579",我们可以将它分成斐波那契式的序列 [123, 456, 579]

形式上,斐波那契式序列是一个非负整数列表 F,且满足:

  • 0 <= F[i] <= 2^31 - 1,(也就是说,每个整数都符合 32 位有符号整数类型);
  • F.length >= 3
  • 对于所有的0 <= i < F.length - 2,都有 F[i] + F[i+1] = F[i+2] 成立。

另外,请注意,将字符串拆分成小块时,每个块的数字一定不要以零开头,除非这个块是数字 0 本身。

返回从 S 拆分出来的所有斐波那契式的序列块,如果不能拆分则返回 []

枚举第一项和第二项的位数即可,保证没有前导0,还需要保证以后的每一项在2的31次方以内

 1 class Solution {
 2 public:
 3     vector<int> splitIntoFibonacci(string S) {
 4         for(int i = 1; i <= min(10, (int)S.size()); i++)
 5         {
 6             for(int j = 1; j <= min(10, (int)S.size()); j++)
 7             {
 8                 if(i + j - 1 >= S.size())break;
 9                 string s1, s2;
10                 for(int k = 0; k < i; k++)s1 += S[k];
11                 for(int k = i; k < i + j; k++)s2 += S[k];
12                 if(s1[0] == ‘0‘ && i != 1 || s2[0] == ‘0‘ && j != 1)continue;
13                 vector<int>ans;
14                 string tmp = s1 + s2;
15                 stringstream ss(s1), ss1(s2);
16                 long long a, b, c;
17                 ss >> a;ss1 >> b;
18                 ans.push_back(a);
19                 ans.push_back(b);
20                 while(1)
21                 {
22                     c = a + b;
23                     if(c >= (1LL<<31))
24                     {
25                         break;
26                     }
27                     ans.push_back(c);
28                     stringstream ss;
29                     ss << c;
30                     string s3;
31                     ss >> s3;
32                     tmp += s3;
33                     if(tmp.size() > S.size())break;
34                     if(tmp == S)return ans;
35                     a = b;
36                     b = c;
37                 }
38             }
39         }
40         vector<int>ans;
41         return ans;
42     }
43 };

D交互题,过两天就写

原文地址:https://www.cnblogs.com/fzl194/p/9097783.html

时间: 2024-08-30 10:36:45

Leetcode Weekly Contest 86的相关文章

840. Magic Squares In Grid ——weekly contest 86

题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time:5ms 本人的解法过于粗糙,看出了中间必须是5,然后比较每行每列每对角线的值是否相等. class Solution { public: int numMagicSquaresInside(vector<vector<int>>& grid) { int n = grid.si

841. Keys and Rooms —— weekly contest 86

题目链接:https://leetcode.com/problems/keys-and-rooms/description/ 简单DFS time:9ms 1 class Solution { 2 public: 3 void DFS(int root,vector<int>& visited,vector<vector<int>>& rooms){ 4 visited[root] = 1; 5 for(auto x : rooms[root]){ 6

842. Split Array into Fibonacci Sequence —— weekly contest 86

题目链接:https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/ 占坑. string 的数值转换函数(c++11)介绍 :https://blog.csdn.net/calmreason/article/details/41204211 答案可参考:https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss

843. Guess the Word —— weekly contest 86

题目链接:https://leetcode.com/problems/guess-the-word/description/ 占坑 据说要用启发式算法,可参考下述答案进行学习:https://leetcode.com/problems/guess-the-word/discuss/133862/Random-Guess-and-Minimax-Guess-with-Comparison 原文地址:https://www.cnblogs.com/jinjin-2018/p/9098158.html

Leetcode Weekly Contest 152

退役老人现在连leetcode都不会做了 = = 今天早上做了leetcode第三题题目看错了,加上比赛中间还在调投稿的实验,一心二用直接gg 总结下教训就是 本渣现在做题连题目都看不清就开始做.开始写题之前应当把样例过一遍,然后自己再造1-2个例子,然后再开始做 A题:统计素数的个数(素数筛或者sqrt(n)判断都可以),然后分别计算count! class Solution { public: int numPrimeArrangements(int n) { vector<int> ha

[Leetcode Weekly Contest]174

链接:LeetCode [Leetcode]5328. 方阵中战斗力最弱的 K 行 给你一个大小为?m?* n?的方阵?mat,方阵由若干军人和平民组成,分别用 0 和 1 表示. 请你返回方阵中战斗力最弱的?k?行的索引,按从最弱到最强排序. 如果第?i?行的军人数量少于第?j?行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱. 军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前. 输入:\(mat = [[1,1,0,0,0], [1,

108th LeetCode Weekly Contest Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

108th LeetCode Weekly Contest Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

113th LeetCode Weekly Contest Flip Equivalent Binary Trees

For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip opera