面试算法题
来源:互联网 发布:电熨斗推荐 知乎 编辑:程序博客网 时间:2024/06/05 07:50
1.字符串压缩
不考虑字符数超过9。
#include <string>#include <iostream>std::string solution(std::string s) { std::string ans; int i = 0, j = 0; while (i < int(s.length())) { while (s[i] == s[j]) ++i; ans += s[j]; ans += std::to_string(i - j); j = i; } return ans;}int main() { // 输入 std::string s; std::cin >> s; // 字符串压缩 std::string ans = solution(s); // 输出 std::cout << ans;}
考虑字符数超过9。
#include <string>#include <iostream>std::string solution(std::string s) { std::string ans; int i = 0, j = 0; while (i < int(s.length())) { while (s[i] == s[j]) ++i; ans += s[j]; int count = i-j; while (count / 10) { ans += std::to_string(9); ans += s[j]; count -= 9; } ans += std::to_string(count); j = i; } return ans;}int main() { // 输入 std::string s; std::cin >> s; // 字符串压缩 std::string ans = solution(s); // 输出 std::cout << ans;}
const scanf = require('scanf');let input = scanf('%s');function solution(s) { let ans = []; let i = 0, j = 0; while (i < s.length) { while (s[i] === s[j]) ++i; ans += s[j]; let count = i - j; while (parseInt(count / 10, 10)) { ans += 9; ans += s[j]; count -= 9; } ans += count; j = i; } return ans;}let ans = solution(input);console.log(ans);
std::string extract_string(std::string s) { std::string ans; for (int i = 0; i < int(s.length()); i+=2) { int count = int(s[i+1])-int('0'); for (int j = 0; j < count; ++j) ans += s[i]; } return ans;}int main() { // 输入 std::string s; std::cin >> s; // 字符串解压 std::string ans = extract_string(s); // 输出 std::cout << ans;}
0 0
- 面试算法题1
- 面试算法题
- 面试算法题2
- 面试算法题3
- 面试算法题4
- 面试算法题5
- 面试算法题6
- 转:面试算法题
- 面试算法题!
- 面试算法题1
- 面试算法题
- 面试算法题
- 微软面试算法题
- java面试算法题
- 数组面试算法题
- 面试算法题整理
- 某个面试算法题
- 面试算法题
- Http和Https的区别
- GC的三大基础算法
- Tensorflow入门学习网站以及资料<一>
- LeetCode 561. Array Partition I
- 注入攻击命令大全
- 面试算法题
- Servlet的转发和重定向的区别
- TCP/IP四层模型
- Effective C++,rule 1,View C++ as a federation of languages
- 消息中间件和JMS介绍(一)
- MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建
- QT关于qml的Component事件
- 有用的网站
- Centos7下的安装Docker1.8