LeatherDeer

Untitled

Oct 19th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. public int romanToInt(String s) {
  2.         HashMap<Character, Integer> map = new HashMap<>();
  3.  
  4.         map.put('I', 1);
  5.         map.put('V', 5);
  6.         map.put('X', 10);
  7.         map.put('L', 50);
  8.         map.put('C', 100);
  9.         map.put('D', 500);
  10.         map.put('M', 1000);
  11.  
  12.         int sum = 0;
  13.  
  14.         for (int i = 0; i < s.length() - 1; i++) {
  15.             if (map.get(s.charAt(i)) >= map.get(s.charAt(i + 1))) {
  16.                 sum += map.get(s.charAt(i));
  17.             } else {
  18.                 sum -= map.get(s.charAt(i));
  19.             }
  20.         }
  21.        
  22.         sum += map.get(s.charAt(s.length() - 1));
  23.  
  24.         return sum;
  25. }
  26.  
  27. class ParkingSystem {
  28.     int[] slots;
  29.  
  30.     public ParkingSystem(int big, int medium, int small) {
  31.         slots = new int[3];
  32.         slots[0] = big;
  33.         slots[1] = medium;
  34.         slots[2] = small;
  35.     }
  36.    
  37.     public boolean addCar(int carType) {
  38.         if (slots[carType - 1] > 0) {
  39.             slots[carType - 1] -= 1;
  40.             return true;
  41.         }
  42.         return false;
  43.     }
  44. }
Add Comment
Please, Sign In to add comment