leetcode刷题记录Java

难度等级:简单
给你两个字符串 word1 和 word2 。请你从 word1 开始,通过交替添加字母来合并字符串。
如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。
返回 合并后的字符串 。
class Solution {
    public String mergeAlternately(String word1, String word2) {
        int len = Math.min(word1.length(),word2.length());
        StringBuffer str = new StringBuffer();
        for(int i = 0;i < len;i++){
            str.append(word1.charAt(i));
            str.append(word2.charAt(i));
        }
        str.append(word1.substring(len));
        str.append(word2.substring(len));
        return str.toString();
    }
}

难度:中等
给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。
题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在  32 位 整数范围内。
请不要使用除法,且在 O(n) 时间复杂度内完成此题。
class Solution {
    public int[] productExceptSelf(int[] nums) {
        int[] answer = new int[nums.length];
        int[] left = new int[nums.length];
        int[] right = new int[nums.length];
        //数组初始化为1
        left[0] = 1;
        right[nums.length-1] = 1;
        for(int i = 1;i < nums.length;i++){
            left[i] = nums[i-1] * left[i-1];
        }
        for(int i = nums.length - 2;i >= 0;i--){
            right[i] = nums[i+1] * right[i+1];
        }
        for(int j = 0; j < nums.length;j++){
            answer[j] = left[j]*right[j];
        }
        return answer;
    }
}

难度:中等
给你一个整数数组 nums ,判断这个数组中是否存在长度为 3 的递增子序列。
如果存在这样的三元组下标 (i, j, k) 且满足 i < j < k ,使得 nums[i] < nums[j] < nums[k] ,
返回 true ;否则,返回 false 。
80个测试用例,通过75个
class Solution {
    public boolean increasingTriplet(int[] nums) { 
        for(int i = 1;i < nums.length - 1;i++){
            int[] left = Arrays.copyOfRange(nums, 0, i);
            int[] right = Arrays.copyOfRange(nums, i + 1, nums.length);
            int leftMin = Arrays.stream(left).min().getAsInt();
            int rightMax = Arrays.stream(right).max().getAsInt();
            if(leftMin < nums[i]&&nums[i] < rightMax){
                return true;
            }
        }
        return false;
    }
}
测试用例全通过的方式(双向遍历)
class Solution {
    public boolean increasingTriplet(int[] nums) {
        int count = nums.length;
        if(count < 3){
            return false;
        }
        int[] left = new int[count];
        int[] right = new int[count];
        left[0] = nums[0];
        right[count - 1] = nums[count - 1];
        for(int i = 1;i < count - 1;i++){
            left[i] = Math.min(left[i - 1],nums[i]);
        }
        for(int i = count - 2;i > 1;i--){
            right[i] = Math.max(right[i + 1], nums[i]);
        }
        for(int i = 1;i < count - 1;i++){
            if(left[i - 1] < nums[i]&&nums[i] < right[i + 1]){
                return true;
            }
        }
        return false;
    }
}
贪心算法的解题方法
class Solution {
    public boolean increasingTriplet(int[] nums){
        int count = nums.length;
        if(count < 3){
            return false;
        }
        int first = nums[0];
        int second = Integer.MAX_VALUE;
        for(int i = 1;i < count;i++){
            int Nums = nums[i];
            if(Nums > second){
                return true;
            }else if(Nums > first){
                second = Nums;
            }
            else{
                first = Nums;
            }
        }
        return false;
    }
}

难度:中等
给你一个字符数组 chars ,请使用下述算法压缩:
从一个空字符串 s 开始。对于 chars 中的每组 连续重复字符 :
如果这一组长度为 1 ,则将字符追加到 s 中。否则,需要向 s 追加字符,后跟这一组的长度。
压缩后得到的字符串 s 不应该直接返回 ,需要转储到字符数组 chars 中。需要注意的是,
如果组长度为 10 或 10 以上,则在 chars 数组中会被拆分为多个字符。
请在 修改完输入数组后 ,返回该数组的新长度。
示例:
输入:chars = ["a","a","b","b","c","c","c"]
输出:返回 6 ,输入数组的前 6 个字符应该是:["a","2","b","2","c","3"]
解释:"aa" 被 "a2" 替代。"bb" 被 "b2" 替代。"ccc" 被 "c3" 替代。
比较繁琐的办法
public int compress(char[] chars) {
    String str = new String();
    char current = chars[0];
    int count = 1;
    if(chars.length == 1) {
        str = String.valueOf(current);
      }
    for(int i = 1;i < chars.length;i++) {
        if(chars[i] == current) {
            count++;
        }
        else {
            if(count > 1) {
                str = str + String.valueOf(current) + String.valueOf(count);
                current = chars[i];
                count = 1;
            }
            else {
                str = str + String.valueOf(current);
                current = chars[i];
                count = 1;
            }
            
        }
        if(i == chars.length - 1) {
            if(count > 1) {
                str = str + String.valueOf(current) + String.valueOf(count);
            }
            else {
                str = str + String.valueOf(current);
            }
        }
    }
    for(int i = 0;i < str.length();i++){
        chars[i] = str.charAt(i);
    }
    return str.length();
}
效率更高的写法
public int compress(char[] chars) {
    int n = chars.length;
    int write = 0;
    int left = 0;
    for(int i = 0;i < n;i++) {
        if(i == n - 1 || chars[i] != chars[i + 1]) {
            int count = i - left + 1;
            if(count == 1) {
                left = i + 1;
                write++; 
                if(i != n - 1){
                    chars[write] = chars[i+ 1];
                }
            }
            else if(count < 10) {
                chars[++write] = (char)(count + '0');
                left = i + 1;
                write++;
                if(i != n - 1){
                    chars[write] = chars[i+ 1];
                }
            }
            else if(count >= 10) {
                String str = String.valueOf(count);
                for(int j = 0;j < str.length();j++){
                    chars[++write] = str.charAt(j);
                }
                left = i + 1;
                write++;
                if(i != n - 1){
                    chars[write] = chars[i+ 1];
                } 
            }
        }
    }
    return write+1;
  }
posted @ 2023-07-15 11:00  稻草人拾荒  阅读(18)  评论(0)    收藏  举报