当前位置:网站首页>Solution to C language problems of force buckle rotation string
Solution to C language problems of force buckle rotation string
2022-06-10 00:39:00 【6 Yingchen】
Power button Rotate string
List of articles
One 、 Complete title
Given two strings , s and goal.
If after several rotations ,s Can become goal , Then the return true .
s The operation of rotating is to turn s The leftmost character moves to the rightmost .
for example , if s = ‘abcde’, After one rotation, the result is ’bcdea’ .
Example 1:
Input : s = “abcde”, goal = “cdeab”
Output : true
Example 2:
Input : s = “abcde”, goal = “abced”
Output : false
Tips :
1 <= s.length, goal.length <= 100
s and goal It's made up of lowercase letters
Two 、 Their thinking
- Create a left pointer , Point to the far left after each rotation , That's the beginning
- Make a comparison after each rotation , If there is any inconsistency, continue to rotate
- Until all the characters are moved once
- Exit loop , Output results
The current position after rotation can be calculated as follows :
(left + j) % len
left For the left pointer , namely s Subscript ,j For the first few elements in the comparison process ,len For the string s The length of
3、 ... and 、 Write code
bool rotateString(char * s, char * goal){
// length
int len;
len = strlen(s);// seek s length
// The head pointer
int left = 0;
// Temporary variable
int i = 0, j;
// Completion mark
int flag = 1;
if(len != strlen(goal)) return false;
while ((i++) < len)
{
// Judge s and goal Whether it is equal or not
for (j = 0, flag = 1; j < len; j++)
{
if (s[(left + j) % len] != goal[j])
{
flag = 0;
break;
}
}
if (flag == 1) break;
// rotate
left++;
}
if (flag == 1) return true;
else return false;
}
Four 、 Evaluation results
5、 ... and 、 Summarize and evaluate
There are many ways to solve this problem , I only made one , It's also easy to think of , Start subscribing , Compare . In fact, it can also be in s Followed by another s, It's equivalent to complete rotation , Just compare directly ; Or use a linked list and so on , Let's open our minds , Do every method .
If you have any questions, you are welcome to point out
The force buckle series will be continuously updated , Welcome to your attention , Learning together
边栏推荐
- 收藏备用 | 关于OAuth2的一些常见问题总结
- Encryption, decryption and signature verification
- 力扣 自除数 C语言 题解
- Manjaro kconsole turns on translucency
- Introduction to bias and variance
- Johns Hopkins, Berkeley, California, etc. | using machine learning of sequence and structural features to achieve Generalized Prediction of antibody thermal stability
- js 逻辑空分配双问号语法 、双竖杠语法 与 可选链语法
- DFS和BFS在二叉树中的应用
- What is the WPS merge cell shortcut
- 试题 历届试题 子串分值和【第十一届】【省赛】【B组】
猜你喜欢

wps文本框如何设置透明

The problem of connecting the computer to the printer (the printer display is not specified) solution

嵌入式商业智能软件Wyn Enterprise正式步入V5.0时代!

How to set transparency for WPS text box

How to keep the contents of WPS merged cells in one cell

The essence and soul of message queue

The cornerstone mvcc mechanism of repeatable read isolation level

Facial Emotion Recognition: State of the Art Performance on FER2013

IDC权威预测,中国制造业即将乘云而上

If function selection when determining whether it is empty
随机推荐
Performance optimization scheme
试题 历届试题 子串分值和【第十一届】【省赛】【B组】
Where is the bookmark saved in Google browser and how to import and export bookmarks
wps合并单元格内容都保留在一个单元格怎么实现
德国乌尔姆大学 | 3D蛋白结构的对比表征学习
How WPS merges cells with different sizes
Mat数据的深浅拷贝
EBS R12.2的克隆
Introduction to bias and variance
Do your filial duty to make an old people's fall prevention alarm system for your family
JVM explanation
ps标尺调精确位置在哪
Win10 how to delete an account
电力系统北斗时钟同步系统的组成和配置
js 邏輯空分配雙問號語法 、雙豎杠語法 與 可選鏈語法
CloudCompare&PCL 主曲率、平均曲率以及高斯曲率计算
力扣 两数之和 C语言 题解
Record a malicious keyword brushed by illegal drainage
AQUANEE将在近期登陆Gate以及BitMart,低位布局的良机
Numpy basic operation