当前位置:网站首页>[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);
}
}
边栏推荐
- 那些大佬经常在bash 命令行终端不经意间666飞起的必备操作
- 最后的 48 小时!云 XR 专题赛邀你一起绽放精彩,我们赛场见!
- (JS)观察者模式
- Does anyone encounter this problem when flinkcdc synchronizes MySQL?
- 添加通知公告,给在线用户发送通知
- X-Frame-Options(点击劫持) 网页劫持漏洞
- (JS)模仿一个instanceof方法
- Please tell me about the Flink SQL batch task, two or more tables join (inner join or outer join
- Xiaomi mobile phone - Unlock BL + open root permission
- Ikvm Net project progress
猜你喜欢
![[FreeRTOS] 08 mutex semaphores and priority inversion](/img/16/9715d5599db6ec107c8001fbd70ae2.png)
[FreeRTOS] 08 mutex semaphores and priority inversion

Stm32f1 and stm32subeide programming example - ultrasonic distance sensor drive

BS-GX-018 基于SSM实现在校学生考试系统

misc3~7

STM32F1与STM32CubeIDE编程实例-超声波测距传感器驱动

期未课程设计:基于SSM的产品销售管理系统

Online text filter less than specified length tool

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

Google Earth engine (GEE) - Gedi L2a vector canopy top height (version 2) global ecosystem data set

math_ Mathematical expression & deformation of equation equation & accumulation of combined operation skills / means
随机推荐
Spark - Task 与 Partition 一一对应与参数详解
Online sql to htmltable tool
(JS)手写bind函数
STM32F1与STM32CubeIDE编程实例-超声波测距传感器驱动
(JS)手写深拷贝
(JS)手写深比较
arcgis创建postgre企业级数据库
The use of variables in shell that you have to be familiar with
Memory allocation - static storage stack heap and static variable
非凸联合创始人李佐凡:将量化作为自己的终身事业
(JS)数组的 pop、push、unshift、shift分别是什么?
(JS)职责链模式
ModbusTCP协议WIFI无线学习型单路红外模块(圆壳版)
内存分配——静态存储区 栈 堆 与static变量
nuc980 已成功启动
MATLAB basic Max to find the maximum value of one-dimensional or two-dimensional array +sleep (pause)
dropout层
任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
NUC980开源项目16-从SPI FLASH(W25Q128)启动
Modbustcp protocol network learning single channel infrared module (medium shell version)