zoukankan      html  css  js  c++  java
  • [Leetcode 17] 13 Roman to Integer

    Problem:

    Given a roman numeral, convert it to an integer.

    Input is guaranteed to be within the range from 1 to 3999.

    Analysis:

    Some Roman Number examples can be seen here: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm

    Use a one pass scan to decide whether add or subtract a number from current sum.

    If a number is greater or equal to its successor or it's the last number, add it.

    If a numver is less than its successor, subtract it.

    Code:

     1 public class Solution {
     2     public int romanToInt(String s) {
     3         // Start typing your Java solution below
     4         // DO NOT write main() function
     5         int sum = 0;
     6         
     7         for (int i=0; i<s.length(); ++i) {
     8             int numi = rtoi(s.charAt(i));
     9             
    10             if (i==(s.length()-1)) {
    11                 sum += numi;
    12             } else if ( numi >= rtoi(s.charAt(i+1))) {
    13                 sum += numi;
    14             } else {
    15                 sum -= numi;
    16             }
    17         }
    18     
    19         return sum;
    20     }
    21     
    22     private int rtoi(char x) {
    23         int val=0; 
    24         
    25         switch (x) {
    26             case 'I': val = 1; break;
    27             case 'V': val = 5; break;
    28             case 'X': val = 10; break;
    29             case 'L': val = 50; break;
    30             case 'C': val = 100; break;
    31             case 'D': val = 500; break;
    32             case 'M': val = 1000; break;
    33         }
    34         
    35         return val;
    36     }
    37 }
    View Code

    An efficient version that call helper function only once in each iteration

     1 public class Solution {
     2     public int romanToInt(String s) {
     3         // Start typing your Java solution below
     4         // DO NOT write main() function
     5         int sum = 0, numi=0, numiSucc=rtoi(s.charAt(0));
     6         
     7         for (int i=0; i<s.length(); ++i) {
     8             numi = numiSucc;
     9             
    10             if (i==(s.length()-1)) {
    11                 sum += numi;
    12                 break;
    13             }
    14             
    15             numiSucc = rtoi(s.charAt(i+1));
    16             if ( numi >= numiSucc) {
    17                 sum += numi;
    18             } else {
    19                 sum -= numi;
    20             }
    21         }
    22     
    23         return sum;
    24     }
    25     
    26     private int rtoi(char x) {
    27         int val=0; 
    28         
    29         switch (x) {
    30             case 'I': val = 1; break;
    31             case 'V': val = 5; break;
    32             case 'X': val = 10; break;
    33             case 'L': val = 50; break;
    34             case 'C': val = 100; break;
    35             case 'D': val = 500; break;
    36             case 'M': val = 1000; break;
    37         }
    38         
    39         return val;
    40     }
    41 }
    View Code

    Attention:

    Know what is Roman Number before coding.

  • 相关阅读:
    Python进阶之浅谈内置方法(补充)
    Python进阶之浅谈内置方法
    Python运算符优先级
    python之浅谈循环
    MTCNN
    MTCNN
    tf.train.batch and tf.train.shuffle_batch
    tf.add_to_collection 和 tf.get_collection 和 tf.add_n
    tensorflow 迁移学习-Demo
    tensorflow 加载预训练模型进行 finetune 的操作解析
  • 原文地址:https://www.cnblogs.com/freeneng/p/3086465.html
Copyright © 2011-2022 走看看