We hope you all are enjoying our videos!!! Don't forget to leave a comment!!! Please like the video to support us!!! Questions you might like: ✅✅✅[ Tree Data Structure ] : ru-vid.com/group/PLJtzaiEpVo2zx-rCqLMmcFEpZw1UpGWls ✅✅✅[ Graphs Data Structure ] : ru-vid.com/group/PLJtzaiEpVo2xg89cZzZCHqX03a1Vb6w7C ✅✅✅[ January Leetcoding Challenge ] : ru-vid.com/group/PLJtzaiEpVo2wCalBcRcNjXQ0C6ku3dRkn ✅✅✅[ December Leetcoding Challenge ] : ru-vid.com/group/PLJtzaiEpVo2xo8OdPZxrpybGR8FmzZpCA ✅✅✅[ November Leetcoding Challenge ] : ru-vid.com/group/PLJtzaiEpVo2yMYz5RPH6pfB0wNnwWsK7e ✅✅✅[ August Leetcoding Challenge ] : ru-vid.com/group/PLJtzaiEpVo2xu4h0gYQzvOMboclK_pZMe ✅✅✅July Leetcoding challenges: ru-vid.com/group/PLJtzaiEpVo2wrUwkvexbC-vbUqVIy7qC- ✅✅✅June Leetcoding challenges: ru-vid.com/group/PLJtzaiEpVo2xIfpptnCvUtKrUcod2zAKG ✅✅✅May Leetcoding challenges: ru-vid.com/group/PLJtzaiEpVo2wRmUCq96zsUwOVD6p66K9e ✅✅✅Cracking the Coding Interview - Unique String: ru-vid.com/group/PLJtzaiEpVo2xXf4LZb3y_BopOnLC1L4mE Struggling in a question?? Leave in a comment and we will make a video!!!🙂🙂🙂
I think it is almost the same. const s = 'LVIII' let result = 0 const dict = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 }; let arr = s.split(''); for (var i = 0; i < arr.length; i++) { if (dict[arr[i]] < dict[arr[i + 1]]) { result += (dict[arr[i]]) * (-1) } else { result += dict[arr[i]] } } return(result)
I have a question, in LeetCode all code should be inside the function? Even the const Map that will be use multiple times? Can I put this map in the Solution class instead?
thanks for this!! One query , how can we handle -ve cases like below? ("IV", "IIII") ("IX", "VIIII") ("XL", "XXXX") ("XC", "LXXXX") ("CD", "CCCC") ("CM", "DCCCC")
C++ code: class Solution { public: int romanToInt(string s) { map map; map['I']=1; map['V']=5; map['X']=10; map['L']=50; map['C']=100; map['D']=500; map['M']=1000; int result = map[s[s.length()-1]]; for(int i =s.length()-2;i>=0;i--){ if(map[s[i]]
Explanation is good 👍but help me for program not working showing this Line 4: Char 13: error: use of undeclared identifier 'character' Mapmap=new HashMap();
Explanation and everything was good. But next time please dont cover up your mistakes. We all are humans (Proof: Check 3rd line of the code at 4:36 and 4:38)
Hi mam, i am facing this issue while programming the roman to integer (Syntax Error: invalid syntax ^ Class Solution { Line 1 (Solution.py)) full program I was did:- Class Solution { public int romanToInt(string, s){ Map map = new Hashmap(); map.put("I" , 1); map.put("V" , 5); map.put("X" , 10); map.put("L" , 50); map.put("C" , 10); map.put("D" , 500); map.put("M" , 1000); int res = map.get(s.charAt(s.length()-1)); for (int i = s.length()-2; i >=0; i--) { if (map.get(s.chartAt(i)) < map.get (s.charAt(i+1))){ res-=map.get(s.charAt(i)); } else{ res+=map.get(s.charAt(i)); } } return res; } } Do we have a any method to resolve it?
Mapmap = new HashMap(); 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 res = map.get(s.charAt(s.length()-1)); for (int i = s.length()-2; i >=0; i--) { if (map.get(s.charAt(i)) < map.get (s.charAt(i+1))){ res-=map.get(s.charAt(i)); } else{ res+=map.get(s.charAt(i)); } } return res; Use this code, This is edited version of your code.
Your solution is 5 ms. This solution is 4 ms. Which one is better to choose? public int romanToInt(String s) { int sum = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'I') sum += 1; if (s.charAt(i) == 'V') sum += 5; if (s.charAt(i) == 'X') sum += 10; if (s.charAt(i) == 'L') sum += 50; if (s.charAt(i) == 'C') sum += 100; if (s.charAt(i) == 'D') sum += 500; if (s.charAt(i) == 'M') sum += 1000; if ( i != 0) { if (s.charAt(i) == 'V' && s.charAt(i - 1) == 'I') sum -=2; if (s.charAt(i) == 'X' && s.charAt(i - 1) == 'I') sum -=2; if (s.charAt(i) == 'L' && s.charAt(i - 1) == 'X') sum -=20; if (s.charAt(i) == 'C' && s.charAt(i - 1) == 'X') sum -=20; if (s.charAt(i) == 'D' && s.charAt(i - 1) == 'C') sum -=200; if (s.charAt(i) == 'M' && s.charAt(i - 1) == 'C') sum -=200; } } return sum; }
May anyone please tell me why does my code always ignore the last element? HashMap map = new HashMap(); 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 total =0; for(int i=0; i