Leetcode Weekly Contest 86
A:840. 矩阵中的幻方
3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等。
给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 “幻方” 子矩阵?(每个子矩阵都是连续的)。
直接模拟即可,本来是签到题,由于粗心,浪费了时间。
 class Solution {
 public:
     int numMagicSquaresInside(vector<vector<int>>& grid) {
         vector<vector<int> >a = grid;
         int ans = ;
         for(int i = ; i +  < a.size(); i++)
         {
             for(int j = ; j +  < a[].size(); j++)
             {
                 set<int>s;
                 bool flag = ;
                 for(int ii = i; ii <= i + ; ii++)
                 {
                     for(int jj = j; jj <= j + ; jj++)
                     {
                         if(a[ii][jj] >=  || a[ii][jj] <= )
                         {
                             flag = ;
                         }
                         s.insert(a[ii][jj]);
                     }
                 }
                 if(flag && s.size() == )
                 {
                     int a1 = a[i][j] + a[i + ][j + ] + a[i + ][j + ];
                     int a2 = a[i][j + ] + a[i + ][j + ] + a[i + ][j];
                     //cout<<a1<<" "<<a2<<endl;
                     if(a1 == a2 && a1 == )
                     {
                         for(int ii = i; ii <= i + ; ii++)
                         {
                             int a3 = a[ii][j] + a[ii][j + ] + a[ii][j + ];
                             if(a3 != )flag = ;
                         }
                         for(int ii = j; ii <= j + ; ii++)
                         {
                             int a3 = a[i][ii] + a[i + ][ii] + a[i + ][ii];
                             if(a3 != )flag = ;
                         }
                         if(flag)ans++;
                     }
                 }
             }
         }
         return ans;
     }
 };
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即可,到达每一点,比赛的时候用并查集也过了,但是后来想这是有向边,并查集合并的是无向边,应该是数据水的原因
 class Solution {
 public:
     bool canVisitAllRooms(vector<vector<int> >& rooms) {
         queue<int>q;
         q.push();
         int vis[] = {};
         while(!q.empty())
         {
             int now = q.front();
             q.pop();
             for(int i = ; i < rooms[now].size(); i++)
             {
                 int next = rooms[now][i];
                 if(!vis[next])
                 {
                     vis[next] = ;
                     q.push(next);
                 }
             }
         }
         for(int i = ; i < rooms.size(); i++)
         {
             if(vis[i] == )return false;
         }
         return true;
     }
 };
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次方以内
 class Solution {
 public:
     vector<int> splitIntoFibonacci(string S) {
         for(int i = ; i <= min(, (int)S.size()); i++)
         {
             for(int j = ; j <= min(, (int)S.size()); j++)
             {
                 if(i + j -  >= S.size())break;
                 string s1, s2;
                 for(int k = ; k < i; k++)s1 += S[k];
                 for(int k = i; k < i + j; k++)s2 += S[k];
                 if(s1[] == '' && i !=  || s2[] == '' && j != )continue;
                 vector<int>ans;
                 string tmp = s1 + s2;
                 stringstream ss(s1), ss1(s2);
                 long long a, b, c;
                 ss >> a;ss1 >> b;
                 ans.push_back(a);
                 ans.push_back(b);
                 while()
                 {
                     c = a + b;
                     if(c >= (1LL<<))
                     {
                         break;
                     }
                     ans.push_back(c);
                     stringstream ss;
                     ss << c;
                     string s3;
                     ss >> s3;
                     tmp += s3;
                     if(tmp.size() > S.size())break;
                     if(tmp == S)return ans;
                     a = b;
                     b = c;
                 }
             }
         }
         vector<int>ans;
         return ans;
     }
 };
D:843. 猜猜这个单词
这个问题是 LeetCode 平台新增的交互式问题 。
我们给出了一个由一些独特的单词组成的单词列表,每个单词都是 6 个字母长,并且这个列表中的一个单词将被选作秘密。
你可以调用 master.guess(word) 来猜单词。你所猜的单词应当是存在于原列表并且由 6 个小写字母组成的类型字符串。
此函数将会返回一个整型数字,表示你的猜测与秘密单词的准确匹配(值和位置同时匹配)的数目。此外,如果你的猜测不在给定的单词列表中,它将返回 -1。
对于每个测试用例,你有 10 次机会来猜出这个单词。当所有调用都结束时,如果您对 master.guess 的调用不超过 10 次,并且至少有一次猜到秘密,那么您将通过该测试用例。
除了下面示例给出的测试用例外,还会有 5 个额外的测试用例,每个单词列表中将会有 100 个单词。这些测试用例中的每个单词的字母都是从 'a' 到 'z' 中随机选取的,并且保证给定单词列表中的每个单词都是唯一的。
一开始以为是难题,后来发现就是水题一个,直接模拟即可,每次随机询问一个位置即可,得到答案为t,把字符串数组中与该位置有t个相同的字符串作为下一组字符串数组继续查询。
/**
* // This is the Master's API interface.
* // You should not implement it, or speculate about its implementation
* class Master {
* public:
* int guess(string word);
* };
*/
class Solution {
public:
int judge(string a, string b)
{
int tot = ;
for(int i = ; i < a.size(); i++)
{
if(a[i] == b[i])tot++;
}
return tot;
}
void findSecretWord(vector<string>& wordlist, Master& master) {
vector<string>now = wordlist, next;
while()
{
next.clear();
int s = rand()%now.size();
int t = master.guess(now[s]);
if(t == )break;
for(int i = ; i < now.size(); i++)
{
if(judge(now[i], now[s]) == t)
next.push_back(now[i]);
}
now = next;
}
}
};
Leetcode Weekly Contest 86的更多相关文章
- LeetCode Weekly Contest 8
		
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
 - leetcode weekly contest 43
		
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
 - LeetCode Weekly Contest 23
		
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
 - LeetCode Weekly Contest
		
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
 - 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
		
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
 - 【LeetCode Weekly Contest 26 Q3】Friend Circles
		
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
 - 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
		
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
 - 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
		
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
 - LeetCode Weekly Contest 47
		
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...
 
随机推荐
- java性能监控工具jstat
			
jstat Monitors Java Virtual Machine (JVM) statistics. This command is experimental and unsupported. ...
 - execl中设置的格式无法实现
			
在一次项目中,需要导出execl表,并且要给表中的表格设置格式,因为每列的格式都不一样,需要单独设置设置这些格式,在后期使用中因为导入的数据过多,是的后面的单元格中设置的格式无法实现. 每次打开exe ...
 - Java中响应结果工具类,可自定义响应码,内容,响应消息
			
创建响应状态码和说明枚举类 /** * 响应状态码和说明 */public enum CodeEnum { SUCCESS(0, "成功!"), FAIL(1, &qu ...
 - jackson @ResponseBody 处理日期类型的字段
			
前言:以前只知道一种方式(@JsonFormat)来处理日期格式问题,今天才发现还有两种方式,并且可以全局设置格式,这里记录一下. 首先,pom.xml 中需要先引入如下 jackson 的依赖: & ...
 - 各种IDE的使用
			
sharpdevelop http://blog.sina.com.cn/s/blog_d1001bff0101di7p.html
 - Cannot perform conversion to XML from legacy HTML:
			
错误信息:Cannot perform conversion to XML from legacy HTML: The nekoHTML library is not in classpath. ne ...
 - EF_CRUD
 - 润乾V5手机报表说明文档
			
1.手机报表实例页面简要说明 index.jsp 是报表资源列表页面: mbReport.jsp 是报表展现页面: mbParam.jsp是参数报表展现页面: echarts.jsp是带有echart ...
 - springboot学习入门之五---开发Web应用之JSP篇
			
转载:http://tengj.top/2017/03/13/springboot5/ 1整体结构 整体的框架结构,跟前面介绍Thymeleaf的时候差不多,只是多了webapp这个用来存放jsp的目 ...
 - 使用ServiceBroker自动激活模拟"秒杀"场景
			
1.简介 SQL Server Service Broker 是SQL server 里面比较独特的一个功能.它可帮助开发人员构建异步的松散耦合应用程序 ServiceBroker入门文章:http: ...