当前位置:网站首页>Rotate K strings to the left (details)
Rotate K strings to the left (details)
2022-07-26 00:32:00 【Node_ Hao】
Today I will bring you a very interesting question Rotate to the left k A string , The conventional solution is very simple , But what I'm talking about today is another interesting method , It may be a great inspiration for you to brush questions in the future .
One . Violence solution :
Violence solution : first floor for loop , The number of overall traversal is to rotate several strings , The second floor for The cycle is the number of exchanges .
#include<stdio.h>
#include<string.h>
void left_rotate(char arr[], int k) {
int len = strlen(arr);
k %= len;// No matter what k How big , Search times are inevitable <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;
}Two . Novel solution :
Suppose we have a string :"abcdef", And rotate two strings to the left . So we can put "abcdef" It is divided into "ab","cdef" First rotate separately "ba" and "fedc" Turn into "bafedc", Then the whole rotation becomes "cdefab". Isn't it amazing ?

#include<stdio.h>
#include<string.h>
#include<assert.h>
// Method 2 :
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;
}summary :
The above are the two solutions I brought. I hope they will be helpful to your follow-up study .
边栏推荐
- Detailed explanation of C language preprocessing
- Flask发送验证码逻辑
- Linked list related methods
- Four characteristics and isolation level of MySQL transactions
- [redis] ③ data elimination strategy, pipeline command, publish and subscribe
- The way to understand JS: the principle of object.call and object.create() inheritance
- MySQL - database log
- Research on the influence of opinion leaders based on network analysis and text mining
- 【目录】mqtt、nodejs项目
- 向左旋转k个字符串(细节)
猜你喜欢

向左旋转k个字符串(细节)

OPENCV学习DAY6

【Redis】① Redis 的介绍、Redis 的安装

8个小妙招-数据库性能优化,yyds~
![[redis] ② redis general command; Why is redis so fast?; Redis data type](/img/72/aaa90d5411b8b20b15a7f87b98bd27.png)
[redis] ② redis general command; Why is redis so fast?; Redis data type

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

软件测试同行评审到底是什么?

12.神经网络模型

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

Find and locate commands
随机推荐
Data driven DDT for automated testing
Modeling and simulation analysis of online medical crowdfunding communication based on SEIR model
Eight common SQL misuses of MySQL, all of which I have learned
二进制表示--2的幂
How to use 120 lines of code to realize an interactive and complete drag and drop upload component?
Tarjan 求强连通分量 O(n+m) ,缩点
Pinduoduo gets the usage instructions of the product details API according to the ID
寻找命令find和locate
What is software testing peer review?
After using MQ message oriented middleware, I began to regret
JVM 三色标记法与读写屏障
Research progress of data traceability based on the perspective of data element circulation
Flask发送验证码逻辑
What is Web3 game?
Detailed explanation of C language preprocessing
试除法--3的幂
Find and locate commands
CountDownLatch
Four characteristics and isolation level of MySQL transactions
使用CMake编译OpenFoam求解器
