ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Programmers] JavaScript 알고리즘 | Lv.0 컨트롤 제트
    ► JS Algorithm/Programmers 2024. 7. 18. 22:47
    반응형

    🔒 문제 설명

    숫자와 "Z" 공백으로 구분되어 담긴 문자열이 주어집니다. 문자열에 있는 숫자를 차례대로 더하려고 합니다. "Z" 나오면 바로 전에 더했던 숫자를 뺀다는 뜻입니다. 숫자와 "Z" 이루어진 문자열 s 주어질 , 머쓱이가 구한 값을 return 하도록 solution 함수를 완성해보세요.


    🔒 제한사항  

    • 1 ≤ s의 길이 ≤ 200
    • -1,000 < s의 원소 중 숫자 < 1,000
    • s는 숫자, "Z", 공백으로 이루어져 있습니다.
    • s에 있는 숫자와 "Z"는 서로 공백으로 구분됩니다.
    • 연속된 공백은 주어지지 않습니다.
    • 0을 제외하고는 0으로 시작하는 숫자는 없습니다.
    • s는 "Z"로 시작하지 않습니다.
    • s의 시작과 끝에는 공백이 없습니다.
    • "Z" 연속해서 나오는 경우는 없습니다.

    🔒 입출력 예

    s result
    "1 2 Z 3" 4
    "10 20 30 40" 100
    "10 Z 20 Z 1" 1
    "10 Z 20 Z" 0
    "-1 -2 -3 Z" -3

    🔒 입출력 예 설명

    입출력 예 #1

    • 본문과 동일합니다.

    입출력 예 #2

    • 10 + 20 + 30 + 40 = 100을 return 합니다.

    입출력 예 #3

    • "10 Z 20 Z 1"에서 10 다음 Z, 20 다음 Z로 10, 20이 지워지고 1만 더하여 1을 return 합니다.

    입출력 예 #4, #5

    설명 생략


    공지 - 2022 11 30 제한사항 테스트 케이스가 수정되었습니다.


     

    🔐 solution of mine

    String.prototype.split()

    Array.prototype.map()

    Array.prototype.forEach()

    function solution(s) {
      let answer = 0;
      let arr = s.split(" ").map((v) => (v === "Z" ? v : Number(v)));
    
      arr.forEach((v, i) => {
        if (v === "Z") {
          answer -= arr[i - 1];
        } else {
          answer += v;
        }
      });
    
      return console.log(answer);
    }
    
    solution("1 2 Z 3"); //output: 4
    solution("10 20 30 40"); //output: 100
    solution("10 Z 20 Z 1"); //output: 1
    solution("10 Z 20 Z"); //output: 0
    solution("-1 -2 -3 Z"); //output: -3

     


     

    🔐 solution of others

    String.prototype.split()

    Array.prototype.forEach()

     

    Array.prototype.pop()

    Array.prototype.push()

    단항 더하기 (+)

    Array.prototype.reduce()

    function solution(s) {
      const stack = [];
    
      s.split(" ").forEach((v) => {
        if (v === "Z") stack.pop();
        else {
          stack.push(+v);
        }
      });
    
      return console.log(stack.reduce((acc, cur) => acc + cur, 0));
    }
    
    solution("1 2 Z 3"); //output: 4
    solution("10 20 30 40"); //output: 100
    solution("10 Z 20 Z 1"); //output: 1
    solution("10 Z 20 Z"); //output: 0
    solution("-1 -2 -3 Z"); //output: -3

     

     

    🔐 solution of others

    String.prototype.split()

    Array.prototype.indexOf()

    Array.prototype.splice()

    Array.prototype.reduce()

    parseInt()

    function solution(s) {
      let arr = s.split(" ");
    
      while (arr.indexOf("Z") > -1) {
        arr.splice(arr.indexOf("Z") - 1, 2);
      }
    
      return console.log(
        arr.reduce((acc, cur) => parseInt(acc) + parseInt(cur), 0)
      );
    }
    
    solution("1 2 Z 3"); //output: 4
    solution("10 20 30 40"); //output: 100
    solution("10 Z 20 Z 1"); //output: 1
    solution("10 Z 20 Z"); //output: 0
    solution("-1 -2 -3 Z"); //output: -3

     

    🔐 solution of others

    String.prototype.split()

    Array.prototype.reduce()

    Array.prototype.slice()

    function solution(s) {
      return s
        .split(" ")
        .reduce(
          (acc, cur) => (cur === "Z" ? acc.slice(0, -1) : [...acc, +cur]),
          []
        )
        .reduce((acc, cur) => acc + cur, 0);
    }
    
    solution("1 2 Z 3"); //output: 4
    solution("10 20 30 40"); //output: 100
    solution("10 Z 20 Z 1"); //output: 1
    solution("10 Z 20 Z"); //output: 0
    solution("-1 -2 -3 Z"); //output: -3

     


     

    반응형
Designed by Tistory.