大家好,我是LvZi,今天带来
笔试狂刷--Day6
一.在字符串中找出连续最⻓的数字串
1.题目链接
在字符串中找出连续最⻓的数字串
2.题目分析
使用双指针模拟
3.代码实现
java">import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main
{public static void main(String[] args){Scanner in = new Scanner(System.in);String ss = in .nextLine();char[] s = ss.toCharArray();int n = ss.length();String ret = "";for(int l = 0; l < n; l++){if(s[l] >= '0' && s[l] <= '9'){int cur = l;while(cur < n && s[cur] >= '0' && s[cur] <= '9') cur++;if(cur - l + 1 > ret.length()) ret = ss.substring(l, cur);if(cur == n - 1) break;l = cur - 1;}}System.out.println(ret);}
}
二.岛屿数量
1.题目链接
岛屿数量
2.题目分析
经典的bfs问题
3.代码实现
java">import java.util.*;
public class Solution
{/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** 判断岛屿数量* @param grid char字符型二维数组* @return int整型*/int[] dx = {1, -1, 0, 0};int[] dy = {0, 0, 1, -1};int m, n;boolean[][] vis;public int solve(char[][] grid){// write code herem = grid.length;n = grid[0].length;vis = new boolean[m][n];int ret = 0;for(int i = 0; i < m; i++){for(int j = 0; j < n; j++){if(grid[i][j] == '1' && !vis[i][j]){vis[i][j] = true;ret += bfs(grid, i, j);}}}return ret;}private int bfs(char[][] arr, int i, int j){int area = 0;Queue < int[] > q = new LinkedList < > ();q.add(new int[]{i, j});area += 1;while(!q.isEmpty()){int[] t = q.poll();int a = t[0], b = t[1];for(int k = 0; k < 4; k++){int x = a + dx[k], y = b + dy[k];if(x >= 0 && x < m && y >= 0 && y < n && arr[x][y] == '1' && !vis[x][y]){q.add(new int[]{x, y});vis[x][y] = true;}}}return area;}
}
三.拼三⻆
1.题目链接
拼三⻆
2.题目分析
根据三角形的性质暴力模拟
3.代码实现
java">import java.util.*;
public class Main
{public static void main(String[] args){Scanner in = new Scanner(System.in);int t = in .nextInt();int[] arr = new int[6];while(t-- != 0){for(int i = 0; i < 6; i++) arr[i] = in .nextInt();Arrays.sort(arr);if(arr[0] + arr[1] > arr[2] && arr[3] + arr[4] > arr[5] || arr[0] + arr[2] > arr[3] && arr[1] + arr[4] > arr[5] || arr[0] + arr[3] > arr[4] && arr[1] + arr[2] > arr[5] || arr[0] + arr[4] > arr[5] && arr[1] + arr[2] > arr[3]){System.out.println("Yes");}else{System.out.println("No");}}}
}