网易笔试:交错01串
来源:互联网 发布:无root一键免流软件 编辑:程序博客网 时间:2024/05/22 07:54
网易笔试:交错01串
题目描述
如果一个01串任意两个相邻位置的字符都是不一样的,我们就叫这个01串为交错01串。例如: “1”,”10101”,”0101010”都是交错01串。
小易现在有一个01串s,小易想找出一个最长的连续子串,并且这个子串是一个交错01串。小易需要你帮帮忙求出最长的这样的子串的长度是多少。
输入描述:
输入包括字符串s,s的长度length(1 ≤ length ≤ 50),字符串中只包含’0’和’1’输出描述:
输出一个整数,表示最长的满足要求的子串长度。输入例子1:
111101111输出例子1:
3
思路:
扫描字符串,统计01交错的个数
java:
import java.util.Scanner;public class Main { public static int solution(String line) { char[] c = line.toCharArray(); int max = 0; int count = 0; for(int i = 0; i < c.length - 1; i++) { if((c[i] == '0' && c[i+1] == '1') || (c[i] == '1' && c[i+1] == '0') ) { count++; } else { max = Math.max(max, count + 1); count = 0; } } max = Math.max(max, count + 1); return max; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { String line = sc.nextLine(); int result = solution(line); System.out.println(result); } sc.close(); }}
c++:
#include <iostream>#include <string.h>using namespace std;int solution(string str) { int len = str.length(); int count = 0, result = 0; for(int i = 0; i < len - 1; i++) { if((str[i] == '0' && str[i+1] == '1') || (str[i] == '1' && str[i+1] == '0') ){ count++; }else { result = max(result, count + 1); count = 0; } } result = max(result, count + 1); return result; }int main() { string str; cin >> str; int result = solution(str); cout << result << endl; return 0;}
注:学渣心里苦,不要学楼主,平时不努力,考试二百五,哭~
阅读全文
0 0
- 网易笔试:交错01串
- 网易2018校招内推笔试-交错01串
- 2018网易秋招内推笔试题——交错01串
- 2018网易内推笔试-交错01串(python)
- 2018校招网易笔试——最长01交错子串
- 网易2018内推笔试题解析(交错01串)
- 网易2018内推_算法工程师_笔试题_交错01串_C/C++
- 交错01串-网易2018校招内推
- 十、网易2017<交错01串>
- 网易编程题 交错01串
- 网易2017内推 [编程题] 交错01串@Java
- 2018年网易校招内推编程题之交错01串
- 网易2018校招内推编程题集合:交错01串 [python]
- 2018网易校招编程题(3)交错01串
- 网易2018校园招聘编程题真题-[编程题] 交错01串
- 交错01串
- 交错01串
- 交错01串
- 绚丽多彩的对话框NiftyDialogEffects(多种颜色和动画进入效果)
- Form表单的6种提交方式
- bzoj3999 [TJOI2015]旅游
- 【webstorm】webstorm官网中文破解版
- poj2114 Boatherds
- 网易笔试:交错01串
- CodeForces
- ARKit从入门到精通(1)-ARKit初体验
- #以应用为中心的无服务器PaaS将成主流
- SGU 319 Kalevich Strikes Back(线段树+扫描线)
- 内存溢出和内存泄漏的区别
- (dfs)迷宫探索
- Random 不重复
- Linux 初始 RAM磁盘(initrd)概述