프라이D
프라이Develog(❁´◡`❁)
프라이D
전체 방문자
오늘
어제
  • ALL (378)
    • TDD, Cleancode with JavaScr.. (5)
    • 프로젝트 (32)
      • work (3)
      • 직접 만드는 기술 블로그 (2)
      • 데일리 옥션 (19)
      • 모락모락 (8)
    • Computer Science (1)
    • Algorithm & 자료구조 (94)
      • 알고리즘 w.JavaScript (53)
      • 자료구조 (5)
      • (인프런) 자바스크립트 알고리즘 문제풀이 (34)
    • JavaScript (45)
      • JavaScript (41)
      • 모던 자바스크립트 Deep Dive (4)
    • WEB (13)
    • 회고 (12)
    • TIL (109)
    • WIL (7)
    • Stacks (20)
      • React.js (6)
      • Next.js (1)
      • Redux (3)
      • Node.js (2)
      • GIT (2)
      • SAP (1)
    • 15일 메이킹 프로젝트 (15)
    • 이전 기록 (14)
    • ETC. (5)
    • ---------------2021 (6)
      • 내일배움단-웹개발 5주 (2)
      • 정보처리기사 (4)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • nomadcoders
  • 알고리즘
  • 코드스테이츠
  • 비트마스크
  • 내일배움단
  • 모던자바스크립트딥다이브
  • 국비지원
  • 투포인터알고리즘
  • 내일배움카드
  • Til
  • 2023 인프콘 후기
  • JavaScript
  • 자바스크립트
  • 코딩프로젝트
  • 자바스크립트비트마스크
  • MySQL
  • 자바스크립트알고리즘
  • 스파르타코딩클럽
  • nomadcoder
  • vanilaJS

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
프라이D

프라이Develog(❁´◡`❁)

Algorithm & 자료구조/알고리즘 w.JavaScript

151. Reverse Words in a String

2024. 1. 3. 22:53
 

Reverse Words in a String - LeetCode

Can you solve this real interview question? Reverse Words in a String - Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a strin

leetcode.com

내 풀이

/*
 [문제]
 - 문자열 s가 주어졌을 때, 단어들의 순서를 뒤집어라
 - 단어들은 공백을 기준으로 나뉘고, 공백을 기점으로 뒤집어야함
 - s가 주어질 때 단어 사이에 여러개의 공백이 있을 수 있는데, 
 - 앞,뒤 공백도 다 없애고 단어는 무조건 하나의 공백으로만 구분지어야 함
  */
var reverseWords = function reverseWords(s) {
  // 단어를 돌면서 공백이 나오기 직전까지 단어를 임시 저장해두었다가
  // 공백이 나오는 시점에 해당 문자열을 배열에 push
  // 그런담에 순서를 뒤집어서 join 하면 될 것 같음

  const words = [];
  let temp = '';

  for (let i = 0; i < s.length; i++) {
    const curr = s[i];
    const isCurrSpace = curr === ' ';

    if (!isCurrSpace) {
      temp += s[i];
    }

    if ((isCurrSpace || i === s.length - 1) && temp) {
      words.push(temp);
      temp = '';
    }
  }

  return words.reverse().join(' ');
};

 

더 나은 풀이

var reverseWords = function(s) {
    let words = s.trim().split(/\s+/).filter(word => word !== ''); 
    const reverseword = words.reverse();
    return reverseword.join(' ');
};
저작자표시 (새창열림)

'Algorithm & 자료구조 > 알고리즘 w.JavaScript' 카테고리의 다른 글

[Leetcode] 345. Reverse Vowels of a String  (0) 2024.01.03
[Leetcode] 605. Can Place Flowers  (0) 2023.12.31
[Leetcode] 1431. Kids With the Greatest Number of Candies  (0) 2023.12.31
[Leetcode] 1071. Greatest Common Divisor of Strings  (0) 2023.12.28
[Leetcode] 1768.Merge Strings Alternately  (2) 2023.12.18
    'Algorithm & 자료구조/알고리즘 w.JavaScript' 카테고리의 다른 글
    • [Leetcode] 345. Reverse Vowels of a String
    • [Leetcode] 605. Can Place Flowers
    • [Leetcode] 1431. Kids With the Greatest Number of Candies
    • [Leetcode] 1071. Greatest Common Divisor of Strings
    프라이D
    프라이D
    틀린내용 정정 및 개선사항은 언제든지 댓글 달아주세요 :D

    티스토리툴바