Coding Problem : 01 string(01字符串)
来源:互联网 发布:node schedule 不好使 编辑:程序博客网 时间:2024/05/16 10:42
Description:
Given a string consists of only characters 0 and 1. You can only delete characters from it.
You want to change it into a string contains only alternating 0 and 1. Namely no Two consecutive characters are the same.
How many characters do you need to delete at least?
Input
A string consists of only characters 0 and 1 whose length is no longer than 100000.
Output
The least number of characters to delete.
Sample Input
1011
Sample Output
1
这是一个01字符串问题
只要遍历整个字符串,计算与前一个字符不同的字符个数即可。
class Solution {public: int leastDeletion(string &s) { int length = s.size(); int count = 0; char flag = s[0]; for(int i = 1; i < length; i++) { if(s[i] == flag) { count++; }else { flag = s[i]; } } return count; }};
题目来源:https://www.judgecode.com/problems/1001
阅读全文
0 0
- Coding Problem : 01 string(01字符串)
- hdu3374 String Problem (字符串最小表示)
- Hihocoder 挑战赛17 String Problem I (字符串HASH)
- ZOJ Problem Set - 3490 String Successor(字符串模拟)
- Coding Interview Problem IV
- coding---常用字符串接口
- HDU3374(String Problem)字符串-最小表示法+KMP
- hdu 3374 String Problem(KMP+字符串最小最大表示)
- hdu 3374 String Problem(字符串最小最大表示法+kmp)
- String Problem
- Key-Value Coding 01 - What is key-Value Coding
- 【Cracking the coding interview】Q1.2(反转字符串)
- 【Cracking the coding interview】Q1.5(替换字符串)
- 【Cracking the coding interview】Q1.8(旋转字符串)
- C++字符串(string)
- 字符串(String)杂谈
- Java字符串(String)
- java 字符串(string)
- Jedis Connection Pool
- 注册脸谱网站时遇到的问题
- 【SpringMVC学习01】宏观上把握SpringMVC框架
- Hadoop datanode 无法启动(All specified directories are failed to load.)
- MySql启动报错Failed to open log (file './mysql-bin.000232', errno 2)
- Coding Problem : 01 string(01字符串)
- WINDOWS局域网文件共享策略及设置方法
- 解决SQLserver版本之间备份还原的兼容问题
- mapreduce流量汇总程序
- Android--通过网页打开APP并传值详解
- ionic2 serve报错
- ReactJS前端项目 问题与解决记录(刚接触前端,根据项目中遇到的坑持续更新)
- 图片全屏预览实现
- form表单提交不跳转