zoukankan      html  css  js  c++  java
  • 58同城笔试(09-14)

    1 求疫情聚集区域的个数

      其实就是求连续的1的区域个数,简单DFS

    int mapp[110][110];
    
    int dfs(int rows, int cols, int x, int y){
        if((x < 0) || (x >= cols) || (y < 0) || (y >= rows) || mapp[y][x] == 0)
            return 0;
        mapp[y][x] = 0;
        return dfs(rows, cols, x - 1, y) + dfs(rows, cols, x + 1, y) + dfs(rows, cols, x, y - 1) + dfs(rows, cols, x, y + 1) + 1;
    }
    
    int main(){
        
    	int m, n;
        scanf("%d %d", &m, &n);
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++)
                scanf("%d", &mapp[i][j]);
        }
        int count = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++)
                if(mapp[j][i] == 1)
                    if(dfs(m, n, i, j) >= 1)
                        count++;    
        }
        printf("%d
    ", count);
        return 0;
    
    }
    
    2 将二叉树进行二维打印

      剑指offer原题,按层打印二叉树。

    /*
    struct TreeNode {
        int val;
        struct TreeNode *left;
        struct TreeNode *right;
        TreeNode(int x) :
                val(x), left(NULL), right(NULL) {
        }
    };
    */
    class Solution {
    public:    
        	queue<TreeNode *> queueTreeNode;
            vector<vector<int> > Print(TreeNode* pRoot){
                vector<vector<int> > ans;
                vector<int> vecNew;
                if(pRoot == nullptr)
                    return ans;
                int nextLevel = 0;
                int toBePrinted = 1;
                queueTreeNode.push(pRoot);
                while(queueTreeNode.size() > 0){
                    TreeNode* pNode = queueTreeNode.front();
                    cout<<pNode->val<<" ";
                    vecNew.push_back(pNode->val);
                    queueTreeNode.pop();
                    if(pNode->left != nullptr){
                        queueTreeNode.push(pNode->left);
                        nextLevel++;
                    }
                    if(pNode->right != nullptr){
                        queueTreeNode.push(pNode->right);
                        nextLevel++;
                    }
                    toBePrinted--;
                    if(toBePrinted == 0){
                        ans.push_back(vecNew);
                        vecNew.clear();
                        cout<<endl;
                        toBePrinted = nextLevel;
                        nextLevel = 0;
                    }
                }
                return ans;
            }
        
    };
    
    3 求最小未出现的正整数

      巧用TreeSet,暴力求解。

    • TreeSet不重复的排序的集合
    public static int firstMissingPositive (int[] nums) {
    	// write code here
        Set<Integer> mySet = new TreeSet<Integer>();
        for(int i = 0; i < nums.length; i++)
        	mySet.add(num[i]);
        int ans = 1;
        // 循环到nums.length + 1以防出现1,2,3,4则返回5
        while(ans < nums.length + 1){
        	if(!mySet.contains(ans))
             	return ans;
            ans++;
        }
        return ans;
    }
    
  • 相关阅读:
    Eclipse的自动排版设置(format)
    Java中" "表示几个空格
    cookie和session详解
    IO流操作详解
    springmvc常用注解标签详解
    mavenWeb工程建立步骤
    数据导出为excel表格
    Springmvc jar包介绍
    【初级算法】5.只出现一次的数字
    【初级算法】4.存在重复
  • 原文地址:https://www.cnblogs.com/flyingrun/p/13669686.html
Copyright © 2011-2022 走看看