Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Roman to Integer - Leetcode 13.js - Invalid Roman Numeric Value Test Case #29

Open
@maheshmuttintidev

Description

@maheshmuttintidev

I found that this solution doesn't handle the "Invalid Roman Numeric Value".

Ex: If we passIIXX, The output is20 which is wrong since theIIXX is invalid roman number. In this case, we throw an error, rest of the code continues..

Here is myjs solution:

functionromanToInt(s){constd={I:1,V:5,X:10,L:50,C:100,D:500,M:1000};letsumm=0;constn=s.length;leti=0;while(i<n){if(i>1&&d[s[i-2]]<d[s[i-1]]&&d[s[i]]<=d[s[i-1]]){thrownewError(`Invalid Roman numeric sequence${s}`);}if(i<n-1&&d[s[i]]<d[s[i+1]]){summ+=d[s[i+1]]-d[s[i]];i+=2;}else{summ+=d[s[i]];i++;}}returnsumm;}console.log(romanToInt('IIXX'));console.log(romanToInt('MMD'));

Note

It would be helpful if you add the example test cases when you have the solution for the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp