zoukankan      html  css  js  c++  java
  • 数据结构——算法之(010)( 字符串的左旋转操作)

    【申明:本文仅限于自我归纳总结和相互交流,有纰漏还望各位指出。 联系邮箱:Mr_chenping@163.com】

    题目:定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部。如把字符串abcdef左旋转2位得到字符串cdefab。
    请实现字符串左旋转的函数。要求对长度为n的字符串操作的时间复杂度为O(n),空间复杂度为O(1)。

    题目分析:

    一、方法非常多,仅仅实现当中的一种。方法非常easy直接看代码实现

    算法实现:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*
    ** 左移一位
    */
    char * left_one_step(char *s, int size)
    {
    	char c = s[0];
    	int i=0;
    	for( ;i<size; ++i)
    		s[i] = s[i+1];
    	s[--i] = c;
    	return s;
    }
    
    char * left_n_step(char *s, int size, int n)
    {
    	char *re = s;
    	/*
    	**循环次数大于字符串长度后,取余数
    	**降低循环次数
    	*/
    	int m = n%size; 
    	while(m--)
    	{
    		re = left_one_step(re, size);
    	}
    	return re;
    }
    
    int main(int argc, char *argv[])
    {
    	char *str = strdup(argv[1]);
    	printf("%s--left:%d--->%s
    ", argv[1], atoi(argv[2]), left_n_step(str, strlen(str), atoi(argv[2])));
    	free(str);
    
    	return 0;
    }
    


  • 相关阅读:
    获取经纬度 CLLocation
    获取手机 IP
    UIBeaierPath 与 CAShapeLayer
    导航栏转场动画CATransition
    UITextField输入限制/小数/首位等
    程序进入后台继续执行
    发送短信
    网络AFNetworking 3.1
    UIBezierPath
    CoreAnimation 核心动画 / CABasicAnimation/ CAKeyframeAnimation
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5244549.html
Copyright © 2011-2022 走看看