Leetcode #Integer to roman#

Source: Internet
Author: User





A bit of meaning ~ as long as the corresponding treatment for 4 and 9 of the two special cases are good. The rest are simple additions.


All right, just stick out my answer, it's ugly ... Not concise enough. Can see the answer of the Hao God ...



Python:

"" "Programmer:eofdate:2015.04.10file:itr.pye-mail: [email protected]" "" "" "varible Description: @ret_string: We put the returning string into this varible @base: Here is the base number F or Roman counting process @table: We could use the base number to index the corresponding re      Presentation in Roman Numbers @base_len: The length of @base @counter: A copy of @base_len @index : The current base number. ""        Class Solution:def Inttoroman (self, num): ret_string = "" Base = [1, 5, 10, 50, 100, 500, 1000]        Table = {1: "I", 5: "V", "X",: "L", "C", "D", +: "M"} Base_len = Len (base) Counter = Base_len while counter > 0:index = base[counter-1] tmp = num/in      Dex while tmp > 0:if self.hight_bit (num) = = 9:counter-= 1              index = base[counter-1] Ret_string + = table[Base[counter-1]] +                table[Base[counter + 1]] break; elif self.hight_bit (num) = = 4:index = base[counter-1] ret_string + = table[base[c                OUNTER-1]] + table[Base[counter]] break;            Else:ret_string + = Table[index] tmp-= 1; Num%= Index Counter-= 1 return ret_string # We Use this function to get the hightest bit in Num CO  nveniently:) def hight_bit (self, num): TMP = num while tmp > 10:tmp/= return TMP #-----------Just for testing-----------------s = solution () Number = 4print S.inttoroman (number) Number = 1 8print S.inttoroman (number) Number = 3999print S.inttoroman (number) Number = 1600print S.inttoRoman (number) Number = 321print S.inttoroman (number) Number = 400print S.inttoroman (number) 




The C + + implementation of the Hao God:

But the base element of the Hao God is more than mine ... Add a few more base to simplify the problem ... My boy.


source:https://oj.leetcode.com/problems/integer-to-roman///Author:hao chen//date:2014-07-17/***************** * * Given An integer, convert it to a Roman numeral.* * Input is guaranteed to being within the range from 1 to 3999.* ************************************************ /#include <stdlib.h> #include <string> #include <iostream>using namespace std;//greeding algorithmstring inttoroman (int num) {string symbol[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL"        , "X", "IX", "V", "IV", "I"};     int value[] = {1000,900,500,400, 100, 90, 50, 40, 10, 9, 5, 4, 1};    string result;            for (int i=0; num!=0, i++) {while (num >= value[i]) {num-= value[i];        Result+=symbol[i]; }} return result;    int main (int argc, char** argv) {int num = 1234;    if (argc>0) {num = Atoi (argv[1]); } CoUT << num << ":" << inttoroman (num) << Endl; return 0;}








Leetcode #Integer to roman#

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.