当前位置: 首页 > news >正文

Java | Leetcode Java题解之第443题压缩字符串

题目:

题解:

class Solution {public int compress(char[] chars) {int n = chars.length;int write = 0, left = 0;for (int read = 0; read < n; read++) {if (read == n - 1 || chars[read] != chars[read + 1]) {chars[write++] = chars[read];int num = read - left + 1;if (num > 1) {int anchor = write;while (num > 0) {chars[write++] = (char) (num % 10 + '0');num /= 10;}reverse(chars, anchor, write - 1);}left = read + 1;}}return write;}public void reverse(char[] chars, int left, int right) {while (left < right) {char temp = chars[left];chars[left] = chars[right];chars[right] = temp;left++;right--;}}
}

http://www.mrgr.cn/news/38718.html

相关文章:

  • 基于PI控制器的车辆行驶控制系统simulink建模与仿真
  • C++入门基础知识88(实例)——实例13【求一个数的阶乘】
  • 一天认识一个硬件之音响与音箱
  • Golang | Leetcode Golang题解之第445题两数相加II
  • tar命令:压缩、解压的好工具
  • undeclared identifier ‘UNITY_PREV_MATRIX_M‘ - Unity Shader自己写URP,引用内部 hlsl
  • linux环境下使用sqlplus访问远程oracle数据库
  • DAY17||654.最大二叉树 |617.合并二叉树 |700.二叉搜索树中的搜索 |
  • 0基础学习CSS(六)字体
  • The 2024 ICPC Asia East Continent Online Contest (II) K. Match(图计数dp 二分图匹配方案)
  • Cpp::STL—string类的使用与理解(下)(9)
  • 矩阵SVD分解中u,s,v的实际意义
  • Golang | Leetcode Golang题解之第443题压缩字符串
  • C++ | Leetcode C++题解之第443题压缩字符串
  • Android平台如何获取CPU占用率和电池电量信息
  • C语言课程设计题目六:学生信息管理系统设计
  • 【分布式微服务云原生】10分钟打造坚不可摧的系统:深入探索系统的鲁棒性
  • 树莓派外挂Camera(基操)(TODO)
  • 全功能运营级开源跨境电商平台源码 —— 多语言全球化商城解决方案
  • systemd-analyze命令:Linux系统启动分析