개발이야기/PS - Problem Solving, 알고리즘
LeetCode 1. Two Sum C++ 풀이
준호씨
2018. 10. 25. 23:39
반응형
단순 방법 풀이
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> answer(2);
int right = nums[1];
unsigned long length = nums.size();
for (int i = 0; i < length; i++) {
int left = nums[i];
for (int j=i+1; j<length; j++) {
if (left + nums[j] == target) {
answer[0] = i;
answer[1] = j;
return answer;
}
}
}
}
};
효율을 좀 더 높일 필요 있음.
반응형