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;
}
}