惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - vcfly

理解性记忆const修饰普通变量和指针的新思路 - vcfly - 博客园 戏剧性的中超2008, 戏剧性的最后四轮,戏剧性的过程, 如我所愿的结果 Gmail换界面了 Hold fast and let go ... 一道逻辑推理题: 猜生日 刚听了许巍的<<风行>>和<<故事>>, 软件开发之胡言乱语5-团队协作 软件开发之胡言乱语4: 实践与软件开发 Google is turning 10 软件开发之胡言乱语3-代码质量 软件开发之胡言乱语2 软件开发之胡言乱语1 决定个人软件质量高低的几个因素 调查: 哪些windows应用软件是用C#写的?哪些网站是用Asp.net写的? ZZ:<<给新人程序员的八点建议>> 越来越看不懂<<程序员>>... 写程序就像是上厕所 谁说Gmail不需要删邮件?! 单位门口的门卫
数组 循环位移 或 循环移动 (左移 或 右移) K位
vcfly · 2008-11-07 · via 博客园 - vcfly

指定一个数组,比如整数或字符串, 长度为N, 将其循环右移K位.

以下是我的解法: 只需要遍历一次数组即可. 空间复杂度是o(1), 时间复杂度是o(N).
不同于其他的解法: 1) 不需要求GCD(N,K) 2)不需要遍历2遍数组(STL源码中的reverse算法)


void Output(int *pBuffer, int nCount)
{
    if(!pBuffer || !nCount) return;

    for (size_t i = 0; i < nCount; i++)
    {
        printf(" %d ", pBuffer[i]);
    }

    printf("\n");

}

void ShiftN(int *pBuffer, int nCount, int nShiftN)
{
    if(!pBuffer || !nCount || !nShiftN) return;

    nShiftN %= nCount;

    int nIndex = 0;
    int nStart  = nIndex;

    int nTemp  = pBuffer[nIndex];

    for (size_t i = 0; i < nCount; i++)
    {
        nIndex = (nIndex + nShiftN) % nCount;

        pBuffer[nIndex] ^= nTemp ^=
        pBuffer[nIndex] ^= nTemp ;

        if(nIndex == nStart)
        {
            nStart ++;
            nIndex = nStart;
            nTemp = pBuffer[nIndex];
        }
    }
}

int main(int argc, char* argv[])
{
    int buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

    int nCount = sizeof(buffer) / sizeof(int);

    Output(buffer, nCount);

    ShiftN(buffer, nCount, 8);

    Output(buffer, nCount);
   
    return 0;
}