LeetCode每日刷题Day2--- 701转换成小写字母、997有序数组的平方、985查询后的偶数和

709 转换成小写字母

GitHub 账户:LuvnJoae 欢迎关注! https://github.com/LuvnJoae
GitHub 代码链接:https://github.com/LuvnJoae/Java_leetcode

思路与结果

在这里插入图片描述

代码

思路1

package Day2_4_2.L709;


import java.util.Arrays;
import java.util.Scanner;


public class Main1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();

        System.out.println(tra(str));
    }

    public static String tra(String str){
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] <= 'Z' && chars[i] >= 'A'){
                chars[i] = (char)(chars[i] + 32);
            }
        }
        str = new String(chars);
        return str;

    }
}

思路2

package Day2_4_2.L709;

import java.util.Scanner;


public class Main2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();

        System.out.println(tra(str));
    }

    public static String tra(String str){
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
                chars[i] = ( chars[i] <= 'Z' && chars[i] >= 'A') ? (char)(chars[i] + 32) : chars[i];
        }
        str = new String(chars);
        return str;

    }
}

997有序数组的平方

思路与结果

在这里插入图片描述

代码

package Day2_4_2.L977;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Main1 {
    public static void main(String[] args) {
        int[] ints = new int[]{
             -4,-1,0,3,10
        };
        sort(ints);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);
        }
    }

    public static int[] sort(int[] ints){
        for (int i = 0; i < ints.length; i++) {
            ints[i] = ints[i] * ints[i];
        }
        Arrays.sort(ints);
        return ints;

    }
}

985 查询后的偶数和

思路与结果

在这里插入图片描述

代码

思路2

package Day2_4_2.L985;



public class Main1 {
    public static void main(String[] args) {
        int[] A = new int[]{
                1,2,3,4
        };
        int[][] queries = {{1,0},{-3,1},{-4,0},{2,3}};
        int[] n = sumEvenAfterQueries(A,queries);
        for (int i = 0; i < 4; i++) {
            System.out.println(n[i]);;
        }

    }

    public static int[] sumEvenAfterQueries(int[] A, int[][] queries) {
        int[] ansewer = new int[queries.length];
        int count = 0;
        int temp = 0;
        for (int i = 0; i < A.length; i++) {
            if (A[i] % 2 == 0){
                count += A[i];
            }
        }
        for (int i = 0; i < queries.length; i++) {
            if ( queries[i][0] % 2 == 0 ){
                if (A[queries[i][1]] % 2 == 0){
                    count += queries[i][0];

                    A[queries[i][1]] += queries[i][0];
                }else {
                    A[queries[i][1]] += queries[i][0];
                }
            }else {
                if (A[queries[i][1]] % 2 ==0 ){
                    temp = A[queries[i][1]];
                    count = count - temp;

                    A[queries[i][1]] += queries[i][0];
                }else {
                    count += queries[i][0] + A[queries[i][1]];

                    A[queries[i][1]] += queries[i][0];
                }
            }
            System.out.println(count);
            ansewer[i] = count;
        }

        return ansewer;

    }
}

思路3

package Day2_4_2.L985;

public class Main2 {
    public static void main(String[] args) {
        int[] A = new int[]{
                1,2,3,4
        };
        int[][] queries = {{1,0},{-3,1},{-4,0},{2,3}};
        int[] n = sumEvenAfterQueries(A,queries);
        for (int i = 0; i < 4; i++) {
            System.out.println(n[i]);;
        }
    }

    public static int[] sumEvenAfterQueries(int[] A, int[][] queries){
        int[] ansewer = new int[queries.length];
    int count = 0;

        for (int i = 0; i < A.length; i++) {
        if (A[i] % 2 == 0){
            count += A[i];
        }
    }

        for (int i = 0; i < queries.length; i++) {
        if (A[queries[i][1]] % 2 == 0){
            count -= A[queries[i][1]];
        }
        A[queries[i][1]] += queries[i][0];
        if (A[queries[i][1]] % 2 == 0){
            count += A[queries[i][1]];
        }
        ansewer[i] = count;
    }
        return ansewer;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值