代码随想录四刷day7

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


如果想把这道题目做到极致,就不要只用额外的辅助空间了! (不过使用Java刷题的录友,一定要使用辅助空间,因为Java里的string不能修改)

一、力扣18. 四数之和

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<>();
        int n = nums.length;
        if(n < 4){
            return res;
        }
        Arrays.sort(nums);
        for(int i = 0; i < n-3; i ++){
            if(nums[i] > 0 && target < 0){
                continue;
            }
            if(i > 0 && nums[i] == nums[i-1]){
                continue;
            }
            for(int j = i+1; j < n-2; j ++){
                if(j > i+1 && nums[j] == nums[j-1]){
                    continue;
                }
                int left = j+1, right = n-1;
                while(left < right){
                    if(left > j+1 && nums[left] == nums[left-1]){
                        left ++;
                        continue;
                    }
                    if(right < n-1 && nums[right] == nums[right+1]){
                        right --;
                        continue;
                    }
                    int temp = nums[i] + nums[j] + nums[left] + nums[right];
                    if(temp == target){
                        res.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
                        left ++;
                        right --;
                    }else if(temp > target){
                        right --;
                    }else{
                        left ++;
                    }
                }
            }
        }
        return res;
    }
}

二、力扣344. 反转字符串

class Solution {
    public void reverseString(char[] s) {
        for(int i = 0, j = s.length-1; i < j; i ++, j --){
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
        }
    }
}

三、力扣541. 反转字符串 II

class Solution {
    public String reverseStr(String s, int k) {
        char[] ch = s.toCharArray();
        for(int i = 0; i < ch.length; i += 2*k){
            if(i+k-1 <= ch.length-1){
                ch = fun(ch,i,i+k-1);
            }else{
                ch = fun(ch,i,ch.length-1);
            }
        }
        return new String(ch);
    }
    public char[] fun(char[] s, int left, int right){
        while(left < right){
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left ++;
            right --;
        }
        return s;
    }
}

四、力扣54. 替换数字(第八期模拟笔试)

import java.util.*;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        char[] ch = s.toCharArray();
        int count = 0;
        for(char c : ch){
            if (c < 'a' ){
                count ++;
            } 
        }
        char[] res = new char[ch.length + count*5];
        char[] temp = new char[]{'n','u','m','b','e','r'};
        for(int i = 0, j = 0; i < ch.length; i ++){
            if(ch[i] < 'a'){
                for(int k = 0; k < temp.length; k ++){
                    res[j++] = temp[k];
                }
            }else{
                res[j++] = ch[i];
            }
        }
        System.out.println(new String(res));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值