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);
    }
}
相关推荐
海清河晏1112 小时前
数据结构 | 单循环链表
数据结构·算法·链表
wuweijianlove6 小时前
算法性能的渐近与非渐近行为对比的技术4
算法
_dindong6 小时前
cf1091div2 C.Grid Covering(数论)
c++·算法
AI成长日志6 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
黎阳之光6 小时前
黎阳之光:视频孪生领跑者,铸就中国数字科技全球竞争力
大数据·人工智能·算法·安全·数字孪生
skywalker_116 小时前
力扣hot100-3(最长连续序列),4(移动零)
数据结构·算法·leetcode
6Hzlia6 小时前
【Hot 100 刷题计划】 LeetCode 17. 电话号码的字母组合 | C++ 回溯算法经典模板
c++·算法·leetcode
wfbcg7 小时前
每日算法练习:LeetCode 209. 长度最小的子数组 ✅
算法·leetcode·职场和发展
_日拱一卒7 小时前
LeetCode:除了自身以外数组的乘积
数据结构·算法·leetcode
计算机安禾7 小时前
【数据结构与算法】第36篇:排序大总结:稳定性、时间复杂度与适用场景
c语言·数据结构·c++·算法·链表·线性回归·visual studio