当前位置:网站首页>向左旋转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;
}总结:
以上就是我带来的两种解法希望对你后续学习有所帮助.
边栏推荐
- IP核:PLL
- LeetCode_ 55_ Jumping game
- 【Redis】② Redis通用命令;Redis 为什么这么快?;Redis 的数据类型
- 为了拿捏 Redis 数据结构,我画了 40 张图(完整版)
- 【论文笔记】—目标姿态估计—EPro-PnP—2022-CVPR
- What is software testing peer review?
- Understanding of "dbdnet: a deep boosting strategy for imagedenoising"
- Detailed explanation of C language preprocessing
- What are the precautions for using MySQL index? (answer from six aspects)
- Applet page generation link sent by SMS
猜你喜欢

VMware ESXI7.0版本的安装与配置
![[hero planet July training leetcode problem solving daily] 25th tree array](/img/e6/a59a1719c4381772ce7475d59d5068.png)
[hero planet July training leetcode problem solving daily] 25th tree array

Pikachu靶机通关和源码分析

对比7种分布式事务方案,还是偏爱阿里开源的Seata(原理+实战)

Leetcode high frequency question 66. add one, give you an array to represent numbers, then add one to return the result

OPENCV学习DAY6

The way of understanding JS: what is prototype chain

Pinduoduo gets the usage instructions of the product details API according to the ID

The way of understanding JS: write a perfect combination inheritance (Es5)

Duplicate disk: recommended system - negative sampling strategy
随机推荐
Four characteristics and isolation level of MySQL transactions
GOM和GEE引擎黑屏不显示界面,装备地图怪物的解决方法
获得JD商品详情原数据 API
IP核:PLL
试除法--3的幂
"Demons dance", is the bull market over? 2021-05-13
[redis] ② redis general command; Why is redis so fast?; Redis data type
How to make your JS code more beautiful
2022/7/19 考试总结
C语言 预处理详解
Wechat applet dynamic style | parameter transfer
Unified handling of global exceptions
计算物理期刊修改
MPLS experiment
[hero planet July training leetcode problem solving daily] 25th tree array
PC website realizes wechat code scanning login function (II)
本地电脑架设传奇怎么开外网叫朋友一起玩?
The way of understanding JS: what is prototype chain
After seven years of testing, the interview with Huawei finally negotiated a salary of 10000. HR said that I didn't respect Huawei and they didn't have such a low salary position~
拼多多根据ID取商品详情 API 的使用说明
