当前位置: 首页 > news >正文

Comparative linguistics

class Solution:def romanToInt(self, s: str) -> int:d = {"I": 1,"V": 5,"X": 10,"L": 50,"C": 100,"D": 500,"M": 1000,}ret = 0i = 0while i < len(s) -1:if (d[s[i]] < d[s[i+1]]):ret -= d[s[i]]else:ret += d[s[i]]i+=1return ret + d[s[i]]
int hash(char c)
{switch(c){case 'I':return 1;break;case 'V':            return 5;break;        case 'X':return 10;break;        case 'L':return 50;break;        case 'C':return 100;break;        case 'D':return 500;break;        case 'M':return 1000;break;        default:break;}return 0;
}int romanToInt(char* s) {int ret = 0;for (int i = 0; i < strlen(s);){if (hash(s[i]) < hash(s[i+1])){ret += (hash(s[i+1]) - hash(s[i]));i+=2;}else {ret += hash(s[i]);i++;}}return ret;
}

all 100%.

  1. C has no hashtable, so I used switch, which actually hash('\0') as 0.
  2. len(s) and strlen(s) in Python and C all got a 3.
    but in python:
>>> s = "abc"
>>> s[3]
Traceback (most recent call last):File "<stdin>", line 1, in <module>
IndexError: string index out of range
printf("%d\n%d", strlen(s), s[4]);

in C, s[3] will printf 0 as %d, and s[4] could be anything but it's accessable. such as 37, whatever.

so in C, I actually access the last char of s as '\0'. But in python, the operator is not safe. but there's also no char type in python.

http://www.gsyq.cn/news/48336.html

相关文章:

  • 2025 国产 ITSM 厂商选型全攻略:基础流程、智能赋能与全链路协同深度解析
  • 2025年WMS仓库管理系统行业观察:智能仓储新格局加速成型
  • 数字无线电 带通调制 / 载波 概念
  • 完整教程:吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
  • 2025WMS仓库管理系统选型攻略
  • 2025年电缆桥架厂家权威推荐榜单:不锈钢电缆桥架/模压电缆桥架/槽式电缆桥架源头厂家精选
  • 2025年11月篷布厂家排名:十强厂家横向对比与选购参考
  • 2025年11月炒股开户券商排行:五家资质全维度对比评价
  • 数字无线电系统的结构分类
  • Flink 的 RocksDB 状态后端在 vivo 的实践
  • TLS1.3协议分析(2)
  • 2025年深圳子女抚养权律师权威推荐榜单:股权分割/离婚房产/继承律师服务专家精选
  • 分类样本不平衡问题
  • 2025年深圳股权分割律师权威推荐榜单:离婚房产/子女抚养权/继承律师服务专家精选
  • 2025年可靠的建筑业体系认证三体系认证服务实力榜
  • 2025项目管理软件测评指南:11款海内外系统排名及选购建议
  • 2025年知名的防砸防刺穿劳保鞋厂家最新权威推荐榜
  • 2025年比较好的会议室舞台灯光厂家最新推荐榜
  • 2025年热门的盐城短视频拍摄本地服务TOP推荐
  • cesium加载天地图影像地图与影像注记
  • 2025年诚信的知产认证知识产权贯标热门口碑排行榜
  • InfluxDB时序数据库
  • 2025年知名的短视频运营本地服务TOP推荐
  • 【大内容项目】基于Spark的海底捞门店绩效内容可视化分析系统\python海底捞门店运营分析与可视化环境源码
  • 2025年比较好的内衣推荐TOP品牌厂家
  • 2025年11月20万六座SUV推荐:六座空间与智能兼得
  • 2025年正规的抖音运营信赖推荐榜
  • 2025年11月工程管理软件推荐榜:全流程数字化排行解析
  • 一文讲解什么是PD快充诱骗协议芯片 及PD诱骗协议芯片的应用场景
  • nmcli常用命令