罗马数字转换成阿拉伯数字以及递归的简单运用
2017-03-06 00:16
459 查看
继上周的阿拉伯数字转换成罗马数字,顺便把罗马数字转阿拉伯数字的也编写一下。
1.题目:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题目解析:
这道题主要运用两个规则,其一当左边的罗马数字比右边的罗马数字大时,则相加;反之,则想减。
程序如下:
class Solution {
public:
int romanToInt(string s) {
int graph[400];
graph['I']=1;
graph['V']=5;
graph['X']=10;
graph['L']=50;
graph['C']=100;
graph['D']=500;
graph['M']=1000;
int sum=graph[s[0]];
int len=s.length();//注意:在函数定义的s,不能调用函数size()求长度
for(int i=0;i<len-1;i++)
{
if(graph[s[i]]>=graph[s[i+1]])
sum+=graph[s[i+1]];
else
sum=sum+graph[s[i+1]]-2*graph[s[i]];
}
return sum;
}
};
这是一道运用递归解决的链表题。
2.题目:
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
题目解析:
此题是有一个循环的,即每两个节点互换,这样我们就可以用到递归,当然也有普通循环解法。
递归程序:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (head == NULL || head->next == NULL) {
return head;
}
ListNode* stamp = head->next;
head->next = swapPairs(stamp->next);
stamp->next = head;
return stamp;
}
};
循环程序:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode **p = &head, *a, *b;
while ((a = *p) && (b = a->next)) {
a->next = b->next;
b->next = a;
*p = b;
p = &(a->next);
}
return head;
}
};
1.题目:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题目解析:
这道题主要运用两个规则,其一当左边的罗马数字比右边的罗马数字大时,则相加;反之,则想减。
程序如下:
class Solution {
public:
int romanToInt(string s) {
int graph[400];
graph['I']=1;
graph['V']=5;
graph['X']=10;
graph['L']=50;
graph['C']=100;
graph['D']=500;
graph['M']=1000;
int sum=graph[s[0]];
int len=s.length();//注意:在函数定义的s,不能调用函数size()求长度
for(int i=0;i<len-1;i++)
{
if(graph[s[i]]>=graph[s[i+1]])
sum+=graph[s[i+1]];
else
sum=sum+graph[s[i+1]]-2*graph[s[i]];
}
return sum;
}
};
这是一道运用递归解决的链表题。
2.题目:
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given
1->2->3->4, you should return the list as
2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
题目解析:
此题是有一个循环的,即每两个节点互换,这样我们就可以用到递归,当然也有普通循环解法。
递归程序:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (head == NULL || head->next == NULL) {
return head;
}
ListNode* stamp = head->next;
head->next = swapPairs(stamp->next);
stamp->next = head;
return stamp;
}
};
循环程序:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode **p = &head, *a, *b;
while ((a = *p) && (b = a->next)) {
a->next = b->next;
b->next = a;
*p = b;
p = &(a->next);
}
return head;
}
};
相关文章推荐
- 罗马数字转换成阿拉伯数字
- 将阿拉伯数字转换成罗马数字
- 一个将阿拉伯数字转换成中文大写的最简单算法
- 将罗马数字转换成阿拉伯数字
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- 罗马数字转换阿拉伯数字0~3999
- 将罗马数字转换成普通的阿拉伯数字
- Java实现阿拉伯数字转换成中文大写数字,以及中文大写数字到阿拉伯数字的转换。
- Python简单实现阿拉伯数字和罗马数字的互相转换功能示例
- [导入][原创] 阿拉伯数字转换汉字大小写以及货币大小写
- 【C++】将罗马数字转换成阿拉伯数字
- Python实现将罗马数字转换成普通阿拉伯数字的方法
- 超级简单的大写金额转换--阿拉伯数字转中文大写
- 罗马数字转换阿拉伯数字(Java版,考虑较为全面)
- 一个比较简单的,只做了正数,一万位以内的,阿拉伯数字转换成大写数字的程序
- 一个将阿拉伯数字转换成中文大写的最简单算法
- 将Asp.net从VS2003以及VS2002工程转换为VS2005工程的简单而又行之有效的方法
- C#实现的【阿拉伯数字转换成中文大写金额】代码(原创)
- 简单的stringstream实例--实现字符串与数字之间相互转换
- 用C#将任意位整数阿拉伯数字转换成中文大写