首页 > 9.14 58同城投票加3AC Java
头像
我的过去常常追赶着我
编辑于 2020-09-14 22:14
+ 关注

9.14 58同城投票加3AC Java 投票

写了40分钟就写完了…感觉58也不收人了…
1.疫情聚集地
import java.util.Scanner;

public class tongcheng1 {
    static int count = 0;
    static int [][] array;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        array = new int [n][m];
        for(int i = 0;i<n;i++){
            for(int j = 0;j<m;j++){
                array[i][j] = sc.nextInt();
                if(array[i][j]==1){
                    count++;
                }
            }
        }
        int res = 0;
        while(count>0){
            for(int i = 0;i<n;i++){
                for(int j = 0;j<m;j++){
                    if(array[i][j]==1){
                        dfs(i,j);
                        res++;
                    }
                }
            }
        }
        System.out.println(res);
    }

    public static void dfs(int x,int y){
        if(x<0||x>=array.length||y<0||y>=array[0].length||array[x][y]!=1){
            return;
        }
        count--;
        array[x][y] = 0;
        dfs(x-1,y);
        dfs(x+1,y);
        dfs(x,y-1);
        dfs(x,y+1);
    }
}
2. 层次遍历
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class tongcheng2 {

     public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
      }

    public ArrayList<ArrayList<Integer>> printNode (TreeNode node) {

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(node);
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        while (!queue.isEmpty()){
            ArrayList<Integer> temp = new ArrayList<>();
            int len = queue.size();
            for(int i = 0;i<len;i++){
                TreeNode no = queue.poll();
                temp.add(no.val);
                if(no.left!=null){
                    queue.offer(no.left);
                }if(no.right!=null){
                    queue.offer(no.right);
                }
            }
            res.add(temp);
        }
        return res;
    }

}
3.最小没出现过的正整数
import java.util.HashSet;
import java.util.Set;

public class tongcheng3 {
    public static void main(String[] args) {
        int [] array = new int[]{1,-1,2,5,4};
        System.out.println(firstMissingPositive(array));
    }
    public static int firstMissingPositive (int[] nums) {
        Set<Integer> set =new HashSet<>();
        for(int i = 0;i<nums.length;i++){
            if(nums[i]>0){
                set.add(nums[i]);
            }
        }
        int res = 0;
        for(int i = 1;i<Integer.MAX_VALUE;i++){
            if(!set.contains(i)){
                res = i;
                break;
            }
        }
        return res;
    }
}





全部评论

(2) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

热门推荐