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);
    }
}
相关推荐
handler018 分钟前
算法:Trie树(字典树)
c语言·数据结构·c++·笔记·算法·深度优先
ZPC82109 分钟前
PPO (Proximal Policy Optimization) 算法模块详细拆解
人工智能·pytorch·算法·机器人
阿Y加油吧12 分钟前
力扣打卡day06——滑动窗口最大值、最小覆盖子串
数据结构·算法·leetcode
沉鱼.4413 分钟前
日期题目集
数据结构·算法
前端摸鱼匠14 分钟前
面试题3:自注意力机制(Self-Attention)的计算流程是什么?
人工智能·ai·面试·职场和发展
Book思议-21 分钟前
【数据结构考研真题】链表题
c语言·数据结构·算法·链表·408·计算机考研
lifallen21 分钟前
从零推导一个现代 ReAct Agent框架
人工智能·算法·语言模型
⁤⁢初遇22 分钟前
数据结构---排序
数据结构·算法·排序算法
前端摸鱼匠25 分钟前
大模型面试题1:简述大模型(LLM)的定义,与传统NLP模型的核心区别是什么?
人工智能·ai·语言模型·自然语言处理·面试·职场和发展
2401_8463416526 分钟前
C++动态链接库开发
开发语言·c++·算法