leetcode:Integer to Roman + Fin…
来源:互联网 发布:睡了兄弟的女朋友知乎 编辑:程序博客网 时间:2024/05/16 17:33
Integer to Roman:
这个思路还是挺简单,只要理清罗马文的书写规则即可,原理就是用递归,不断的减小数字,转为罗马文。如:
if(num>=50) return "L"+intToRoman(num-50);
Find Right Interval:
给出一组线段的起点和终点,要求逐个找出这些线段右边第一个线段的id。中心思路就是找到第一个start大于目标线段end的线段。
自己想的方法很2,把所有线段按照start升序sort一下,然后两层for循环逐个寻找。
之所以要这么麻烦,是因为原线段数组是无序的,而我要暴力查找第一个“大于特定值”就势必得先让数组有序。所以思路从一开始就错了,首先暴力查找不一定要有序,直接找也行。其次,我忽略了一个很重要的STL函数:map容器里的lower_bound函数,功能是返回不小于key值的第一个元素,而map本身是按照key值的有序容器,所以用map当然更有效率。
Maximum Product of Word Lengths:
给出一组字符串,找出两个完全没有重复字母的字符串,求满足条件的字符串对中,字符串长度乘积最大值。
我的想法是,把字符串按长度sort一次,然后遍历数组,对于每两个字符串,用map逐个字符比较。这样的方法挂在了大数据的输入中,TLE
后来看了题解,思路是用一个整型数组记录每个字符串组成,比如含有a,则把第26位置1等等。再用与运算比较
另外,用这道题学到了几点:
1、初始化一个二维数组,且指定二维数组的维度:
vector > chcount(words.size(),vector (26,0));
2、max(a,b)函数不能接受字符串size()的返回值,必须static()来转为整型。
阅读全文
0 0
- leetcode:Integer to Roman + Fin…
- leetcode:Fraction to Recurring …
- How to get a good idea to perfor…
- How to pass a hash to a subrouti…
- How to move LOB Data to Another …
- unable to create …
- how to do with a slow oracle dat…
- Make sure to have the zlib libs …
- Make sure to have the zlib libs …
- How to Disable and Turn Off UAC …
- tomorrow I will have a trip to W…
- How to sign a Driver by Windows …
- How to got the Mac address from …
- yii2 Call to undefined functi…
- OracleserviceSID to write to net…
- A + B Problem (Big integer versi…
- A * B Problem (Big integer versi…
- How to be a …
- 数据库的锁表和解锁操作
- leetcode:Fraction to Recurring …
- leetcode:Single Number123 …
- leetcode:Linked List Random Node…
- leetcode:Shuffle an Array + Miss…
- leetcode:Integer to Roman + Fin…
- finetune入门,如何用已有的caffem…
- 009-logback中configuration的logger和root子节点
- 如何同时使用相同项目的两个分支
- 在Ubuntu上安装opencv
- leetcode:Bulb Switcher + K…
- leetcode:Reconstruct Original D…
- intellij idea导入极客验证码官方demo以及在Tomcat中部署
- c++使用杂录(一)