华为od:机试两道题

1.输入一些单词 再输入一个前缀 输出所有前缀开始的单词 不包括相同的单词 按字典序排序 100分
华为od:机试两道题_第1张图片

class Solution {
public:
	vector<string> prefixCount(vector<string>& words, string pref) {
		int len = pref.size();
		sort(words.begin(), words.end());
		vector<string>tmp;
		
		for ( int i = 0; i < words.size(); i++)//
		{
			if (words[i].substr(0, len) == pref)
			{
			
				if (i >= 1&&words[i] != words[i-1])//i>=1要放在前面,以防访问元素(i-1=-1)时发生越界。~~~~!!!!!!!
				{
					tmp.push_back(words[i]);
				}
				if (i == 0)
				{
					tmp.push_back(words[i]);
				}
				
			}
		}
		return tmp;
	}
};
int main()
{
	Solution s;
	vector <string>s1 = { "ace","acc","dce","ace","bed","acce","sn","acce"};
	vector<string>s2 = s.prefixCount(s1, "ac");
	for (auto& x : s2)
	{
		cout << x << endl;
	}
	return 0;
}

华为od:机试两道题_第2张图片

using namespace std;
class Solution 
{
public:
	int Min_Sum(string &s1)
	{
		int sum = 0,count=0;
		int tmp;
		string st;
		for (int i=0;i<s1.size() ; i++)
		{
			if (isdigit(s1[i]))
			{
				sum += (s1[i]-'0');
			}
			if (s1[i] == '-')//也可以把“-”号最先放进容器中。这是另一个方法。
			{
				i++;
				//st += '-';
				while (isdigit(s1[i]))
				{
					st += s1[i];
					count++;
					i++;
				}
				if (!st.empty())
				{
					int digit = stoi(st);
					st.clear();
				
				sum -= digit;
				}
			}
		}
		return sum;
	}

};
int main()
{
	Solution s;
	string s1 = {"ab0c12ss-123b-11s1s+dd-1g1g"};
	int sum = s.Min_Sum(s1);
	cout << sum << endl;
	return 0;
}

你可能感兴趣的:(leecode,刷T,算法,c++,leetcode)