벌써 4일이라고? 지려~
오늘은 4일차니까 4문제 ㄱㄱ
안되면말고 ㅎ
가보자고~
1. 중복숫자 한개로 만들고 빈칸 얼마나 만들었는지 갯수랑 보여주기~
https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
Remove Duplicates from Sorted Array - LeetCode
Can you solve this real interview question? Remove Duplicates from Sorted Array - Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place [https://en.wikipedia.org/wiki/In-place_algorithm] such that each unique element ap
leetcode.com
ㅋㅋ 항상 문제를보면 풀수있을것같은 근거없는 자신감이 10 분뒤 나를 더 비참하게 만듬 ㅋㅋ
내생각대로 풀었는데 테스트케이스에서 내가 예상하지 못하는 결과가 계속나와서 이해가 안간다
nums =[1,1,2]일경우 return nums.length를 해줬는데 아웃풋이 [1,1,2]가 나온다던가
이해가 안간다.. 걍 return 8 해봤는데 또같은 아웃풋이 나옴ㅋㅋㅋㅋㅋㅋ개어이없음....
다른사람들이 푼 솔루션 보고 비슷해서 내가 풀면 아웃풋 이상하게 나오고 솔루션하고 똑같이 풀면 패스 되고....
뭐가 잘못된건지 이해가 안간다 나만 이해 안가는듯 다들 푸는것보면 ㅎㅎ
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
let a = 0; //변수 하나 선언해놓고
for(let i=1; i< nums.length; i++){//두번째 숫자부터 포문돌리면서
if(nums[i] !== nums[a]){//비교하는거야
a++// 두숫자가 같지 않으면 a변수 숫자 1플러스해주고
nums[a] = nums[i] //i번째 숫자를 1플러스한a번째 에 넣어버려~
}
}
return a + 1;
};
그리고 중복제거거된 요소 갯수를 리턴
걍 return 2라고 하면 혼자서 아웃풋을 바꿔버리는지는 귀신도 모름
아무리 답이 정해져있어도 말도없이 아웃풋 바꿔서 돌리는건 에바 아님? 같은 생각은 허접인 나니까 하는거겠지
뭐 일단 이해했고 담에 나오면 풀수 있을것같아 넘어간다만
기분이 찝찝 함 ㅎ...^^
2. 글자 있는지 확인하기
https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/
Find the Index of the First Occurrence in a String - LeetCode
Can you solve this real interview question? Find the Index of the First Occurrence in a String - Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: I
leetcode.com
이번엔 입방정 안떨고 풀이 시작
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
var word = []
for(var i = 0; i <= haystack.length; i++){
for(var j = 0; j <= needle.length; j++){
if(haystack[i] == needle[j]){
i++
word.push(needle[j])
if(needle.length == word.length){
return i-needle.length
}
}else{
word = []
}
}
}
};
풀이실패 틀렸음 머리에서 이렇게 저렇게꼬여서
단어가 포함이 안되있는것 -1 처리를 못하게 됨
그리고 이렇게 포문안에서 i값을 수동으로 수정하는것은 오류를 만들어내는 것이라고한다.. ㅎㅎ
var strStr = function(haystack, needle) {
if (needle === "") return 0;
for (var i = 0; i <= haystack.length - needle.length; i++) {
var match = true;
for (var j = 0; j < needle.length; j++) {
if (haystack[i + j] !== needle[j]) {
match = false;
break;
}
}
if (match) return i;
}
return -1;
};
다른사람 풀이 개깔끔하게짠다 ㄹㅇ...
3. 배열의 끝에 1더해서 자리수에 맞춰 배열로 반환하기
https://leetcode.com/problems/plus-one/description/
Plus One - LeetCode
Can you solve this real interview question? Plus One - You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-
leetcode.com
/**
* @param {number[]} digits
* @return {number[]}
*/
var plusOne = function(digits) {
digits[digits.length-1] += 1
for(var i = digits.length; i >= 0; i--){
if(digits[i] == 9){
digits[i] = 0
digits[i-1] += 1
}
}
return digits
};
이렇게 풀었는데 case 3의경우
[9] 가 들어가면 [1,0]이 나와야하는데 [10]이 나오게 됨
문제가 있다
unshift(1)을 써서 앞자리 1을 넣어줬는데 이것도 롱엔서
이경우를 해결못함 ㅋㅋ
var plusOne = function(digits) {
for(var i = digits.length - 1; i >= 0; i--){
if(digits[i] < 9){
digits[i]++;
return digits;
}
digits[i] = 0;
}
digits.unshift(1);
return digits;
};
걍앞에 9가나오면 다 0처리하고 언시프트로 1앞에 넣어주면 됨 ㅋㅋㅋㅋ
중간이면 걍 그앞자리 1더해주고 그앞자리가 9면 또 0처리하고 그앞자리 1넣어주고 이거 돌리는거다...
벌써 도서관 문이 닫을시간이다 4문제풀고싶었는데 4번째 문제는 집가서 풀던가 안풀던가 할예정 ㅎ