Nine-chapter algorithm surface question 55 rotating string

Source: Internet
Author: User

Nine Chapters count judges Net-original website

http://www.jiuzhang.com/problem/55/


Topics

Offsets a string and a rotated offset, and shifts the string to the right offset bit.

For example: "ABCDEFG" Loop shifted right 4 bit after changed to: "DEFGABC"

Requires an O (1) extra space cost, O (n) time


Online testhttp://lintcode.com/problem/rotate-string/


Answer

The three-step inversion method is used.

Take s= "ABCDEFG" offset=4 as an example


First, the string is considered: "abc" + "DEFG"

First overall reversal: Get "gfed" + "CBA"

Then each reversal: Get "DEFG" + "abc" = "Defgabc"


Time complexity O (n), additional space complexity O (1)

Nine-chapter algorithm surface question 55 rotating string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.