当前位置:网站首页>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
边栏推荐
- SAP SD module foreground operation
- 谁说程序员不懂浪漫,表白代码来啦~
- 【虚拟户生态平台】虚拟化平台安装时遇到的坑
- 计算首屏时间
- html select tag assignment database query result
- FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
- 实例035:设置输出颜色
- Intranet penetration - application
- Sticker Spelling - Memory Search / Shape Pressure DP
- Flask Framework Beginner-06-Add, Delete, Modify and Check the Database
猜你喜欢

The idea of the diagram

Example: 036 is a prime number

工程制图平面投影练习

Example 035: Setting the output color

nodejs+express实现数据库mysql的访问,并展示数据到页面上

Kubernetes:(十一)KubeSphere的介绍和安装(华丽的篇章)

数组_滑动窗口 | leecode刷题笔记

实例038:矩阵对角线之和

Flask Framework Beginner-05-Command Management Manager and Database Use

nodejs+express realizes the access to the database mysql and displays the data on the page
随机推荐
sql有关问题,小时粒度,找到前一个小时内的数据
boot issue
boot issue
IDEA02:配置SQL Server2019数据库
持续投入商品研发,叮咚买菜赢在了供应链投入上
5.scrapy中间件&分布式爬虫
halcon自定义函数基本操作
Multithreading JUC Learning Chapter 1 Steps to Create Multithreading
数组_滑动窗口 | leecode刷题笔记
工程制图名词解释-重点知识
持续投入商品研发,叮咚买菜赢在了供应链投入上
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
可变字符串
实例037:排序
22/8/3(板子)树状dp板子+中国剩余定理+求组合数3,4+容斥原理
GNSS[0]- Topic
工程制图平面投影练习
哎,又跟HR在小群吵了一架!
Variable string
redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)