Maintain the official account:, like
bigsai
and then read, develop a habit!
08 string to integer
Title description:
Analysis:
This question is mainly about the processing of strings. Need to pay attention to the following points:
- Remove the blank character before the character
- The first valid character must be a symbol or number
- There can only be one symbol
- Note that the value is out of range
So in specific processing, you can intercept a character and then directly convert it into a number type (considering out of bounds) but it is not recommended. Personally, when traversing a string of numeric characters, the '0'
difference between the character and the character is converted into a number for calculation, and it stops directly when it exceeds the int range.
The pass code is:
public static int myAtoi(String str) {
int zheng = 1;
int index=0;
long value=0;
while (index<str.length()&&str.charAt(index)==' ') {
//防止"" 和 " "等
index++;
}
if(index>str.length()-1)return 0;
if(str.charAt(index)=='+') {
index++;}
else if (str.charAt(index)=='-') {
zheng=-1;index++;
}
for(int j=index;j<str.length();j++)
{
if(str.charAt(j)>='0'&&str.charAt(j)<='9')
{
value=value*10+str.charAt(j)-'0';
if(value*zheng>Integer.MAX_VALUE)return Integer.MAX_VALUE;
if(value*zheng<Integer.MIN_VALUE)return Integer.MIN_VALUE;
}
else {
break;
}
}
value=zheng*value;
return (int)value;
}
09 palindrome
description:
- Palindrome number
Determine whether an integer is a palindrome number. The palindrome number refers to the same integer in both positive order (from left to right) and reverse order (from right to left).
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: Read from left to right, it is -121. Reading from right to left, it is 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reading from right to left, it is 01. Therefore it is not a palindrome.
Analysis:
This question is relatively simple, the following points need to be considered:
- Cannot be negative, negative number does not meet the requirements of palindrome number
- Consider the character of odd and even length numbers
Two methods are provided: the first is to convert numbers into strings, and expand the comparison from the middle to both sides.
The implementation code is:
//11%
public boolean isPalindrome(int x) {
if(x<0)return false;
String va=x+"";
int left=0,right=0;
if(va.length()%2==0)
{
left=va.length()/2-1;right=left+1;
}
else {
left=va.length()/2;right=left;
}
while (left>=0) {
if(va.charAt(left)!=va.charAt(right))
return false;
left--;right++;
}
return true;
}
Unfortunately, this method is relatively inefficient and can only defeat 11% of the people, about 18ms.
But you can change the way of thinking, using strings is less efficient. You can use the number type to calculate the reverse value and then compare whether the final value is the same:
public boolean isPalindrome(int x) {
if(x<0)return false;
int team=x;
int va=0;
while (x>0) {
va=va*10+x%10;
x/=10;
}
if(va==team)return true;
return false;
}
This is about 9-10ms, 9ms probably beats 98% and 10ms only more than 40%.
At last
The opening this week is over, and the opening question this week is:
05 The longest palindrome string 06
Z-shaped transformation & 07 integer inversion
Welcome to like and follow. You can also add the author's WeChat public account:, bigsai
reply to the group to join the check-in.