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);
    }
}
相关推荐
你撅嘴真丑5 小时前
第九章-数字三角形
算法
uesowys5 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder5 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮5 小时前
AI 视觉连载1:像素
算法
智驱力人工智能5 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥6 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风6 小时前
代码随想录第十五天
数据结构·算法·leetcode
XX風7 小时前
8.1 PFH&&FPFH
图像处理·算法
NEXT067 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法
代码游侠7 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法