# 【英雄算法六月集训】Day04

2000. 反转单词前缀

先循环找到等于ch的字母,此时可知道i,然后前后两个指针,while循环去逐个swap

class Solution {
    public boolean isLetter(char ch){
        return ch >'a' || ch <'z' || ch > 'A' || ch < 'Z';
    }
    public void swap(char[] chs,int i,int j){
        char tmp = chs[i];
        chs[i] = chs[j];
        chs[j] = tmp;
    }
    public String reversePrefix(String word, char ch) {
       int i;
       char[] chs = word.toCharArray();
       for(i=0;i < word.length();++i){
           if(chs[i] == ch){
               break;
           }
       }
       if(i == word.length()){
           return word;
       }
       int j=i;
       i = 0;
       while(i <= j){
           swap(chs,i,j);
           ++i;
           --j;
       }
       return new String(chs);
    }
}

917. 仅仅反转字母

class Solution {
    
    private void swapLetterChar(char[] chars, int left, int right) {
        char tmp = chars[left];
        chars[left] = chars[right];
        chars[right] = tmp;
    }
    public String reverseOnlyLetters(String s) {
        int n = s.length();
        char[] chars = s.toCharArray();
        int left = 0, right = n - 1;
        while (true) {
            while(left<right&&!Character.isLetter(s.charAt(left))){
                left++;
            }

            while (right>left&&!Character.isLetter(s.charAt(right))){
                right--;
            }

            if(left>=right){
                break;
            }
            swapLetterChar(chars,left,right);
            left++;
            right--;
        }
        return String.valueOf(chars);
    }
}

475. 供暖器

两个数组排序,排序后的供暖器数组,可以把它暂时看成是一个区域,然后开始for循环遍历房子,每遍历一个房子,

就考虑以下几种情况

  1. 房子的位置小于供暖区域(也就是最左侧的供暖器),则半径是供暖器位置-房子的位置

  2. 房子的位置大于供暖区域(也就是最左侧的供暖器),则半径是房子的位置-供暖器的位置

  3. 房子的位置在供暖区域里,则进行二分查找,找到那个我们需要的供暖器的下标ans

    以上集中情况,在循环里用if else if 去表达即可

    最后ret,x取最大值是因为要覆盖所有房子 肯定要选最大值

class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(houses);
        Arrays.sort(heaters);
        int x,i;
        int ans;
        int ret=0;
        for(i =0;i<houses.length;++i){
            if(houses[i] < heaters[0]){
                x = heaters[0]-houses[i];
            }else if(houses[i] > heaters[heaters.length-1]){
                x = houses[i] - heaters[heaters.length-1];
            }else {
                ans = -1;
                int l = 0,r=heaters.length -1;
                while(l <= r){
                    int mid = (l+r)>>1;
                    if(heaters[mid] <= houses[i]){
                        l = mid+1;
                        ans = mid;
                    }else{
                        r = mid -1;
                    }
                }

                if(ans == heaters.length-1){
                    x = houses[i] - heaters[ans];
                }else{
                    x = Math.min(heaters[ans+1]-houses[i],houses[i] - heaters[ans]);
                }
            }
            ret = Math.max(x,ret);
        }
        return ret;
    }
}

面试题 16.06. 最小差

和475. 供暖器一样的解法,只是这个地方求的最小值,然后要注意数据的大小,要用long型来存储,否则会产生溢出,结果就不对了

class Solution {
    public int smallestDifference(int[] houses, int[] heaters) {
       Arrays.sort(houses);
        Arrays.sort(heaters);
        long x;
        int i;
        int ans;
        long ret=1000000000L;
        ret*=ret;
        for(i =0;i<houses.length;++i){
            if(houses[i] < heaters[0]){
                x = (long)heaters[0]-(long)houses[i];
            }else if(houses[i] > heaters[heaters.length-1]){
                x = (long)houses[i] - (long)heaters[heaters.length-1];
            }else {
                ans = -1;
                int l = 0,r=heaters.length -1;
                while(l <= r){
                    int mid = (l+r)>>1;
                    if(heaters[mid] <= houses[i]){
                        l = mid+1;
                        ans = mid;
                    }else{
                        r = mid -1;
                    }
                }
                if(ans == heaters.length-1){
                    x = (long)houses[i] - (long)heaters[ans];
                }else{
                    x = Math.min((long)heaters[ans+1]-(long)houses[i],(long)houses[i] - (long)heaters[ans]);
                }
            }
            ret = Math.min(x,ret);
        }
        return (int)ret;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值