3. Longest Substring Without Repeating Characters

문제

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given “abcabcbb”, the answer is “abc”, which the length is 3.

Given “bbbbb”, the answer is “b”, with the length of 1.

Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

주어진 문자열에서 중복되지 않는 가장 긴 문자열을 찾아 길이를 리턴하는 문제

풀이

const lengthOfLongestSubstring = (s) => {
    let max = 0;
    let temp = [];
    for ( let i = 0; i < s.length; i++ ) {
        temp = [];
        for ( let j = i; j < s.length; j++ ) {
            if ( temp.indexOf(s[j]) === -1 ) {
                temp.push(s[j]);
                if ( max < temp.length ) {
                    max = temp.length
                }
            } else {
                break;
            }
        }
    }
    return max
};

for문 한개로 짜보려 했는데 도무지 생각이 안나서 두개로 어떻게 짜긴 짰는데 별로 좋은 코드는 아닌것 같다.

그래도 쓰긴 쓴거니까 설명을 붙이자면,

temp라는 임시 배열에 j번째 원소가 없다면 푸시해주는 방식으로 해서 temp 배열 길이의 최대값을 max에 담아 리턴하는식

어렵다 난이도가 medium 밖에 안되는 문젠데 ㅜ 갈길이 멀다.

다른사람의 풀이

const lengthOfLongestSubstring = function(s) {
    let result = '';
    let tempResult = '';
    for ( let i = 0; i < s.length; i++ ) {
        if ( tempResult.indexOf(s[i]) == -1 ) {
            tempResult += s[i];
            if ( tempResult.length > result.length ) {
                result = tempResult;
            }
        } else {
            if ( tempResult.length > result.length ) {
                result = tempResult;
            }
            let index = tempResult.indexOf(s[i]);
            tempResult = tempResult.slice(index+1) + s[i];
        }
    }
    return result.length;
};

사실 코드를 보고도 어떻게 하는지 감이 딱 오진 않지만, 내가 짠 코드보다는 낫다는건 알 것 같다.

내공이 부족한것 같다.


'자료구조, 알고리즘 > leetcode 문제' 카테고리의 다른 글

9. Palindrome Number ( easy )  (0) 2017.11.01
13. Roman to Integer ( easy )  (0) 2017.10.31
7. Reverse Integer ( easy )  (0) 2017.10.30
1. Two Sum ( easy )  (0) 2017.10.29
0. LeetCode 알고리즘 풀이  (0) 2017.10.29

Two Sum

문제

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

nums라는 배열에서 임의의 두 수의 합이 target이 될 때 그 두수의 인덱스를 리턴하는 문제.
이 때 타겟이 만들어지는 경우는 한가지밖에 없다고 가정

풀이

const twoSum = (nums, target) =>  {
    for ( let i = 0; i < nums.length; i++ ) {
        if ( nums.indexOf(target-nums[i]) !== -1 && nums.indexOf(target-nums[i]) !== i ) {
            return [i, nums.indexOf(target-nums[i])]
        }
    }
};

포문으로 첫번째 인덱스 값부터 돌 때 타겟에서 뺀값이 배열내에 존재하면 해당 인덱스를 리턴하는 식

말로 설명하는게 더어렵다 ㅜ


LeetCode 알고리즘 공부

자바스크립트로 공부하기

보시면서 틀린 부분, 이해안되는 부분, 본인이 생각하는 더 나은 코드가 있다면 남겨주시기 바랍니다!

화이팅

티스토리 블로그 폰트 바꾸기

  1. 설정에서 HTML/CSS 편집 들어가기





  2. 구글 폰트에서 원하는 폰트를 찾아 @import방식으로 사용
    폰트를 다운받아 사용할 수도 있지만, 다운받지않고
    바로 폰트를 가져오는 방법이 더 편해서 원하는 폰트를 찾아 코드를 가져옴.
    예시로 이 블로그는 ‘Lato’ 폰트를 사용했음

  3. HTML/CSS 편집창에서 CSS 클릭후 최상단에 붙여넣기


  4. Ctrl + F 로 font-family를 검색해 원하는 폰트 이름을 삽입 후 저장
    검색이 안될때는 새로고침 후 검색
    폰트의 두께는 font-weight 속성으로 설정


끝!

6/159


'개발 > CSS' 카테고리의 다른 글

CSS 중앙정렬 가이드  (6) 2019.10.31
CSS 정리  (3) 2018.04.20
CSS 포지션(position)  (0) 2017.10.12
CSS 디스플레이(display)  (0) 2017.10.12
CSS 박스모델(box-model)  (0) 2017.10.12

'개발 > CSS' 카테고리의 다른 글

CSS 정리  (3) 2018.04.20
CSS 변수(variables)  (0) 2017.10.13
CSS 디스플레이(display)  (0) 2017.10.12
CSS 박스모델(box-model)  (0) 2017.10.12
CSS reset  (0) 2017.06.09

'개발 > CSS' 카테고리의 다른 글

CSS 변수(variables)  (0) 2017.10.13
CSS 포지션(position)  (0) 2017.10.12
CSS 박스모델(box-model)  (0) 2017.10.12
CSS reset  (0) 2017.06.09
CSS flex-box 재미있게 익히기  (0) 2017.04.24

'개발 > CSS' 카테고리의 다른 글

CSS 포지션(position)  (0) 2017.10.12
CSS 디스플레이(display)  (0) 2017.10.12
CSS reset  (0) 2017.06.09
CSS flex-box 재미있게 익히기  (0) 2017.04.24
CSS / float를 clear하는 방법 네가지(clearfix)  (9) 2017.04.13

+ Recent posts