LeetCode zigzag 、roman to integer

eclipse使用快捷键:
1.  查找声明 : F3 ( 或者 Ctrl 加鼠标左键点击 )

2.  头文件和源文件切换 . Ctrl +Tab

3.  查看类继承关系图 Ctrl +T

4.  查找源文件 Ctrl +Shift+R

5.  查找字段 Ctrl +H

6.  查找文件的函数或者全局变量 Ctrl +O

7.  查找被引用的位置 Ctrl + Shift + G
string convert(string s, int nRows) {

    if (nRows <= 1)
        return s;

    const int len = (int)s.length();
    string *str = new string[nRows];

    int row = 0, step = 1;
    for (int i = 0; i < len; ++i)
    {
        str[row].push_back(s[i]);

        if (row == 0)
            step = 1;
        else if (row == nRows - 1)
            step = -1;

        row += step;
    }
    s.clear();
    for (int j = 0; j < nRows; ++j)
    {
        s.append(str[j]);
    }
    delete[] str;
    return s;
}
//
public int romanToInt(String s) {
     int sum=0;
    if(s.indexOf("IV")!=-1){sum-=2;}
    if(s.indexOf("IX")!=-1){sum-=2;}
    if(s.indexOf("XL")!=-1){sum-=20;}
    if(s.indexOf("XC")!=-1){sum-=20;}
    if(s.indexOf("CD")!=-1){sum-=200;}
    if(s.indexOf("CM")!=-1){sum-=200;}

    char c[]=s.toCharArray();
    int count=0;

   for(;count<=s.length()-1;count++){
       if(c[count]==‘M‘) sum+=1000;
       if(c[count]==‘D‘) sum+=500;
       if(c[count]==‘C‘) sum+=100;
       if(c[count]==‘L‘) sum+=50;
       if(c[count]==‘X‘) sum+=10;
       if(c[count]==‘V‘) sum+=5;
       if(c[count]==‘I‘) sum+=1;

   }

   return sum;

}

  

时间: 2024-08-01 06:23:02

LeetCode zigzag 、roman to integer的相关文章

【LeetCode】6 - Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Solution: 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 个位数举例 Ⅰ-1.Ⅱ-2.Ⅲ-3.Ⅳ-4.Ⅴ-5.Ⅵ-6.Ⅶ-7.Ⅷ-8.Ⅸ-9 十位数举例 Ⅹ-10.Ⅺ-11.Ⅻ-12.XIII-13.XIV-14.

【leetcode】13. Roman to Integer

题目描述: Given a roman numeral, convert it to an integer. 解题思路: 罗马计数法有以下规则: 基本数字 Ⅰ.X .C 中的任何一个.自身连用构成数目.或者放在大数的右边连用构成数目.都不能超过三个:放在大数的左边只能用一个: 不能把基本数字 V .L .D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目:放在大数的右边采用相加的方式构成数目.只能使用一个: V 和 X 左边的小数字只能用 Ⅰ: L 和 C 左边的小数字只能用X: D

leetcode笔记:Roman to Integer

一.题目描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 二.题目分析 还是先总结一下罗马数字,这是网上找到的一些解释: 罗马数字是最古老的数字表示方式,比阿拉伯数组早2000多年,起源于罗马- 罗马数字有如下符号: 基本字符: I V X L C D M 对应阿拉伯数字 :1 5 10 50 100 500 1000 计数规

LeetCode解题报告--Roman to Integer

题目:罗马数字转为阿拉伯数字 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 分析:题意:将给定的罗马数字转为阿拉伯数字 从前往后遍历罗马数字,如果某个数比前一个数小,则把该数加入到结果中: 反之,则在结果中两次减去前一个数并加上当前这个数: java 代码:(accepted) import java.util.HashMap;

【LeetCode】013. Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 题解: 只要知道罗马数字的规律即可 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 1.相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3: 2.小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,

[LeetCode] NO.13 Roman to Integer

[题目] Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. [题目解析] 这道题很关键的是要理解罗马数字的几个主要的代表符号和表示方式,罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000). 用来表示数字的时候,如果相邻的两个罗马数字,前面表示的数字比后面的小,那么就要减去前面的

【leetcode】7 Roman to Integer

罗马字符转整数 注意事项: 1 几个罗马字符对应的整数 'I':  1; 'V':  5; 'X':  10; 'L':   50; 'C':  100; 'D':  500; 'M': 1000; 2 对于DC这种前者大于后者的好处理, 对于CD这种前者小于后者的,相当于C+D-2*C class Solution {public:    int toInt(char x){        switch(x){            case 'I': return 1;           

「Leetcode」13. Roman to Integer(Java)

分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (int i=0; i!=s.length(); ++i) { switch(s.charAt(i)) { case 'I': if(i<s.length()-1 && (s.charAt(i+1)=='X' || s.charAt(i+1)=='V')) { ans--; break; }

乘风破浪:LeetCode真题_013_Roman to Integer

乘风破浪:LeetCode真题_013_Roman to Integer 一.前言 上一节我们讨论了如何把阿拉伯数字转换成罗马数字,现在我们需要思考一下如何把罗马数字转换成阿拉伯数字,其实我们仔细观擦这些结构就会发现罗马数字如果前面的比后面的小,就需要用后面的减去前面的.而且如果有这样的运算,也只是两个字符拼接而成的,这为我们解题提供了思路. 二.Roman to Integer 2.1 问题 2.2 分析与解决 根据题意,我们可以明白只需要从开始到结尾遍历这些罗马数字,如果发现前一个小于后一个