第一题:统计某个单词数量
题目:给定一行输入,包括空格,字符,符号等,求有多少个Good,符号顺序不可改变,区分大小写
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
int g = 0;
int o = 0;
int d = 0;
int res = 0;
char[] c = s.toCharArray();
for(int i=0;i<c.length;i++){
if(c[i]=='G') g++;
if(g>=1 && c[i]=='o') o++;
if(g>=1 && o>=2 && c[i]=='d') d++;
if(g>=1 && o>=2 && d>=1){
res++;
g--;
o = o-2;
d=0;
}
}
System.out.println(res);
}
}
}
//GoodoodGGooodddGGoooddGoood\n
//Good Good Good Good Good Good
——AC
第二题:矩阵中的最长递增路径
——LeetCode329
给定一个整数矩阵,找出最长递增路径的长度。
对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int N = sc.nextInt();
int M = sc.nextInt();
int[][] nums = new int[N][M];
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
nums[i][j] = sc.nextInt();
}
}
System.out.println(longestPath(nums));
}
}
public static int longestPath(int[][] nums){
if(nums == null || nums.length == 0 || nums[0].length == 0){
return 0;
}
int max = 0;
int row = nums.length;
int cols = nums[0].length;
int[][] dp = new int[row][cols];
for(int i=0;i<row;i++){
for(int j=0;j<cols;j++){
max = Math.max(max,helper(nums,Integer.MIN_VALUE,dp,i,j));
}
}
return max;
}
public static int helper(int[][] nums,int pre,int[][] dp,int i,int j){
if(i<0 || j<0 || i>=nums.length || j>=nums[0].length || nums[i][j]<=pre){
return 0;
}
if(dp[i][j] != 0){
return dp[i][j];
}
int max = 0;
max = Math.max(max,helper(nums,nums[i][j],dp,i-1,j));
max = Math.max(max,helper(nums,nums[i][j],dp,i+1,j));
max = Math.max(max,helper(nums,nums[i][j],dp,i,j-1));
max = Math.max(max,helper(nums,nums[i][j],dp,i,j+1));
dp[i][j] = max + 1;
return dp[i][j];
}
}
——AC
第三题
题目:给定一个区间集合,找到需要移除区间的最小数值,使剩余区间互不重叠。
import java.util.*;
public class Solution {
/**
* @param intervals int整型二维数组
* @return int整型
*/
public int eraseOverlapIntervals (int[][] intervals) {
// write code here
int N = intervals.length;
int M = intervals[0].length;
int res = 0;
for(int i=0;i<N-1;i++){
if(intervals[i][1] > intervals[i+1][0]){
res++;
}
}
return res;
}
}
——这就AC了?