当前位置:网站首页>[3 questions per day (2)] minimum operand for generating alternate binary strings
[3 questions per day (2)] minimum operand for generating alternate binary strings
2022-06-29 11:13:00 【Programmed ape without hair loss 2】
subject :
Give you a character only ‘0’ and ‘1’ Composed string s . In one step , You can put any ‘0’ become ‘1’ , Or will ‘1’ become ‘0’ .
Alternate strings Defined as : If there is no case that two adjacent characters are equal in the string , So the string is an alternate string . for example , character string “010” It's an alternate string , And strings “0100” No .
Return to make s become Alternate strings The required least Operands .
Example 1:
Input :s = “0100”
Output :1
explain : If you change the last character to ‘1’ ,s It becomes “0101” , That is, it conforms to the alternate string definition .
Example 2:
Input :s = “10”
Output :0
explain :s It's already an alternate string .
Example 3:
Input :s = “1111”
Output :2
explain : need 2 Step operation to get “0101” or “1010” .
Tips :
1 <= s.length <= 10^4
s[i] yes ‘0’ or ‘1’
Ideas :
Actually , Alternating strings are two cases , The first is ‘1010…’, The second is 0101…, namely
1. Even digits are 0, Odd bits are 1
In this case , The value of any bit is the same as the index parity , namely s[i]%2==i%2, If not satisfied , That is, this bit needs to be changed , Count cnt1++
2. Even digits are 1, Odd bits are 0
In this case , The value and index parity of any bit are different , namely s[i]%2!=i%2, If not satisfied , That is, this bit needs to be changed , Count cnt2++
java Code :
class Solution {
public int minOperations(String s) {
int step1=0,step2=0;
for (int i = 0; i <s.length() ; i++) {
if((s.charAt(i)-'0')%2!=i%2){
step1++;
}else {
step2++;
}
}
return Math.min(step1,step2);
}
}
边栏推荐
- ModbusTCP协议网络学习型单路红外模块(中壳版)
- (JS)手写深比较
- 由ASP.NET Core根据路径下载文件异常引发的探究
- EasyDSS部署在C盘,录像回看无法正常播放该如何解决?
- [untitled] I am plotting a big event
- By asp Net core downloading files according to the path
- Ningde era Kirin battery has greater ambition
- Shell 中你不得不熟知的变量运用
- 中国计算语言学大会、全国知识图谱与语义计算大会赛题火热进行中
- 9 easy to use JSON processing tools, greatly improving efficiency!
猜你喜欢

多线程实现客户端与服务端通信(初级版本)

Graduation season · advanced technology Er - workers in the workplace

【各种**问题系列】OLTP和OLAP是啥?

Exemples de programmation stm32f1 et stm32cubeide - entraînement du capteur de portée ultrasonique

Specific method and example program of Siemens s7-200smart control stepping motor

The last 48 hours! The cloud XR theme competition invites you to bloom together. See you at the competition!

第12周实验---基于FPGA的VGA协议实现

多线程高并发服务器:3个问题

Bs-gx-017 online examination management system based on SSM

Bs-gx-018 student examination system based on SSM
随机推荐
学习通否认 QQ 号被盗与其有关:已报案;iPhone 14 量产工作就绪:四款齐发;简洁优雅的软件早已是明日黄花|极客头条...
He was in '98. I can't play with him
What happened during the MySQL installation?
Dropout layer
The necessary operation for those big guys to fly 666 inadvertently at the bash command line terminal
Exemples de programmation stm32f1 et stm32cubeide - entraînement du capteur de portée ultrasonique
Reids设计与实现
X-Frame-Options(点击劫持) 网页劫持漏洞
彻头彻尾理解JVM系列之七:对象在分代模型中的流转过程是怎样的?
第12周实验---基于FPGA的VGA协议实现
基于支持向量机的手写数字识别详解(MATLAB GUI代码,提供手写板)
如何通过WinDbg获取方法参数值
NUC980开源项目16-从SPI FLASH(W25Q128)启动
内存分配——静态存储区 栈 堆 与static变量
5.移植uboot-设置默认环境变量,裁剪,并分区
(JS)职责链模式
(JS)数组的 pop、push、unshift、shift分别是什么?
企业竞争分析的几种方法:SWOT、波特五力、PEST「建议收藏」
【无标题】我在密谋一件大事
Xiaomi mobile phone - Unlock BL + open root permission