当前位置:网站首页>LeetCode:899. 有序队列【思维题】
LeetCode:899. 有序队列【思维题】
2022-08-04 01:43:00 【星空皓月】
题目描述

思路
当k = 1时,轮转字符串,得到一个字典树最小的字符串;
当k = 2时,可以想象成一个环,将s[0]单拿出来,让字符串转动,可以放到任意位置,s[1],s[2]…都是如此,这样就能使得将整个字符串的每一位调整到任意位置。所以将该字符串排为升序即可。
AC代码
class Solution:
def orderlyQueue(self, s: str, k: int) -> str:
if k == 1:
ans = s
for i in range(len(s) - 1):
s = s[1:] + s[0]
ans = min(ans, s)
return ans
return ''.join(sorted(s)) # 注sorted返回的时list,要将其转化为str
边栏推荐
- FeatureNotFound( bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requested:
- 【Untitled】
- 《Greenplum构建实时数据仓库实践》简介
- 贪吃蛇游戏Bug解析及功能扩展
- ASP.NET 获取数据库的数据并写入到excel表格中
- Simple sorting (summer vacation daily question 14)
- Analysis of usage scenarios of mutex, read-write lock, spin lock, and atomic operation instructions xaddl and cmpxchg
- GNSS【0】- 专题
- Kubernetes:(十一)KubeSphere的介绍和安装(华丽的篇章)
- Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)
猜你喜欢

【虚拟户生态平台】虚拟化平台安装时遇到的坑

MySQL回表指的是什么

Flask Framework Beginner-06-Add, Delete, Modify and Check the Database

如何用C语言代码实现商品管理系统开发

Quickly build a website with static files

Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment

实例038:矩阵对角线之和

工程制图复习题(带答案)

Parquet encoding

DDTL:远距离的域迁移学习
随机推荐
The idea of the diagram
《Greenplum构建实时数据仓库实践》简介
简单排序(暑假每日一题 14)
工程制图复习题(带答案)
nodejs切换版本使用(不需要卸载重装)
JS 从零教你手写节流throttle
工程制图名词解释-重点知识
GraphQL背后处理及执行过程是什么
Is there any jdbc link to Youxuan database documentation and examples?
Promise solves blocking synchronization and turns asynchronous into synchronous
HBuilderX的下载安装和创建/运行项目
【正则表达式】笔记
敏捷交付的工程效能治理
Promise 解决阻塞式同步,将异步变为同步
Thinkphp commonly used techniques
esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)
C 学生管理系统_添加学生
What is SVN (Subversion)?
实例037:排序
Priority_queue element as a pointer, the overloaded operators