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);
    }
}
相关推荐
dragoooon342 小时前
[C++——lesson29.数据结构进阶——「AVL树」]
算法
碧海银沙音频科技研究院2 小时前
论文写作word插入公式显示灰色解决办法
人工智能·深度学习·算法
长沙京卓2 小时前
【无人机算法】低空经济下无人机巡检检测识别算法(城市、林业、水利)
算法·无人机
hn小菜鸡2 小时前
LeetCode 1971.寻找图中是否存在路径
算法·leetcode·职场和发展
Han.miracle2 小时前
数据结构与算法--007三数之和(medium)
算法·leetcode·排序算法
听风吹等浪起2 小时前
机器学习算法:随机梯度下降算法
人工智能·深度学习·算法·机器学习
落羽的落羽2 小时前
【C++】哈希扩展——位图和布隆过滤器的介绍与实现
linux·服务器·开发语言·c++·人工智能·算法·机器学习
仁桃仁呀2 小时前
160.相交链表
数据结构·算法·链表
普密斯科技3 小时前
从点测量到解决方案:光谱共焦技术如何集成于运动平台,实现3D轮廓扫描与透明物体测厚?
人工智能·算法·计算机视觉·3d·集成测试·测量