class Solution {
solve(nums, k) {
const map = new Map();
for (let i = 0; i < nums.length; i++)
map.set(k - nums[i], i);
for (let i = 0; i < nums.length; i++)
if (map.has(nums[i]) && map.get(nums[i]) !== i)
return true;
return false;
}
}
Two pointer
class Solution {
solve(num) {
const numStr = num.toString();
let left = 0;
let right = numStr.length - 1;
while (left < numStr.length && right >= 0 && left <= right) {
if (numStr[left] != numStr[right]) return false;
left++;
right--;
}
return true;
}
}
369 게임
class Solution {
solve(n) {
const list = new Array(n);
for (let i = 1; i <= n; i++) {
if (i % 3 == 0) {
list[i - 1] = "clap";
continue;
}
const curNum = i.toString();
let flag = false;
for (let j = 0; j < curNum.length; j++) {
if (curNum[j] === "3" || curNum[j] === "6" || curNum[j] === "9") {
list[i - 1] = "clap";
flag = true;
break;
}
}
if (flag) continue;
list[i - 1] = i.toString();
}
return list;
}
}
연속되는 중복 문자의 개수를 세서 문자의 개수와 문자를 붙여 줄여서 표현하는 문제다.
class Solution {
solve(s) {
const result = [];
let left = 0, right = 0, count = 0;
while (left <= right && left < s.length && right < s.length) {
if (left === right) {
count = 1;
right++;
} else {
if (s[left] === s[right]) {
count++;
right++;
} else {
result.push(count);
result.push(s[left]);
left = right;
}
}
if (right === s.length) {
result.push(count);
result.push(s[right - 1]);
}
}
return result.join('');
}
}
class Solution {
solve(s) {
const result = [];
let count = 1;
for (let i = 0; i < s.length; i++) {
if (s[i] === s[i + 1]) {
count++;
continue;
}
result.push(count, s[i]);
count = 1;
}
return result.join('');
}
}
class Solution {
solve(nums) {
if (nums.length === 1) return true;
if (nums[0] === nums[1]) return false;
let up = nums[0] < nums[1] ? true : false;
for (let i = 1; i < nums.length - 1; i++) {
if (up && nums[i] >= nums[i + 1]) return false;
if (!up && nums[i] <= nums[i + 1]) return false;
}
return true;
}
}