[Geeks For Geeks] Reverse words in a given String

2021. 2. 18. 08:58ยทAlgorithm/Algorithm (๋ฌธ์ œํ’€์ด)

๐Ÿ”’ ๋ฌธ์ œ 

.์„ ๊ธฐ์ค€์œผ๋กœ ์˜จ์ „ํžˆ ๋‹จ์–ด์ˆœ์„œ๋งŒ ๋’ค์ง‘๋Š” ๋ฌธ์ œ

 


๊ตฌ๋ถ„์ž๊ฐ€ .์ด ์•„๋‹ˆ๋ผ ' ' ๊ณต๋ฐฑ์ด ์—ฌ๋Ÿฌ๊ฐœ ๋“ค์–ด์™”์„ ๊ฒฝ์šฐ์— ๋’ค์ง‘๋Š” ํ’€์ด

 

๐Ÿ”‘ ํ’€์ด (C++)

#include <iostream>
#include <string>
using namespace std;

string reverseWords(string S) {
    int endIndex = S.length();

    string result = "";

    while (endIndex > 0) {
        while (endIndex > 0 && S[endIndex--] == ' ');

        int startIndex = endIndex;
        while (startIndex > 0 && S[--startIndex] != ' ');


        if (startIndex != 0) {
            string word = S.substr(startIndex + 1, endIndex - startIndex);
            result += (word + " ");
        } else {
            string word = S.substr(startIndex, endIndex - startIndex);
            result += word;
        }

        endIndex = startIndex;
    }

    return result;
}


int main()
{
    cout << reverseWords("i.like.this.program.very.much") << endl;

    return 0;
}

 


๊ณต๋ฐฑ์ด ์—ฌ๋Ÿฌ ๋ฌธ์ž ์—ฐ์†์œผ๋กœ ๋“ค์–ด์˜ค์ง€ ์•Š๊ณ  ํŠน์ • delimiter ๊ธฐ์ค€์œผ๋กœ๋งŒ ๊ตฌ๋ถ„ํ•˜๋ฉด ๋˜๋Š”๋ฌธ์ œ๋Š” 

kotlin, JavaScript์— ๋‚ด์žฅ๋œ split ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

๐Ÿ”‘ ํ’€์ด (Javascript)

class Solution {
    reverseWords(s){
        return s.split('.').reverse().join('.');
    }
}

chaining ํ•œ์ค„๋กœ ๋๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋ฅผ

C++ character array ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ผ์ผํžˆ ์ธ๋ฑ์Šค ์ปจํŠธ๋กค์„ํ•˜๋ฉด ์•„์ฃผ ์Œฉ ์‘ˆ๋ฅผ ํ•ด์•ผํ•œ๋‹ค.

๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋ฌธ์ œ ๋งŒํผ์€ C, C++์„ ์“ฐ์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ •์‹ ๊ฑด๊ฐ•์— ์ข‹๋‹ค๋Š”๊ฒŒ ๋‚ด ์ง€๋ก ์ด๋‹ค.
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'Algorithm > Algorithm (๋ฌธ์ œํ’€์ด)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Geeks for Geeks] Validate an IP Address  (0) 2021.02.20
[C++] Iterator  (0) 2021.02.20
[Geeks For Geeks] Reverse a String  (0) 2021.02.18
[GeeksForGeeks] Prim  (0) 2021.02.08
[GeeksForGeeks] Detect Cycle in a directed graph  (0) 2021.02.05
'Algorithm/Algorithm (๋ฌธ์ œํ’€์ด)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [Geeks for Geeks] Validate an IP Address
  • [C++] Iterator
  • [Geeks For Geeks] Reverse a String
  • [GeeksForGeeks] Prim
M_Falcon
M_Falcon
  • M_Falcon
    Falcon
    M_Falcon
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (432)
      • Web (16)
        • Nodejs (14)
        • Javascript (23)
        • FrontEnd (4)
      • DataBase (39)
        • Fundamental (1)
        • Redis (4)
        • PostgreSQL (10)
        • NoSQL (4)
        • MySQL (9)
        • MSSQL (3)
        • Error (4)
      • Algorithm (79)
        • Algorithm (๋ฌธ์ œํ’€์ด) (56)
        • Algorithm (์ด๋ก ) (23)
      • JVM (65)
        • Spring (13)
        • JPA (5)
        • Kotlin (13)
        • Java (24)
        • Error (7)
      • ๊ธฐํƒ€ (70)
        • Kafka (3)
        • Kubernetes (3)
        • Docker (13)
        • git (19)
        • ์žก๋™์‚ฌ๋‹ˆ (27)
      • ์žฌํ…Œํฌ (11)
        • ์„ธ๋ฌด (4)
        • ํˆฌ์ž (3)
        • ๋ณดํ—˜ (0)
      • BlockChain (2)
        • BitCoin (0)
      • C (32)
        • C (10)
        • C++ (17)
        • Error (3)
      • Low Level (8)
        • OS (3)
        • ์‹œ์Šคํ…œ ๋ณด์•ˆ (5)
      • ๋„คํŠธ์›Œํฌ (3)
      • LINUX (30)
        • Linux (26)
        • Error (4)
      • ์ €์ž‘๊ถŒ๊ณผ ์Šค๋งˆํŠธํฐ์˜ ์ดํ•ด (0)
      • ์ƒ๊ฐ ๋ญ‰์น˜ (6)
      • ๊ถ๊ธˆ์ฆ (2)
      • Private (4)
        • ์ด์ง ๊ฒฝํ—˜ (0)
        • ๊ฟˆ์„ ์ฐพ์•„์„œ (1)
      • Android (21)
        • OS (4)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • WEB
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜
    • DataBase
    • Linux
    • Mobile
    • C
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

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

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
    linux
    android
    C++
    ๋ฐฑ์ค€
    kafka
    Programmers
    database
    PostgreSQL
    Spring
    docker
    JPA
    Git
    java
    Bitcoin
    Kotlin
    javascript
    ์•Œ๊ณ ๋ฆฌ์ฆ˜
    ubuntu
    algorithm
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
M_Falcon
[Geeks For Geeks] Reverse words in a given String
์ƒ๋‹จ์œผ๋กœ

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