Java实现LeetCode_0013_RomanToInteger

本文介绍了一种将罗马数字转换为整数的算法实现,提供了三种不同的方法:使用Map直接映射、利用栈结构逆向计算以及通过特殊组合进行修正。通过多种测试数据验证了算法的有效性和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package javaLeetCode.primary;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;

public class RomanToInteger_13 {
	public static void main(String[] args) {
		System.out.println("Please input a roman numeral:");
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		String s = input.next();
		System.out.println(romanToInt_3(s));
	}// end main()

	/**
	 * Conventional thinking.
	 * Scan from head to tail and use Map or Array.
	 */
	/*
	 * Test Data: 
	 * III--3 
	 * IV--4 
	 * IX--9 
	 * LVIII--58 
	 * MCMXCIV--1994
	 * MCCCXIV--1314
	 * MMMIX--3009
	 * MMMCCXLIX--3249
	 */
	public static int romanToInt_1(String s) {
		char[] str = s.toCharArray();
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		map.put('I', 1);
		map.put('V', 5);
		map.put('X', 10);
		map.put('L', 50);
		map.put('C', 100);
		map.put('D', 500);
		map.put('M', 1000);

		int num = 0;
		for (int i = 0; i < s.length() - 1; i++) {
			if (map.get(str[i]) >= map.get(str[i + 1])) {
				num += map.get(str[i]);
			} else {
				num -= map.get(str[i]);
			} // end if
		} // end for
		num += map.get(str[str.length - 1]);
		return num;
	}// end romanToInt()
	
	/**
	 * Conventional thinking.
	 * Scan from head to tail and use Stack to reserve the total.
	 */
	public static int romanToInt_2(String s) {
		char[] str = s.toCharArray();
		Stack <Integer>romanStack = new Stack<Integer>();
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		map.put('I', 1);
		map.put('V', 5);
		map.put('X', 10);
		map.put('L', 50);
		map.put('C', 100);
		map.put('D', 500);
		map.put('M', 1000);
		

		int num = 0;
		romanStack.push(map.get(str[0]));
		for (int i = 1; i < s.length() ; i++) {
			if (map.get(str[i]) <= map.get(str[i - 1])) {
				romanStack.push(map.get(str[i]));
			} else {
				romanStack.push(map.get(str[i])-romanStack.pop());
			} // end if
		} // end for
		
		//Don't write "for(int I =0;I < romanStack. The size ();I++) ", 
		//because the stack size is fetched every time the loop occurs
		int length = romanStack.size();
		for(int i=0;i<length;i++) {
			num += romanStack.pop();
		}//end for
		return num;
	}// end romanToInt()
	
	public static int romanToInt_3(String s) {
		int num = 0;
		for(int i=0;i<s.length();i++) {
			char c = s.charAt(i);
			if(c=='I') {num += 1;}
			if(c=='V') {num += 5;}
			if(c=='X') {num += 10;}
			if(c=='L') {num += 50;}
			if(c=='C') {num += 100;}
			if(c=='D') {num += 500;}
			if(c=='M') {num += 1000;}
			
		}//end for
		
		if(s.contains("IV")) {num -= 2;}
		if(s.contains("IX")) {num -= 2;}
		if(s.contains("XL")) {num -= 20;}
		if(s.contains("XC")) {num -= 20;}
		if(s.contains("CD")) {num -= 200;}
		if(s.contains("CM")) {num -= 200;}
		
		return num;
	}// end romanToInt()
}// end RomanToInteger_13


评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值