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

算法6:模拟运算

文章目录

  • z字形变幻
  • 外观数列
  • 数青蛙

题目均来自于力扣

z字形变幻

在这里插入图片描述

class Solution {
public:string convert(string s, int numRows) {int n = s.size();if(n <= numRows || numRows == 1) return s;int d = 2 * numRows - 2;string res;for(int j = 0; j < n; j += d){res += s[j];    }for(int i = 1; i < numRows - 1; ++i){   int left = d - 2 * i;int righ = d - left;bool flag = true;for(int j = i; j < n;){res += s[j];if(flag)    j += left;else j+= righ;flag = !flag;}}for(int j = numRows - 1; j < n; j += d){res += s[j];    }return res;}
};

外观数列

class Solution {
public:string countAndSay(int n) {string res = "1";for(int i = 1; i < n; ++i){string tmp;int left = 0, right = 0;for(; right < res.size(); ++right){if(res[right] != res[left]){tmp += to_string(right - left) + res[left];left = right;}}tmp += to_string(right - left) + res[left];res = tmp;}return res;}
};

数青蛙

class Solution {
public:int minNumberOfFrogs(string cF) {int hash[130];for (int i = 0; i < cF.size(); i++){if(cF[i] != 'c'){if ((cF[i] == 'r' && hash['c'] > 0) ||(cF[i] == 'o' && hash['r'] > 0) ||(cF[i] == 'a' && hash['o'] > 0) ||(cF[i] == 'k' && hash['a'] > 0)){   // crookif(cF[i] == 'r') hash['c']--;if(cF[i] == 'o') hash['r']--;if(cF[i] == 'a') hash['o']--;if(cF[i] == 'k') hash['a']--;hash[cF[i]]++;}else    return -1;}else{if(hash['k'] > 0)hash['k']--;hash['c']++;}}for(int ch = 0; ch < 130; ch++){if(ch != 'k' && hash[ch] != 0) return -1;}return hash['k'];}
};
http://www.lryc.cn/news/455166.html

相关文章:

  • 【网络协议大花园】应用层 http协议的使用小技巧,用好了都不用加班,效率翻两倍(上篇)
  • 今日指数day8实战补充(上)
  • Python 之进阶语法:with...as...
  • 嵌入式硬件设计知识详解
  • 计算机网络:物理层 —— 信道及其极限容量
  • 面向对象特性中 继承详解
  • C++ | Leetcode C++题解之第455题分发饼干
  • java版基于Spring Boot + Mybatis在线招投标|评标|竞标|单一采购|询价|邀标|在线开标|招标公告发布|评审专家|招投标采购系统源码
  • Anaconda的安装与环境设置
  • 使用FastAPI做人工智能后端服务器时,接口内的操作不是异步操作的解决方案
  • Leetcode 3312. Sorted GCD Pair Queries
  • 用 Delphi 做了一个简单的 CMS
  • ASK, PSK, FSK, DPSK
  • 【Linux】认识Linux内核中进程级别的文件结构体【files_struct】&文件IO模型初步演示
  • [Offsec Lab] ICMP Monitorr-RCE+hping3权限提升
  • Studying-多线程学习Part4 - 异步并发——async future、packaged_task、promise
  • 【Java基础】用Scanner类获取控制台输入
  • 微服务seata解析部署使用全流程
  • Linux性能调优技巧
  • python 实现sha1算法
  • ejb-ref元素
  • Perl 子程序(函数)
  • ElasticSearch 备考 -- Snapshot Restore
  • 【Linux】进程替换、命令行参数及环境变量(超详解)
  • MySQL事务日志—redo日志介绍
  • 告别音乐小白!字节跳动AI音乐创作工具,让你一键变作曲家!
  • 空心正方形图案
  • 【EXCEL数据处理】000020 案例 保姆级教程,附多个操作案例。EXCEL使用表格。
  • 虾皮Shopee大数据面试题及参考答案
  • 重学SpringBoot3-集成Redis(六)之消息队列