leetcode 1573
简介
我们自己观察题目发现了什么这是一道数学题,哈哈哈。
个人的思路是分成两类去判断,
第一种:
全是0
使用
\]
第二种:
有1
然后观察10101
发现10101
其中0的个数两个之间
1个和1个
\]
Tips
注意防止溢出
AC code
class Solution1573 {
public:
int numWays(string s) {
// step 1. count the number of the 1
long int numberOne = 0;
for(long int i=0; i<s.length(); i++){
if(s[i] == '1'){
numberOne++;
}
}
// step 2. check the numberOne can be subdivided 3?
if(numberOne % 3 != 0){
return 0;
}
if(numberOne == 0){
double n = s.length() - 1;
return (long int)((n / 2) * (n-1)) % (1000000000 + 7) ;
}
long int onePiece = numberOne / 3;
// step 3. get the numberZero between first piece and third piece
long int numberZero1 = 0; long int numberZero2 = 0;
long int index = 0;
long int status = -1;
for(long int i=0; i<s.length(); i++){
if(index == onePiece){
status = 0;
}
if(status == -1){
if(s[i] == '1'){
index++;
}
}
if(status == 0){
if(s[i] == '1'){
status = 1;
break;
}else if(s[i] == '0'){
numberZero1++;
}
}
}
string tmp;
tmp.resize(s.length());
long int j=0;
for(long int i=s.length() -1; i>=0; i--,j++){
tmp[j] = s[i];
}
index = 0;
status = -1;
for(long int i=0; i<tmp.length(); i++){
if(index == onePiece){
status = 0;
}
if(status == -1){
if(tmp[i] == '1'){
index++;
}
}
if(status == 0){
if(tmp[i] == '1'){
status = 1;
break;
}else if(tmp[i] == '0'){
numberZero2++;
}
}
}
// step 4. get the number
return ((numberZero1+1) % (1000000000 + 7) * (numberZero2+1) % (1000000000 + 7))% (1000000000 + 7);
}
long long factorial(long long n){
long long num = 1;
for(long long i=1; i <= n; i++){
num = num * i % (1000000000 + 7);
}
return num;
}
};
link
https://github.com/lishaohsuai/leetCode
https://github.com/haoel/leetcode
leetcode 1573的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- SpringAI版本更新:向量数据库不可用的解决方案!
Spring AI 前两天(4.10 日)更新了 1.0.0-M7 版本后,原来的 SimpleVectorStore 内存级别的向量数据库就不能用了,Spring AI 将其全部源码删除了. 此时我 ...
- symfony4.4加密密码时报错Libsodium is not available. You should either install the sodium extension, upgrade
报错: "Libsodium is not available. You should either install the sodium extension, upgrade to PHP ...
- win10、win11环境下查看IIS里各项目资源占用情况
参照链接: [如何设置IIS程序池的回收时间,才能最大程度的减少对用户的影响?] - 走看看 (zoukankan.com) 概念: 简单理解IIS应用程序池 应用程序池可以看成是计算机分配给Web应 ...
- 【代码】Python3|Requests 库怎么继承 Selenium 的 Headers (2024,Chrome)
本文使用的版本: Chrome 124 Python 12 Selenium 4.19.0 版本过旧可能会出现问题,但只要别差异太大,就可以看本文,因为本文对新老版本都有讲解. 文章目录 1 难点解析 ...
- pod数据持久化-pv与pvc资源及动态存储StorageClass
一.pc与pvc的概念 在传统的存储卷挂载,比如说nfs,它虽然能够实现我们大多数的生产场景,但是,耦合性比较高: 举例:假设,我们要将集群从"阿里云"迁移到我们私有云服务器上,并 ...
- BP算法完整推导 2.0 (下)
上篇主要阐述 BP算法的过程, 以及 推导的 4 大公式的结论, 现在呢要来逐步推导出这写公式的原理. 当理解到这一步, 就算真正理解 BP算法了. 也是先做一个简单的回顾一下, 不是很细, 重点在推 ...
- 我的Vue之旅(1)
2020-10-17 今天主要学习了Vue中以下几个指令的使用 v-bind v-if v-on v-for v-model 其中v-bind与v-model都是属于数据绑定,v-bind通常来说是绑 ...
- 使用 Leangoo 看板工具高效管理直播筹备活动
在组织一场成功的直播活动中,筹备工作通常涉及多个环节,包括选题策划.嘉宾邀请.物料准备.技术支持等.为了更高效地管理这些活动,我们选择使用 Leangoo 看板工具 来规划和跟踪直播的各项筹备任务.以 ...
- 洛谷 P5512 [NOIP1997 提高组] 棋盘问题 加强版
洛谷 P5512 [NOIP1997 提高组] 棋盘问题 加强版 Problem&Background 可以参考这个 Solution 首先先完成这道题的普通版本P1549 A了那一道题之后, ...
- 你应该懂的AI大模型(三)之 RAG
从本篇开始笔者会尽量多使用一些英文缩写和单词,不是笔者为了装X,是为了大家在后面遇到的时候不至于被别人装到. 一.什么是RAG 1.1 大模型的局限性 大模型的知识不是实时的,比如现在<藏海传& ...