简介

我们自己观察题目发现了什么这是一道数学题,哈哈哈。

个人的思路是分成两类去判断,

第一种:

全是0

使用

\[ (n-1) * (n - 2) / 2
\]

第二种:

有1

然后观察10101

发现10101

其中0的个数两个之间

1个和1个

\[ (前一堆1和中间一堆1之间的0的个数+1) * (后一堆1和中间一堆1之间的0的个数+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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. kettle使用MD5加密增量获取接口数据

    kettle使用MD5加密增量获取接口数据 场景介绍: 使用JavaScript组件进行MD5加密得到Http header,调用API接口增量获取接口数据,使用json input组件解析数据入库 ...

  2. C#/.NET/.NET Core技术前沿周刊 | 第 35 期(2025年4.14-4.20)

    前言 C#/.NET/.NET Core技术前沿周刊,你的每周技术指南针!记录.追踪C#/.NET/.NET Core领域.生态的每周最新.最实用.最有价值的技术文章.社区动态.优质项目和学习资源等. ...

  3. php高级工程师面试题

    转载于:王召波 博客 PHP对象的克隆与引用有什么区别? 摘要: 是这样的,这个问题确切说应该是这样的:" PHP对象的赋值和克隆有什么区别 ",注意不是复制,就是复制,打开窗子说 ...

  4. MVVM绑定 填坑,必须在与 DependencyObject 相同的线程上创建 DependencySource

    场景:线程里面构建MVVM实体类,实体类包含 Brush 属性时,构建 SolidColorBrush 需要UI线程,否则会报 "必须在与 DependencyObject 相同的线程上创建 ...

  5. EFCore——树形结构篇

    1.整体数据量不大的场景 参照:EntityFramework Linq 查询数据获得树形结构-YES开发框架网 (yesdotnet.com) 核心方法GetChildData,特点将所有的数据查到 ...

  6. Sentinel——控制台使用

    简介 官网:https://sentinelguard.io/ 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentinel 是面向分布式.多语言异构化服务架构的流量治理组件,主要以流量为 ...

  7. 内网私仓全流程搭建记录(一)-Nexus3环境搭建

    1.部署 1)在https://help.sonatype.com/repomanager3/product-information/download中下载对应环境及版本,此处要求3以上版本,本次以& ...

  8. (NLP)关键词提取之——TF-IDF解析

    关键词提取--TF-IDF 1 TF-IDF定义 概要 tf-idf(英语:term frequency–inverse document frequency)是一种用于信息检索与文本挖掘的常用加权技 ...

  9. 在CentOS 7虚拟机上正确安装Redis

    在CentOS 7虚拟机上正确安装Redis,可以按照以下步骤进行操作: 更新系统软件包:sudo yum update 安装Redis依赖库:sudo yum install epel-releas ...

  10. WPF后台自动添加控件Demo

    xaml <Window x:Class="EBPlugIn2.EBPlugIn2_YJW_13" xmlns="http://schemas.microsoft. ...