leetcode:相等的有理数

题目描述

cpp 复制代码
class Solution {
public:
bool is_repeating(std::string s)
{
	auto i = s.find('(');
	if (i < s.size())
	{
		return true;
	}
	else
	{
		return false;
	}
}

std::string get_repeating_part(std::string s)
{
	auto i1 = s.find('(');
	auto i2 = s.find(')');
	std::string repeating_part = s.substr(i1+1,i2-i1-1);
	return repeating_part;
}

void shift_repeating_part(std::string & front_part, std::string & repeating_part, int n)
{
	std::string new_repeating_part = "";
	for (int i = n; i < n + repeating_part.size(); i++)
	{
		new_repeating_part.push_back(repeating_part[i%repeating_part.size()]);
	}
	for (int i = 0; i < n; i++) front_part.push_back(repeating_part[i%repeating_part.size()]);
	repeating_part = new_repeating_part;
}

std::string extend_repeating_part(std::string & s, int n)
{
	std::string new_repeating_part = s;
	for (int i = 0; i < n-s.size(); i++)
	{
		new_repeating_part.push_back(s[i%s.size()]);
	}
	return new_repeating_part;
}

bool repeating_part_all_9(std::string s)
{
	bool all_9 = true;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] != '9') 
		{
			all_9 = false;
			break;
		}
	}
	return all_9;
}

int last_not_9(std::string s)
{
	int loc = s.size()-1;
	for (; loc >=0; loc--)
	{
		if (s[loc] != '.' && s[loc] != '9')
		{
			break;
		}
	}
	return loc;
}

void front_part_process(std::string & front_part)
{
		std::string new_front_part = front_part;
		for (int i = last_not_9(front_part) + 1; i < front_part.size(); i++)  if(front_part[i]!='.') new_front_part[i] = '0';
		if (last_not_9(front_part) == -1) {
			new_front_part = "1" + front_part;
		}
		else {
			new_front_part[last_not_9(front_part)]++;
		}
		front_part=new_front_part;
}

bool compare_rational(std::string s1, std::string s2)
{
	// 统一转换为循环小数
	if (s1.find('.') == string::npos)
	{
		s1 += '.';
	}
	if (s2.find('.') == string::npos)
	{
		s2 += '.';
	}	
	if (is_repeating(s1) == false)
	{
		s1 += "(0)";
	}
	if (is_repeating(s2) == false)
	{
		s2 += "(0)";
	}
	// 循环节靠后者为s2
	auto i1 = s1.find('(');
	auto i2 = s2.find('(');
	if (i1 > i2) {
		std::string s = s1;
		int temp = i1;
		i1 = i2;
		i2 = temp;
		s1 = s2;
		s2 = s;
	}
	// 对齐循环节出现的位置
	std::string s1_repeating = get_repeating_part(s1);
	std::string s2_repeating = get_repeating_part(s2);
	std::string s1_front = s1.substr(0, i1);
	std::string s2_front = s2.substr(0, i2);
	shift_repeating_part(s1_front, s1_repeating, i2 - i1);
	// 对齐循环节的长度
	int target_repeating_size = (s1_repeating.size() * s2_repeating.size());
	s1_repeating = extend_repeating_part(s1_repeating, target_repeating_size);
	s2_repeating = extend_repeating_part(s2_repeating, target_repeating_size);
	// 循环节全9转全0
	if (repeating_part_all_9(s1_repeating))
	{
		front_part_process(s1_front);
		for (int i = 0; i < s1_repeating.size(); i++)
		{
			s1_repeating[i] = '0';
		}
	}
	if (repeating_part_all_9(s2_repeating))
	{
		front_part_process(s2_front);
		for (int i = 0; i < s2_repeating.size(); i++)
		{
			s2_repeating[i] = '0';
		}
	}
	// 比对有理数字符串
	if (s1_front.compare(s2_front) == 0 && s1_repeating.compare(s2_repeating) == 0) return true;
	return false;
}
    bool isRationalEqual(string s, string t) {
        return compare_rational(s, t);
    }
}
相关推荐
AlenTech1 分钟前
226. 翻转二叉树 - 力扣(LeetCode)
算法·leetcode·职场和发展
Tisfy5 分钟前
LeetCode 1458.两个子序列的最大点积:动态规划
算法·leetcode·动态规划·题解·dp
求梦8205 分钟前
【力扣hot100题】合并区间(9)
算法·leetcode·职场和发展
汽车仪器仪表相关领域20 分钟前
工况模拟精准检测,合规减排赋能行业 ——NHASM-1 型稳态工况法汽车排气检测系统项目实战经验分享
数据库·算法·单元测试·汽车·压力测试·可用性测试
chilavert31829 分钟前
技术演进中的开发沉思-299 计算机原理:数据结构
算法·计算机原理
C+-C资深大佬38 分钟前
C++逻辑运算
开发语言·c++·算法
天天进步20151 小时前
KrillinAI 源码级深度拆解二:时间轴的艺术:深入 KrillinAI 的字幕对齐与音频切分算法
算法·音视频
爱编程的小吴1 小时前
【力扣练习题】121. 买卖股票的最佳时机
算法·leetcode·职场和发展
生信大杂烩1 小时前
空间转录组分析新工具 | MEcell:自适应微环境感知建模,精准解析细胞身份!
算法·数据分析
kaikaile19951 小时前
计算向量x的功率谱密度
算法