leetcode刷题

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;
       }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值