Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the ordered queue with the leetcode skills of golang

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to achieve an ordered queue with golang leetcode skills. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Given a string S consisting of lowercase letters. Then we can make any number of moves.

In each move, we select one of the first K letters (starting from the left), remove it from its original position, and place it at the end of the string.

Returns the smallest lexicographically ordered string we can have after any number of moves.

Example 1:

Input: S = "cba", K = 1 Output: "acb" Explanation: In the first step, we move the first character ("c") to the end to get the string "bac". In the second step, we move the first character ("b") to the end to get the final result "acb".

Example 2:

Input: S = "baaca", K = 3 Output: "aaabc" Explanation: In the first step, we move the first character ("b") to the end to get the string "aacab". In the second step, we move the third character ("c") to the end to get the final result "aaabc".

Tip:

When 1 = 2, we can shift the string to get the smallest dictionary order.

code

func orderlyQueue(S string, K int) string { if K==1{ s:=S for i:=0;i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report