当前位置:网站首页>Leetcode-9-palindromes (simple)
Leetcode-9-palindromes (simple)
2022-06-13 00:59:00 【Didi dada】
9. Palindrome number ( Simple )
Give you an integer x , If x Is a palindrome integer , return true ; otherwise , return false .
Palindrome number refers to positive order ( From left to right ) Reverse order ( From right to left ) Read all the same integers . for example ,121 It's palindrome. , and 123 No .
** Advanced :** Can you solve this problem without converting integers into strings ?
Example 1:
Input :x = 121
Output :true
Example 2:
Input :x = -121
Output :false
explain : Read left to right , by -121 . Read right to left , by 121- . So it's not a palindrome number .
Ideas :
- Negative numbers are not palindromes
- Determine whether a positive number is a palindrome number , Judge whether the numbers before and after reversal are equal
- Convert a number into a string and judge
- Do not convert string , utilize %10 and /10, Reverse the number , Then judge whether it is equal or not
(1)C++
A time-consuming version of a string
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)
return false;
string s = to_string(x);
string s1 = s;
reverse(s1.begin(),s1.end());
if(s1==s)
return true;
else
return false;
}
};
Take advantage of a short, time-consuming version of string pointers
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)
return false;
string s = to_string(x);
bool temp=true;
int i =0 , j=s.size()-1;
while(i<j){
if(s[i]!=s[j]){
temp = false;
break;
}
else{
i++;
j--;
}
}
return temp;
}
};
(2)C++( No strings 、 Compare the first and last digits of a number )
Time consuming versions
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)
return false;
int temp = x;
long long int y=0;
while(temp){
y = y*10 + temp%10;
temp=temp/10;
}
if(x==y)
return true;
else
return false;
}
};
A short time-consuming version
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)
return false;
int len = 1;
bool temp=true;
while(x / len >= 10) { //len Of 1 The location of is current x The position of the first digit of
len = len * 10;
}
while(x){
int left = x/len;
int right = x%10;
if(left!=right){
temp=false;
break;
}
x = x%len;
x = x/10;
len=len/100; // because x Two missing
}
return temp;
}
};
(2)python
String version
class Solution:
def isPalindrome(self, x: int) -> bool:
if(x<0):
return False
s = str(x)
n = len(s)
for i in range(n//2):
j=-(i+1)
if(s[i]!=s[j]):
return False
return True
Non string version
class Solution:
def isPalindrome(self, x: int) -> bool:
if(x<0):
return False
l = 1
while(x//l >=10):
l=l*10
while(x):
left = x // l
right = x % 10
if(left!=right):
return False
x = x%l
x = x//10
l = l//100
return True
边栏推荐
- Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
- Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
- pytorch是什么?解释pytorch的基本概念
- Unitywebrequest asynchronous Download
- (01). Net Maui actual construction project
- 深度学习模型剪枝
- How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic benefits of ASI, VR, arbr, DPO and trix indicators
- Canvas random bubbling background
- Characteristics of transactions -- atomicity (implementation principle)
- [JS component library] drag sorting component
猜你喜欢

Four startup modes of kotlin collaboration

Pipeline流水线项目构建

Illustrator tutorial, how to add dashes and arrows in illustrator?

Undirected graph -- computing the degree of a node in compressed storage

Et5.0 simply transform referencecollectorieditor

Download nail live playback through packet capturing

spiral matrix visit Search a 2D Matrix

gpu加速pytorch能用吗?
![[JS component] browse progress bar](/img/cb/913f446db2cacdb965a3bf619aa478.jpg)
[JS component] browse progress bar

Traditional machine learning classification model predicts the rise and fall of stock prices
随机推荐
[JS component] simulation framework
[JS] solve the problem that removeeventlistener is invalid after the class listening event from new is bound to this
How to determine whether T is a value type in a generic type or a reference class- How to determine whether T is a value type or reference class in generic?
Matrix fast power
sort
Pipeline pipeline project construction
Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
生态聚合NFT来袭,Metaverse Ape引领Web 3.0元宇宙新范式革命
[JS component] dazzle radio box and multi box
深度学习每周期的步数多少合适?
[JS component] customize the right-click menu
什么是 Meebits?一个简短的解释
Android Weather
Hard (magnetic) disk (I)
Influence of higher order poles on waveform
Sequence table - find main element
People and gods are angry. Details of Tangshan "mass beating of women incident"
MySQL transaction
[latex] insert picture
Et5.0 configuring Excel