Java实现LeetCode_0013_RomanToInteger

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


posted @   南墙1  阅读(78)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· .NET 8.0 + Linux 香橙派,实现高效的 IoT 数据采集与控制解决方案
· .NET中 泛型 + 依赖注入 的实现与应用
点击右上角即可分享
微信分享提示