Skip to content

Instantly share code, notes, and snippets.

@chrismcfee
Created July 22, 2018 23:12
Show Gist options
  • Save chrismcfee/00c203aabcec2c2be9b84b73e4988a13 to your computer and use it in GitHub Desktop.
Save chrismcfee/00c203aabcec2c2be9b84b73e4988a13 to your computer and use it in GitHub Desktop.
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
std::vector<int> sorted;
//bool containsDuplicated=false;
for (int i=0; i<nums.size(); i++)
{
sorted.push_back(nums[i]);
}
sort(sorted.begin(), sorted.end());
if (sorted.size() == 1){
return false;
}
for (int i = 0; i < sorted.size(); i++){
cout << "is " << sorted[i] << "sad";
}
for (int i = 0; i < nums.size(); i++){
if (sorted[i] == sorted[i+1]){
//containsDuplicated = true;
return true;
}
}
//containsDuplicated=false;
return false;
}
};
void trimLeftTrailingSpaces(string &input) {
input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) {
return !isspace(ch);
}));
}
void trimRightTrailingSpaces(string &input) {
input.erase(find_if(input.rbegin(), input.rend(), [](int ch) {
return !isspace(ch);
}).base(), input.end());
}
vector<int> stringToIntegerVector(string input) {
vector<int> output;
trimLeftTrailingSpaces(input);
trimRightTrailingSpaces(input);
input = input.substr(1, input.length() - 2);
stringstream ss;
ss.str(input);
string item;
char delim = ',';
while (getline(ss, item, delim)) {
output.push_back(stoi(item));
}
return output;
}
string boolToString(bool input) {
return input ? "True" : "False";
}
int main() {
string line;
while (getline(cin, line)) {
vector<int> nums = stringToIntegerVector(line);
bool ret = Solution().containsDuplicate(nums);
string out = boolToString(ret);
cout << out << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment