LeetCode刷題之旅(簡單-4): 羅馬數字轉整數

2019年5月3日java

目錄app

題目:性能

測試用例​測試

解決方法一:使用hashMap映射鍵值,根據規則進行數值判斷ui

性能結果:編碼

小結:url

解決方法二:性能確實好,可是if太多了spa

性能結果:.net

小結:code

其餘總結:


題目:

測試用例

 

解決方法一:使用hashMap映射鍵值,根據規則進行數值判斷

package leetCode;

import java.util.Arrays;
import java.util.HashMap;

/**
 * Date: 2019/5/3 10 :28
 *
 */

public class RomanNumeralToInteger {
    //匿名內部類初始化法,調用父類(非私有方法)方法,向上轉型
    private static HashMap<String, Integer> romanNumberToIntegerMap = new HashMap<String, Integer>(){
  {
        put("I",1);
        put("V",5);
        put("X",10);
        put("L",50);
        put("C",100);
        put("D",500);
        put("M",1000);
        put("IV",4);
        put("IX",9);
        put("XL",40);
        put("XC",90);
        put("CD",400);
        put("CM",900);
    }};

    public static int romanToInt(String s) {


        char[] romanNumbers = s.toCharArray();
        Integer result = 0;

        for (int i = 0; i < romanNumbers.length ; i ++){
            switch (romanNumbers[i]) {
                case 'I':
                    if ((i < romanNumbers.length-1) && (romanNumbers[i + 1] == 'V' || romanNumbers[i + 1] == 'X')) {
                        result += getIntegerValue(romanNumbers[i] , romanNumbers[i + 1]);
                        i++;
                    }else {
                        result += getIntegerValue(romanNumbers[i]);
                    }
                    break;
                case 'X':
                    if ((i < romanNumbers.length-1) && (romanNumbers[i + 1] == 'L' || romanNumbers[i + 1] == 'C')) {
                        result += getIntegerValue(romanNumbers[i] , romanNumbers[i + 1]);
                        i++;
                    }else {
                        result += getIntegerValue(romanNumbers[i]);
                    }
                    break;
                case 'C':
                    if ((i < romanNumbers.length-1) && (romanNumbers[i + 1] == 'D' || romanNumbers[i + 1] == 'M')) {
                        result += getIntegerValue(romanNumbers[i] , romanNumbers[i + 1]);
                        i++;
                    }else {
                        result += getIntegerValue(romanNumbers[i]);
                    }
                    break;
                default:
                    result += getIntegerValue(romanNumbers[i]);
                    break;
            }
        }

        return result;
    }

    public static Integer getIntegerValue(Character... characters){
        StringBuilder sb = new StringBuilder();
        Arrays.stream(characters).forEach(
            (r) -> {
                sb.append(r);
            }
        );
        return romanNumberToIntegerMap.get(sb.toString());
    }

    public static void main(String[] args){
        int result = romanToInt("MCMXCIV");
        System.out.println("result: "+result);
    }
}

性能結果:

小結:

  • 我以爲封裝好了一個專用方法,能提現層次感,可是因爲HashMap佔用更多的內存,致使性能不如意;

 

解決方法二:性能確實好,可是if太多了

class Solution {
	public int romanToInt(String s) {
		char[] a = s.toCharArray();
		int sum = 0;
		for (int i = 0; i < a.length; i++) {
			if (i != a.length - 1) {
				if (a[i] == 'I' && a[i + 1] == 'V') {
					sum += 4;
					i++;
					continue;
				}
				if (a[i] == 'I' && a[i + 1] == 'X') {
					sum += 9;
					i++;
					continue;
				}
				if (a[i] == 'X' && a[i + 1] == 'L') {
					sum += 40;
					i++;
					continue;
				}
				if (a[i] == 'X' && a[i + 1] == 'C') {
					sum += 90;
					i++;
					continue;
				}
				if (a[i] == 'C' && a[i + 1] == 'D') {
					sum += 400;
					i++;
					continue;
				}
				if (a[i] == 'C' && a[i + 1] == 'M') {
					sum += 900;
					i++;
					continue;
				}
			}
			if (a[i] == 'I')
				sum += 1;
			if (a[i] == 'V')
				sum += 5;
			if (a[i] == 'X')
				sum += 10;
			if (a[i] == 'L')
				sum += 50;
			if (a[i] == 'C')
				sum += 100;
			if (a[i] == 'D')
				sum += 500;
			if (a[i] == 'M')
				sum += 1000;

		}
		return sum;
	}
}

性能結果:

小結:

  • 這是網友提供的解法,使用硬編碼,也使用了窮舉方法,對輸入字符串進行一一組合斷定;
  • 也許以爲代碼比較冗餘,可是我執行以後發現性能確是最好的;

其餘總結:

  • String 和 Char的區別是什麼?

char 定義時用 單引號 ,只能有一個字母數字,char 是一個基本類型;

eg:char a='h';   //不能直接對a調用方法

 

String定義時用 雙引號 ,能夠是一個,或者多個字母 漢字等,就是所謂的字符串,String 是一個類,能夠直接引用;

eg:String a="hello";    //String是類,能夠對對象進行調用

a.charAt(0);//返回 h 

a.charAt(1);//返回 e

  • java方法的可變參數使用?

這是JDK1.5新增語法,新特性,動態參數或者是可變參數的意思。  (1)使用…將參數聲明成可變長參數。  (2)可變長參數必須是最後一個參數。  (3)可變參數同時能夠跟固定的參數混合使用,可是一個方法的參數中不能同時擁有2種類型的可變參數。

相關文章
相關標籤/搜索