当前位置:网站首页>牛客网:判断是否为回文字符串
牛客网:判断是否为回文字符串
2022-06-22 17:30:00 【lsgoose】

这里用两种方法来解决
目录
1.双指针
我们可以两头往中间或者中间往两边,总之都是利用了这个对称的结构
代码如下所示:
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param str string字符串 待判断的字符串
* @return bool布尔型
*/
bool judge(string str) {
// write code here
int left=0;
int right=str.length()-1;
while(left<right){
if(str[left]!=str[right]){
return false;
}
left++;
right--;
}
return true;
}
};2.栈(翻转)
如果利用栈的思想就是翻转一下字符串即可。然后用这个字符串和原字符串相比是否有不同之处。
代码如下所示:
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param str string字符串 待判断的字符串
* @return bool布尔型
*/
bool judge(string str) {
// write code here
string tmp=str;
reverse(tmp.begin(), tmp.end());
if(tmp!=str){
return false;
}
return true;
}
};边栏推荐
- Database industry analysis: from the global IT industry trend to the development of domestic databases
- Jenkins容器安装ruby-runtime插件失败报错解决
- China's two meteorological "new stars" data products are shared with global users
- Pre training language model, Bert, roformer SIM, also known as simbertv2
- Pytoch -- error reporting solution: "torch/optim/adamw.py" beta1, unboundlocalerror: local variable 'beta1‘
- wpa_supplicant的状态机迁移
- Introduction to rsps2022 finalist | Dr. Yang Bai
- 中国移动手机用户缓慢增长,但努力争取高利润的5G套餐用户
- SystemVerilog (12) - $unit declaration space
- Sort---
猜你喜欢

Nuxt - create nuxt app
传输层 知识点总结

< JVM part I: memory and garbage collection part > 08 object instantiation and direct memory

腾讯云国际版云服务器欠费说明

【win11】注册表修改fix 右键没有新建

Using stream API instead of SQL

Explain the startup process of opengauss multithreading architecture in detail

2022年G2电站锅炉司炉题库及在线模拟考试

The world's first AR contact lens, the entrance of metauniverse is really opened this time?

math_ Angular function & inverse trigonometric function
随机推荐
Beijing restorer's half moon: how to rekindle the fireworks in store management
jsp连接MySQL总出错
SystemVerilog (12) - $unit declaration space
GPIO operation method of imx6ull
请问一下,CDC2.0在读取mysql的时候,应该是没有表锁了吧,刚刚报了一个错误,access d
不断重修的计划与变化
You guys, the first time you used flick MySQL CDC, now the program starts without error, and the new data is not printed out
Linked list 4- 21 merge two ordered linked lists
Is it safe for Ping An Securities to open an account? What is its relationship with Ping An Bank?
阿里云过户找不到账号安全组ID问题
High voltage direct current (HVDC) model based on converter (MMC) technology and voltage source converter (VSC) (implemented by MATLAB & Simulink)
阻碍华为5G手机的关键芯片取得突破,国产芯片已取得一成份额
[learn shell programming easily]-4. The difference between single quotation marks and double quotation marks, the operation of integer values, the definition of arrays in the shell and the detailed us
Jenkins的安装与升级
Array implementation of circular linked list
2022年5月中国游戏厂商及应用出海 EMEA 地区收入30强
2022重庆幼教产业展览会|高科技玩具益智解压玩具博览会
Alibaba cloud cannot find the account security group id problem during the account transfer
SystemVerilog(十二)-$unit声明空间
Golang implements redis (10): local atomic transactions