58同城9.12笔试

1.

题目描述:

给定一个字符串,字符串是有序的整数集合,逗号相连,移除相同的数字,使每个数字出现一次,输出最终的数字个数

样例:

输入:

0,0,1,1,1,2,2,3,3,4

输出:

5

#include
#include
using namespace std;
void func(vector& array) {
	vectorans;
	int temp = 0;
	ans.push_back(array[0]);
	for (int i = 1; i < array.size(); i++) {
		if (array[i] != ans[ans.size() - 1]) {
			ans.push_back(array[i]);
		}
		else {
			continue;
		}
	}
	cout << ans.size() << endl;
}
//空间复杂度o(1)的方法 没提交过
//void func(vector& array) {
//	vector::iterator iter = array.begin();
//	for (++iter; iter < array.end();) {
//		if (*iter == * (iter - 1)) {
//			iter = array.erase(iter);
//			//指向删除位置的下一个元素,所以无需自加
//		}
//		else {
//			iter++;
//		}
//	}
//	cout << array.size() << endl;
//}
int main() {
	vectorarray;
	int temp = 0;
	cin >> temp;
	array.push_back(temp);
	while (cin.get() != '\n') {
		cin >> temp;
		array.push_back(temp);
	}
	func(array);
	return 0;
}


 

2.leetcode135

#include
#include
#include
using namespace std;
void func(vector& ratings) {
	vectorltor(ratings.size(), 1);
	vectorrtol(ratings.size(), 1);
	//从左到右遍历
	for (int i = 1; i < ratings.size(); i++) {
		if (ratings[i] > ratings[i - 1]) {
			ltor[i] = ltor[i - 1] + 1;
		}
	}
	//从右到左遍历
	for (int i = ratings.size() - 2; i >= 0; i--) {
		if (ratings[i] > ratings[i + 1]) {
			rtol[i] = rtol[i + 1] + 1;
		}
	}
	//取两个数组较大值
	int sum = 0;
	for (int i = 0; i < ratings.size(); i++) {
		ltor[i] = max(ltor[i], rtol[i]);
		sum += ltor[i];
	}
	cout << sum << endl;
}
int main() {
	int N = 0;
	cin >> N;
	vectorarray;
	int temp = 0;
	for (int i = 0; i < N; i++) {
		cin >> temp;
		array.push_back(temp);
	}
	func(array);

	return 0;
}

 

你可能感兴趣的:(笔试)