栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

微软笔试题

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微软笔试题

1. Write a function solution that, given an array A of N integers, returnsthe largest integer K>0 such that both values K and -K(the oppositenumber)exist in array A. If there is no such integer, the function should return 0.

Examples:

1. Given A=[3, 2, -2 5, -3], the function should return 3(both 3 and-3exist in array A)

2. Given A=[1, 1, 2, -1, 2, -1], the function should return 1(both 1 and-1exist in array A)

3. Given A=[1, 2, 3, -4], the function should return 0(there is no such K for which both values K and-k exist in array A)

Write an efficient algorithm for the following assumptions N is an integer within the range [1..100, 000),each element of array A is an integer within the range [-1,000,000000.100000000]

#include 
#include 
#include 
using namespace std;

int solution(vector arr) {
    unordered_set hash;
    int ans=0;
    for (auto a : arr) {
        if (hash.count(0-a)!=0) {
            ans=max(ans, max(a, 0-a));
        }
        hash.insert(a);
    }
    return ans;
}
int main()
{
	cout< 

2. A string is called balanced when every letter occurring in the string ,appears both in upper-and lowercase . For example , the string "Catattac" is balanced ( 'a', 'c', 't' occur in both cases ) , but "Madam" is not ( 'd' and 'a' appear only in lowercase ) . Note that the number of occurrences does not matter.

Write a function :

int solution ( string &S) ;

that , given a string S of length N , returns the length of the shortest balanced fragment of S. If S does not contain any balanced fragments ,the function should return -1.

Examples :

1 . Given S = "azABaabza" the function should return 5 . The shortest balanced fragment of S is "Abaab".

2 . Given S = "TacoCat" the function should return-1 . There is no balanced fragment

3 . Given S = "AcZCbaBz" the function should return 8 . The shortestbalanced fragment is the whole string

4 . Given S = abcdefghijklmnopqrstuvwxyz" , the function should return -1

Assume that:

N is an integer within the range [1...200]

string S consists only of letters ( a-z and / or A-Z )

In your solution , focus on correctness . The performance of your solution will not be the focus of the assessment.

#include 
#include 
#include 
using namespace std;
bool fun(string str, int l, int r) {
	unordered_set hash;
	for (int i=l; i<=r; i++) {
		hash.insert(str[i]);
	}
	for (auto ch : hash) {
		if (ch>='a' && ch<='z') {
			if (hash.count('A'+(ch-'a'))==0)	return false;
		}else {
			if (hash.count('a'+(ch-'A'))==0)	return false;
		}
	}
	return true;
}
int solution(string str) {
	int ans=0;
	for (int k=2; k<=str.size(); k++) {
		for (int i=0; i<=str.size()-k; i++) {
			if (fun(str, i, i+k-1)){
				ans=max(ans, k);
			}
		}
	}
	return ans==0 ? -1 : ans;
}

int main()
{
	cout< 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/512382.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号