当前位置:网站首页>华为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;
}边栏推荐
- Sentinel规则持久化到Nacos
- 官方零基础入门 Jetpack Compose 的中文课程来啦!
- Mech 3002 explanation
- LeetCode 77. 组合
- No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
- ROS create workspace
- 利用NVIDIA GPU将Minecraft场景渲染成真实场景
- 队列(线性结构)
- 深入学习JVM底层(五):类加载机制
- Use of Arduino wire Library
猜你喜欢

官方零基础入门 Jetpack Compose 的中文课程来啦!

Lucene Basics

New version of dedecms collection and release plug-in tutorial tool

Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table

深入了解JUC并发(一)什么是JUC

Spark overview

Flutter hybrid development: develop a simple quick start framework | developers say · dtalk

广告业务Bug复盘总结

来自读者们的 I/O 观后感|有奖征集获奖名单

介绍两款代码自动生成器,帮助提升工作效率
随机推荐
CUDA中的存储空间修饰符
BGP报文详细解释
Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
CUDA中的函数执行空间说明符
【每日一题】—华为机试01
CUDA中的异步数据拷贝
广告业务Bug复盘总结
Sentinel规则持久化到Nacos
From design delivery to development, easy and efficient!
Summary of WLAN related knowledge points
标签属性disabled selected checked等布尔类型赋值不生效?
Hydration failed because the initial UI does not match what was rendered on the server. One of the reasons for the problem
队列(线性结构)
Detailed steps of JS foreground parsing of complex JSON data "case: I"
Frequently asked questions about jetpack compose and material you
Support new and old imperial CMS collection and warehousing tutorials
线性dp(拆分篇)
日志(常用的日志框架)
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
BGP 路由优选规则和通告原则