虹软笔试题

  1. 像素设定
    题目来源:https://www.nowcoder.com/questionTerminal/31ade926268441878d423029c54f5171
    思路:首先解释一下题目的意思,我刚开始没搞懂,怀疑自己是不是中国人了hh,题目给的全是0的二维数组,x和y代表需要让填充颜色的像素点,在x-y这个范围的像素点变为1(即数组元素的对应二进制位变为1),这个就可以用位运算符,|来进行操作了,对吧,t是行数,k是在行中的位置。
class Render {
public:
    vector<int> renderPixel(vector<int> screen, int x, int y) {
 	for(int i = x ; i <= y ; i ++)
    {
           int k = i % 8 ;
           int t = i / 8 ;
           screen[t] = screen[t] | (1<< k);
    }
    return screen ;
    }
};
  1. 主持人调度(二)
    题目来源:
    https://www.nowcoder.com/practice/4edf6e6d01554870a12f218c94e8a299?tpId=196&tqId=37562&ru=/exam/oj
class Solution {
public:
    int minmumNumberOfHost(int n, vector<vector<int> >& startEnd) {
        vector<int> start;
        vector<int> end;
        //分别得到活动起始时间
        for(int i = 0; i < n; i++){
            start.push_back(startEnd[i][0]);
            end.push_back(startEnd[i][1]);
        }
        //分别对开始和结束时间排序
        sort(start.begin(), start.end());
        sort(end.begin(), end.end());
        int res = 0;
        int j = 0;
        for(int i = 0; i < n; i++){
            //新开始的节目大于上一轮结束的时间,主持人不变
            if(start[i] >= end[j]) 
                j++;  
            else
                //主持人增加
                res++;  
        }
        return res;
    }
};

  1. 一种消息接收并打印的结构设计
    题目来源:
    https://www.nowcoder.com/practice/155238870f834406ba6971dec4825ab6?tpId=101&tqId=33147&ru=/exam/oj
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    int a[n], Min=1, k=1;
    priority_queue<int, vector<int>, greater<int>> q;
    for(int i=0;i<n;i++){
        cin>>a[i];
        q.push(a[i]);
        if(a[i]==Min)
            while(q.top()==Min){
                cout<<k++<<" "<<a[i]<<endl;
                Min++;
                q.pop();
            }
    }
    return 0;
}
  1. 字典树的实现
    题目来源:
    https://www.nowcoder.com/practice/7f8a8553ddbf4eaab749ec988726702b?tpId=101&tqId=33208&ru=/exam/oj
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>

typedef struct tn {
    int pass;
    int end;
    struct tn **children;  // 存放子节点的指针
} node;

node *new_node() {
    node *new_node = malloc(sizeof(node));
    new_node->pass = 0;
    new_node->end = 0;
    new_node->children = (node **) calloc(26, sizeof(node *));
    return new_node;
}

void destroy_node(node *node) {
    free(node->children);
    free(node);
}

void destroy_whole_path(node *node) {
    for (int i = 0; i < 26; i++) {
        if (node->children[i] != NULL) {
            destroy_node(node->children[i]);
        }
    }
    destroy_node(node);
}

typedef node *trie;

trie new_trie() {
    return new_node();
}

void insert(trie trie, char *word) {
    if (word == NULL || strlen(word) == 0) {
        return;
    }
    int len = (int) strlen(word);
    node *cur = trie;
    cur->pass++;
    int path;
    for (int i = 0; i < len; i++) {
        path = word[i] - 'a';
        if (cur->children[path] == NULL) {
            cur->children[path] = new_node();
        }
        cur = cur->children[path];
        cur->pass++;
    }
    cur->end++;
}

bool search(trie trie, char *word) {
    if (word == NULL || strlen(word) == 0) {
        return false;
    }
    node *cur = trie;
    int path;
    for (int i = 0; i < strlen(word); i++) {
        path = word[i] - 'a';
        if (cur->children[path] == NULL) {
            return false;
        }
        cur = cur->children[path];
    }
    return cur->end != 0;
}

void delete(trie trie, char *word) {
    if (!search(trie, word)) {
        return;
    }
    node *cur = trie, *next;
    cur->pass--;
    int path;
    for (int i = 0; i < strlen(word); i++) {
        path = word[i] - 'a';
        if (--cur->children[path]->pass == 0) {
            next = cur->children[path];
            cur->children[path] = NULL;
            destroy_whole_path(next);
            return;
        }
        cur = cur->children[path];
    }
    cur->end--;
}

int prefixNumber(trie trie, char *word) {
    if (word == NULL || strlen(word) == 0) {
        return false;
    }
    node *cur = trie;
    int path;
    for (int i = 0; i < strlen(word); i++) {
        path = word[i] - 'a';
        if (cur->children[path] == NULL) {
            return 0;
        }
        cur = cur->children[path];
    }
    return cur->pass;
}

#define MAXLEN 21

int main(void) {
    trie trie = new_trie();
    int n, op;
    char str[MAXLEN];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%s", &op, str);
        if (op == 1) insert(trie, str);
        else if (op == 2) delete(trie, str);
        else if (op == 3) printf("%s\n", search(trie, str) ? "YES" : "NO");
        else if (op == 4) printf("%d\n", prefixNumber(trie, str));
    }
    destroy_node(trie);
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值