当前位置:网站首页>向左旋转k个字符串(细节)
向左旋转k个字符串(细节)
2022-07-26 00:17:00 【Node_Hao】
今天给大家带来一道非常有趣的题向左旋转k个字符串,常规解法非常简单,但我今天主要讲的的是另一个有趣的方法,可能对大家今后的刷题大有启发.
一.暴力解法:
暴力解法:第一层for循环,整体遍历的次数也就是要旋转几个字符串,第二层for循环是交换的次数.
#include<stdio.h>
#include<string.h>
void left_rotate(char arr[], int k) {
int len = strlen(arr);
k %= len;//无论k为多大,查找次数必然<len-1
for (int i = 0; i < k; i++) {
char tmp = arr[0];
for (int j = 0; j < len-1; j++) {
arr[j] = arr[j+1];
}
arr[len-1] = tmp;
}
}
int main() {
char arr[] = "abcdef";
int k = 0;
scanf_s("%d", &k);
left_rotate(arr, k);
printf("%s", arr);
return 0;
}二.新颖解法:
假设我们有字符串:"abcdef",并且向左旋转两个字符串.那么我们就可以将"abcdef"分为"ab","cdef"先分别旋转"ba"和"fedc"变为"bafedc",然后再整体旋转变成"cdefab".是不是非常神奇?

#include<stdio.h>
#include<string.h>
#include<assert.h>
//方法二:
void reverse(char* left, char* right) {
assert(left && right);
while (left < right) {
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
void left_rotate(char arr[],int k,size_t len) {
k %= len;
reverse(arr, arr + k - 1);
reverse(arr + k, arr + len - 1);
reverse(arr, arr + len - 1);
}
int main() {
char arr[] = "abcdef";
int k = 0;
size_t len = strlen(arr);
scanf_s("%d", &k);
left_rotate(arr,k,len);
printf("%s", arr);
return 0;
}总结:
以上就是我带来的两种解法希望对你后续学习有所帮助.
边栏推荐
- 2022/7/25 考试总结
- SSM environment integration
- Study on gene targeting preparation of tissue plasminogen activator loaded on albumin nano ultrasonic microbubbles
- Installation and configuration of VMware esxi7.0
- 自动化测试之数据驱动DDT
- Representation and implementation of stack (C language)
- nodejs启动mqtt服务报错SchemaError: Expected `schema` to be an object or boolean问题解决
- JSON data development
- Solid smart contract development - 3.2-solid syntax array, structure, mapping
- Distributed transactions: the final consistency scheme of reliable messages
猜你喜欢

白蛋白纳米粒表面修饰低分子量鱼精蛋白LMWP/PEG-1900修饰牛血清白蛋白制备研究

【Redis】② Redis通用命令;Redis 为什么这么快?;Redis 的数据类型

Study on gene targeting preparation of tissue plasminogen activator loaded on albumin nano ultrasonic microbubbles

Tarjan 求强连通分量 O(n+m) ,缩点

Preparation of bovine serum albumin modified by low molecular weight protamine lmwp/peg-1900 on the surface of albumin nanoparticles

Redis killed twelve questions. How many questions can you carry?

关于“DBDnet: A Deep Boosting Strategy for ImageDenoising“一文理解

Detailed explanation of C language preprocessing

Elementary C language - branch statements (if, switch)

FreeRTOS personal notes - mutex
随机推荐
[directory] nodejs, NPM, yarn, bug
Introduction of MySQL transactions
8种MySQL常见SQL错误用法,我全中
The way to understand JS: the principle of object.call and object.create() inheritance
Matlab makes the image of serial port output data in real time
34 use of sparksql custom functions, architecture and calculation process of sparkstreaming, dstream conversion operation, and processing of sparkstreaming docking Kafka and offset
寻找命令find和locate
Private cloud disk setup
[redis] ③ data elimination strategy, pipeline command, publish and subscribe
TID-MOP:面向数据交易所场景下的安全管控综合框架
白蛋白纳米粒表面修饰低分子量鱼精蛋白LMWP/PEG-1900修饰牛血清白蛋白制备研究
12.神经网络模型
Binary representation -- power of 2
markdown写作平台
LCA 三种姿势(倍增,Tarjan+并查集,树链剖分)
2022/7/19 考试总结
对“DOF: A Demand-oriented Framework for ImageDenoising“的理解
Study on gene targeting preparation of tissue plasminogen activator loaded on albumin nano ultrasonic microbubbles
Eight common SQL misuses of MySQL, all of which I have learned
The way to understand JS: six common inheritance methods of JS
