首页 > 其他分享 >leetcode 12

leetcode 12

时间:2023-08-23 22:24:27浏览次数:39  
标签:12 string int 哈希 leetcode 贪心

image

算法介绍:

  • 哈希

  • 贪心

  • 实现代码如下

class Solution {
public:
    string intToRoman(int num) {
        // 哈希贪心
		string s[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
		int v[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
		int i = 13;
		string res = "";
		for(int i = 0;i<13;i++){
			int temp = num / v[i];
			num -= temp * v[i];
			
			for(int j = 0;j < temp;j++){
				res += s[i];
			}
		}
		return res;
    }
};

标签:12,string,int,哈希,leetcode,贪心
From: https://www.cnblogs.com/maoshine/p/17652909.html

相关文章

  • ZLMediaKit实现拉取摄像头(海康协议)编码为H265并使用flv.js播放时提示:FLV:Unsupport
    场景开源流媒体服务器ZLMediaKit在Windows上运行、配置、按需拉流拉取摄像头rtsp视频流)并使用http-flv网页播放:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/130136245按照上面流程进行拉取摄像头的rtsp流并转流使用flv.js播放时提示:DemuxException:type-......
  • Leetcode605——种花问题
    假设有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花不能种植在相邻的地块上,它们会争夺水源,两者都会死去。给你一个整数数组 flowerbed 表示花坛,由若干 0 和 1 组成,其中 0 表示没种植花,1 表示种植了花。另有一个数 n ,能否在不打破种植规则的情况下种入 n......
  • [LeetCode][121]best-time-to-buy-and-sell-stock
    ContentYouaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Youwanttomaximizeyourprofitbychoosingasingledaytobuyonestockandchoosingadifferentdayinthefuturetosellthatstock.Returnthemaximu......
  • 无法连接仓库:Command "git ls-remote -h -- https://gitee.com/xxx/xxxrned status co
    无法连接仓库:Command"gitls-remote-h--https://gitee.com/xxx/xxxrnedstatuscode128:stdout:stderr:remote:[session-554c92af]Usernamefor'https:Incorrectusernameorpassword(accesstoken)fatal:Authenticationfailedfor'http......
  • LeetCode 算法题解之 26 进制转换 All In One
    LeetCode算法题解之26进制转换AllInOne26进制转换171.ExcelSheetColumnNumber171.Excel工作表列号functiontitleToNumber(columnTitle:string):number{//如何动态生成字典✅26进制//A-Z->1-26conststrs='ABCDEFGHIJKLMNOPQRSTUVWXYZ';......
  • Python基础入门学习笔记 075 GUI的终极选择:Tkinter12
    Message组件Message(消息)组件是Label组件的变体,用于显示多行文本信息。Message组件能够自动换行,并调整文本的尺寸使其适应给定得尺寸。实例1:1fromtkinterimport*23root=Tk()4w1=Message(root,text="这是一则消息",width=100)5w1.pack()6w2=Message(root,......
  • AOJ0121(Seven Puzzle, BFS+Cantor+逆向思维)
    参考康托展开自己真的一点头绪都没有,根据前面的大神博客自己几乎100%复制了一个,但是还是WA,觉得还是出在选方向时的判断上面。Cantor感觉这个可以作为模板,状态压缩的一个思路。还有就是BFS特点:1.从一个起点到一个终点2.起点和终点可以互相到达,双向的//#defineLOCAL#include......
  • leetcode-1-two sum(brute force, hash table)
    Wecanusebruteforcetogetit,usetwoforloopiandj,whichi=1:nandj=i:n.However,thetimecomplexityisO(n^2),whichisnotefficient.Usehashtable,thefirstthingisfirststoreeveryelementtotable,thendotraverseagaintolookup......
  • Python基础入门学习笔记 012列表:一个打了激素的数组3
    列表的一些常用操作符•比较操作符 •逻辑操作符 •连接操作符 •重复操作符 •成员关系操作符 关于分片“拷贝”概念的补充 >>>dir(list)可查看所有列表的操作函数 count()函数可计算列表中相同元素个数 index()函数可索引列表元素 reverse()将列......
  • Leetcode 459——重复的子字符串
    给定一个非空的字符串 s ,检查是否可以通过由它的一个子串重复多次构成。示例1:输入:s="abab"输出:true解释:可由子串"ab"重复两次构成。示例2:输入:s="aba"输出:false示例3:输入:s="abcabcabcabc"输出:true解释:可由子串"abc"重复四次构成。......