当前位置:网站首页>华为MindSpore开源实习机试题
华为MindSpore开源实习机试题
2022-07-02 06:19:00 【沙子是沙子】
浅浅记录下华为MindSpore开源实习机试题目,三道题。
整体来看不算太难。难点是输入输出别扭,不知道是有意为之还是其他原因。
第一道题:请参考此题剑指 Offer 39. 数组中出现次数超过一半的数字
数组中出现超过次数超过一半的数字(哈希)


题解:
//
// Created by zhanghao on 2022/6/2.
//
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
int countNum(vector<int> & nums){
int ans = 0;
int n = nums.size();
unordered_map<int,int> mymap(n);
for(int i = 0 ; i < n; i++){
mymap[nums[i]]++;
}
for(int i = 0 ; i < n;i++){
if(mymap[nums[i]] > n/2){
ans = nums[i];
break;
}
}
return ans;
}
int main(){
vector<int> nums;
int num;
string s;
cin >> s;
for(auto i : s){
if(i >= '0' && i <= '9'){
nums.push_back((i-'0'));
}
}
int ans = countNum(nums);
cout<<ans<<endl;
return 0;
}第二道题:请参考此题14. 最长公共前缀
题解:
//
// Created by zhanghao on 2022/6/2.
//
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string longest(const string& str1, const string& str2) {
int length = min(str1.size(), str2.size());
int index = 0;
while (index < length && str1[index] == str2[index]) {
++index;
}
return str1.substr(0, index);
}
string longestCommonPrefix(vector<string>& strs) {
if (!strs.size()) {
return "";
}
string prefix = strs[0];
int count = strs.size();
for (int i = 1; i < count; ++i) {
prefix = longest(prefix, strs[i]);
if (!prefix.size()) {
break;
}
}
return prefix;
}
int main()
{
// please define the C++ input here. For example: int a,b; cin>>a>>b;;
// please finish the function body here.
// please define the C++ output here. For example:cout<<____<<endl;
string s;
vector<string> arr;
cin >> s;
int n = s.size();
for(int i = 0 ; i < n;i++){
if(s[i] == '"'){
int left = ++i;
int n = 0;
while(s[i]!='"'){
n++;
i++;
}
string temp = s.substr(left,n);
arr.push_back(temp);
}
}
string ans = longestCommonPrefix(arr);
cout<<ans<<endl;
return 0;
}
第三题:请参考此题96. 不同的二叉搜索树

题解:
#include <iostream>
#include <vector>
using namespace std;
int numTree(int n){
vector<int> dp(n+1);
dp[0] = 1;
for(int i = 1;i <= n;i++){
for(int j = 0; j <= i-1;j++){
dp[i] += dp[j]*dp[(i-1)-j];
}
}
return dp[n];
}
int main(){
int n;
cin >> n;
int ans = numTree(n);
cout<<ans<<endl;
return 0;
}边栏推荐
- Generic classes and parameterized classes of SystemVerilog
- Little bear sect manual query and ADC in-depth study
- BGP 路由優選規則和通告原則
- 深入学习JVM底层(三):垃圾回收器与内存分配策略
- Golang -- map capacity expansion mechanism (including source code)
- Find the highest value of the current element Z-index of the page
- Decryption skills of encrypted compressed files
- Google Play Academy 组队 PK 赛,正式开赛!
- Does the assignment of Boolean types such as tag attribute disabled selected checked not take effect?
- 利用传统方法(N-gram,HMM等)、神经网络方法(CNN,LSTM等)和预训练方法(Bert等)的中文分词任务实现
猜你喜欢

Community theory | kotlin flow's principle and design philosophy

VLAN experiment of switching technology

线性dp(拆分篇)

Network related knowledge (Hardware Engineer)

Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish

Sentinel 阿里开源流量防护组件

LeetCode 90. Subset II

Eco express micro engine system has supported one click deployment to cloud hosting

Detailed explanation of BGP message

CUDA中的Warp Shuffle
随机推荐
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
The official zero foundation introduction jetpack compose Chinese course is coming!
【张三学C语言之】—深入理解数据存储
IPv6 experiment and summary
浅谈三点建议为所有已经毕业和终将毕业的同学
Data science [viii]: SVD (I)
Let every developer use machine learning technology
Web components series (VIII) -- custom component style settings
Is there a really free applet?
LeetCode 283. Move zero
Step by step | help you easily submit Google play data security form
Database learning summary 5
TensorRT的数据格式定义详解
深入了解JUC并发(二)并发理论
Current situation analysis of Devops and noops
Arduino Wire 库使用
实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了
一口气说出 6 种实现延时消息的方案
Golang -- map capacity expansion mechanism (including source code)
Talking about MySQL database