每日一题之《剑指offer》49,50题

第49题:把字符串转换成整数

难易度:⭐

题目描述:
将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 
数值为0或者字符串不是一个合法的数值则返回0
输入一个字符串,包括数字字母符号,可以为空
如果是合法的数值表达则返回该数字,否则返回0
示例:
输入
"+2147483647"
输出
2147483647

输入
"1a33"
输出
0

这个代码写的很烂,没时间改了,二刷的时候一定改进 ( - - )
需要注意几点:

  1. 允许有正负号
  2. 注意越界

代码如下:

public class Solution {
    public int StrToInt(String str) {
        if(str == null || str.length() == 0){
            return 0;
        }
        char[] chs = str.toCharArray();
        int len = str.length();
        int lenTemp = len;
        boolean isPositive = true;
        long res = 0L;
        for(int i = 0;i < len;i++){
            if(i == 0){
                if(chs[i] == '-'){
                    isPositive = false;
                    lenTemp--;
                }else if(chs[i] == '+'){
                    lenTemp--;
                }else{
                    if(chs[i] < '0' || chs[i] > '9'){
                        return 0;
                    }else{
                        res += (int)((chs[i] - '0') * Math.pow(10,lenTemp - 1));
                        lenTemp--;
                    }
                    
                }
            }else{
                if(chs[i] < '0' || chs[i] > '9'){
                        return 0;
                }else{
                    res += (int)((chs[i] - '0') * Math.pow(10,lenTemp - 1));
                    lenTemp--;
                }
            }
        }
        if(isPositive){
            return res > Integer.MAX_VALUE ? 0 : (int)res;
        }else{
            return -res < Integer.MIN_VALUE ? 0 : (int)(-1 * res);
        }
    }
}

第50题:数组中重复的数字

难易度:⭐⭐

题目描述
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 
数组中某些数字是重复的,但不知道有几个数字是重复的。
也不知道每个数字重复几次。
请找出数组中任意一个重复的数字。
 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

本题有两种思路:

  1. 用空间换时间
    不难想到,本题使用哈希表这种数据结构,就可以很快找到重复的数字,代码如下:
import java.util.HashSet;
public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    这里要特别注意~返回任意重复的一个,赋值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        HashSet<Integer> set = new HashSet<>();
        for(int i = 0;i < length;i++){
            if(set.contains(numbers[i])){
                duplication[0] = numbers[i];
                return true;
            }else{
                set.add(numbers[i]);
            }
        }
        return false;
    }
}
  1. 不使用额外的数据结构,即要求额外空间复杂度为O(1),概念上属于拿时间换空间的做法
    类似二分法的思路:将n个数字中间数字m分为两个部分,前一半为0~m,后一半为m+1~n。如果0~m的数字的数目超过了m,那么这一版的区间一定包含重复的数字,否则,量一般m+1 ~ n的区间里,一定包含重复的数字。我们可以一直讲包含重复数字的区间一分为二,直到找到重复的数字为止,因为二分策略的时间复杂度为O(logn),每次我们还要遍历数组,时间复杂度为O(nlogn),但是额外空间复杂度则为O(1),所以这是一种时间换空间的策略。
    代码如下:
public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    这里要特别注意~返回任意重复的一个,赋值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        if(numbers == null){
            return false;
        }
        int start = 0;
        int end = length - 1;
        while(end >= start){
            int middle = ((end - start) >> 1) + start;
            int count = count(numbers,length,start,middle);
            if(end == start){
                if(count > 1){
                    duplication[0] = start;
                    return true;
                }else{
                    break;
                }
            }
            if(count > (middle - start + 1)){
                end = middle;
            }else if(count == (middle - start + 1)){
                start = start + 1;
            }else{
                start = middle + 1;
            }
        }
        return false;
    }
    
    public static int count(int numbers[],int length,int start,int middle){
        if(numbers == null){
            return 0;
        }
        int res = 0;
        for(int i = 0;i < length;i++){
            if(numbers[i] >= start && numbers[i] <= middle){
                res++;
            }
        }
        return res;
    }
}
全部评论

相关推荐

爪哇沉淀ing:哎 感觉很丰富 其实没啥含金量 我本科也是理工的 实话实说这个学校真的没啥竞争力 建议还是提升学历
今天你投了哪些公司?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
9135次浏览 83人参与
# 你的实习产出是真实的还是包装的? #
1684次浏览 40人参与
# 巨人网络春招 #
11297次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7382次浏览 40人参与
# 重来一次,我还会选择这个专业吗 #
433301次浏览 3926人参与
# 简历第一个项目做什么 #
31500次浏览 327人参与
# 米连集团26产品管培生项目 #
5612次浏览 214人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186885次浏览 1118人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152269次浏览 887人参与
# 研究所笔面经互助 #
118851次浏览 577人参与
# 简历中的项目经历要怎么写? #
309944次浏览 4189人参与
# 面试紧张时你会有什么表现? #
30473次浏览 188人参与
# 你今年的平均薪资是多少? #
212980次浏览 1039人参与
# AI时代,哪些岗位最容易被淘汰 #
63310次浏览 798人参与
# 我的求职精神状态 #
447961次浏览 3128人参与
# 你最满意的offer薪资是哪家公司? #
76415次浏览 374人参与
# 高学历就一定能找到好工作吗? #
64294次浏览 620人参与
# 牛客AI文生图 #
21399次浏览 238人参与
# 你怎么看待AI面试 #
179799次浏览 1229人参与
# 正在春招的你,也参与了去年秋招吗? #
363190次浏览 2636人参与
# 腾讯音乐求职进展汇总 #
160562次浏览 1109人参与
# 职能管理面试记录 #
10795次浏览 59人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务