[leetCode] 151. Reverse Words in a String - Javascript

2024. 2. 2. 16:36ยท ๐Ÿ’ป Dev/๐Ÿ’ฌ Algorithm
๋ชฉ์ฐจ
  1. ๋ฌธ์ œ
  2. ์ ‘๊ทผ๋ฐฉ๋ฒ•
  3. ์ฝ”๋“œ 
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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 string of the words in reverse order concatenated by a single space.

Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.

 

Example 1:

Input: s = "the sky is blue"
Output: "blue is sky the"

Example 2:

Input: s = "  hello world  "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.

Example 3:

Input: s = "a good   example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

 

 

 

์ ‘๊ทผ๋ฐฉ๋ฒ•

Initially, I transformed the given string parameter into an array and reversed the array using reverse() method.

After checking all of obects in array using a loop, i removed empty objects. 
Then, I transformed the array back into a string. considering only the none-empty elements.

 

 

 

 

 

์ฝ”๋“œ 

var reverseWords = function(s) {
    let wordArr = []
    s = s.split(' ').reverse()
    s.forEach((w) => {
        if(w != ''){
            wordArr.push(w)
        }
    })
    
    return wordArr.join(" ")

};

 

 

 

 

 

 

 

 

728x90
  1. ๋ฌธ์ œ
  2. ์ ‘๊ทผ๋ฐฉ๋ฒ•
  3. ์ฝ”๋“œ 
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Frontend Developer
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
Ming devlog
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
์ „์ฒด
์˜ค๋Š˜
์–ด์ œ

๊ณต์ง€์‚ฌํ•ญ

  • About Ming ๐Ÿฐ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (261)
    • ๊ฐœ๋ฐœ (255)
      • ๐Ÿ“ TIL (58)
      • โŒ ์˜ค๋ฅ˜ ๋…ธํŠธ (23)
      • React (23)
      • Next.js (11)
      • React-Native (6)
      • JavaScript (10)
      • Flutter (19)
      • Database (1)
      • ๐Ÿ’ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ (21)
      • ๐Ÿ“š English (9)
      • Etc... (25)
      • โ›ต ํ•ญํ•ด99 (49)
    • ๐Ÿ’ป Dev (6)
      • ๐Ÿ“ TIL (4)
      • ๐Ÿ’ฌ Algorithm (1)
      • Etc... (1)

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ์ฝ”ํ…Œ๊ณต๋ถ€
  • twid
  • ๋ฆฌ์•กํŠธ
  • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
  • react-native
  • ๊ฐœ๋ฐœ์ž์ผ๊ธฐ
  • nextjs
  • ํ•ญํ•ด99
  • react
  • ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
  • ํ”Œ๋Ÿฌํ„ฐ
  • Git
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
  • leetcode
  • ๋ถ€ํŠธ์บ ํ”„
  • ์ด์ฝ”๋…ธ๋ฏธ์ŠคํŠธ
  • github
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
  • Firebase
  • TIL
  • ReactNative
  • ๋ฆฌ์•กํŠธ๋„ค์ดํ‹ฐ๋ธŒ
  • flutter
  • ์˜์–ด๊ณต๋ถ€
  • ์ฝ”ํ…Œ
  • ์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ
  • ์ฝ”๋”ฉ๊ณต๋ถ€
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • todaywhatidid
  • ๊ฐœ๋ฐœ์žํšŒ๊ณ 

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

250x250
hELLO ยท Designed By ์ •์ƒ์šฐ.v4.2.1
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
[leetCode] 151. Reverse Words in a String - Javascript
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๊ฐœ์ธ์ •๋ณด

  • ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ
  • ํฌ๋Ÿผ
  • ๋กœ๊ทธ์ธ

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.