question1: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] add = new int[2];
int len = nums.length;
for(int i=0;i<len;i++){
for(int j=i+1;j<len;j++){
if(nums[i]+nums[j]==target){
add[0]=i;
add[1]=j;
break;
}
}
}
return add;
}
}
question2:给你一个 32 位的有符号整数 x ,返回 x 中每位上的数字反转后的结果。
如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0。
假设环境不允许存储 64 位整数(有符号或无符号)。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public int reverse(int x) {
int res = 0;
while(x!=0){
int i = x%10;
if(res>214748364||(res==214748364 && i>7)){
return 0;
}
if(res<-214748364||(res==-214748364&&i>8)){
return 0;
}
res =res*10+i;
x =x/10;
}
return res;
}
}
question3:给你一个整数 x ,如果 x 是一个回文整数,返回 ture ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/palindrome-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public boolean isPalindrome(int x) {
int y = x;
if(y<0){
return false;
}
int res = 0;
while(y>0){
int i = y%10;
res = res*10 + i;
y = y/10;
}
if(res == x){
return true;
}else{
return false;
}
}
}