像素设定 题目来源: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 ;
}
} ;
主持人调度(二) 题目来源: 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;
}
} ;
一种消息接收并打印的结构设计 题目来源: 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 ;
}
字典树的实现 题目来源: 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 ;
}