算法第二周解题报告

本文介绍了如何将字符串转换为整数并实现字符串的反转及回文验证,包括处理边界情况和异常输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8. String to Integer (atoi)

问题描述:Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

解题思路:这道题主要的难点有二:

第一在于合法的输入样例不明确,例如整数的开头能不能带0,带0的整数要不要算做八进制数等都是需要尝试才知道的问题。

第二,如何判断当前的数字是否越界。在这里本人使用的方法是:

  以INT_MAX 2147483647为例,其是一个10位数。 对于一个10进制数a1a2a3...an,可以将其拆分为a1 * 10 ^(n-1) + a2a3...an。这样当n==10时,若a1 >= 2 且 a1a2....an >= 147483647,我们可以知道a1....an >= INT_MAX,我们可以直接返回INT_MAX。

代码如下:

class Solution {
public:
	int myAtoi(string str) {
		bool isPositive = true;
		int index = 0;
		while (str[index] == ' ')
			++index;
		if (index == str.length()) return 0;
		if (str[index] != '+' && str[index] != '-' && (str[index] < '0' || str[index] > '9')) return 0;
		if (str[index] == '+' || str[index] == '-'){
			if (!(index + 1 != str.length() && str[index + 1] >= '0' && str[index + 1] <= '9'))return 0;
		}
		if (str[index] == '+' || str[index] == '-'){
			isPositive = str[index] == '+' ? true : false;
			++index;
		}
		//if (str[index] == '0') return 0;
		int res = 0;
		int bits = 0;
		while (index != str.length() && str[index] >= '0' && str[index] <= '9'){
			if (bits >= 10){
				return isPositive ? INT_MAX : INT_MIN;
			}
			if (bits == 9){
				int tmp = (int)pow(10, 8);
				int head = res / tmp;
				int rem = res % tmp;
				rem = rem * 10 + str[index] - '0';
				if (isPositive == true && head >= 2 && rem >= 147483647){
					return INT_MAX;
				}
				if (isPositive == false && head >= c && rem >= 147483648){
					return INT_MIN;
				}
			}
			res = res * 10 + (str[index] - '0');
			++index;
			++bits;
		}
		//cout << (isPositive) << endl;
		int as = (isPositive == true ? 1 : -1);
		return as * res;
	}
};
结果如下:


151. Reverse Words in a String

题目描述:

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

解题思路:

    这道题是一道简单题,主要的疑惑点是不知道题目对于分割符的限定以及处理方式,WA几次后终于知道分隔符全是空格,且重组后每个单词间有且只有一个空格;但是题目中有一行要求是C程序员需要在O(1)的辅助空间下完成这个操作,这就真的让我百思不得其解了。

代码如下:

using namespace std;
class Solution {
public:
	void reverseWords(string &s) {
		bool isNull = false;
		for (int i = 0; i < s.length(); ++i){
			if (s[i] != ' '){
				isNull = true;
				break;
			}
		}
		if (isNull == false){
			s = "";
			return;
		}
		string res(s);
		int index = 0;
		int begin = s.length() - 1;
		int end = begin;
		for (int i = s.length() - 1; i >= 0; --i){
			if (s[i] == ' '){
				for (int j = i + 1; j <= end; j++){
					res[index++] = s[j];
				}
				res[index++] = ' ';
				while (i >= 0 && s[i] == ' '){
					//res[index++] = s[i];
					--i;
				}
				end = i;
			}
		}
		if (s[0] != ' '){
			for (int i = 0; i <= end; ++i){
				res[index++] = s[i];
			}
		}
		if (s[0] == ' ' && s[s.length() - 1] == ' '){
			s = res.substr(1, index - 2);
		}
		else if (s[0] == ' ' && s[s.length() - 1] != ' '){
			s = res.substr(0, index - 1);
		}
		else if (s[0] != ' ' && s[s.length() - 1] == ' '){
			s = res.substr(1, index - 1);
		}
		else{
			s = res;
		}
	}
};
运行结果如下:


125. Valid Palindrome

问题描述:

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

解题思路:简单题,直接在字符串头尾设置两个指针,当指针指向的字符不是合法字符时一直滑动到下一个合法字符出现的位置。当前两个指针指向的合法字符若不相等返回false,否则当头指针begin >= 尾指针end时返回true。

代码如下:

using namespace std;
#include<algorithm>
using namespace std;
class Solution {
public:
	bool isPalindrome(string s) {
		int begin = 0;
		int end = s.length() - 1;
		for (int i = 0; i < s.length(); ++i){
			if (s[i] >= 'A' && s[i] <= 'Z'){
				s[i] += 'a' - 'A';
			}
		}
		while (begin < end){
			while (!IsValidC(s[begin])){
				++begin;
			}
			while (!IsValidC(s[end])){
				--end;
			}
			if (end <= begin) return true;
			if (s[begin] != s[end]) {
				return false;
			}
			++begin;
			--end;
		}
		return true;
	}
	bool IsValidC(char c){
		return ((c >= 'a' &&  c <= 'z') ||
			(c >= 'A' && c <= 'Z') ||
			(c >= '0' && c <= '9')) ? true : false;
	}
};
程序运行结果如下:


内容概要:本文从关键概念、核心技巧、应用场景、代码案例分析及未来发展趋势五个维度探讨了Python编程语言的进阶之路。关键概念涵盖装饰器、生成器、上下文管理器、元类和异步编程,这些概念有助于开发者突破基础认知的核心壁垒。核心技巧方面,介绍了内存优化、性能加速、代码复用和异步处理的方法,例如使用生成器处理大数据流、numba库加速计算密集型任务等。应用场景展示了Python在大数据处理、Web开发、人工智能和自动化运维等多个领域的广泛运用,特别是在FastAPI框架中构建异步API服务的实战案例,详细分析了装饰器日志记录、异步数据库查询和性能优化技巧。最后展望了Python的未来发展趋势,包括异步编程的普及、类型提示的强化、AI框架的深度整合以及多语言协同。 适合人群:已经掌握Python基础语法,希望进一步提升编程技能的开发者,特别是有意向从事数据科学、Web开发或AI相关工作的技术人员。 使用场景及目标:①掌握Python进阶概念和技术,如装饰器、生成器、异步编程等,提升代码质量和效率;②学习如何在实际项目中应用这些技术,如通过FastAPI构建高效的异步API服务;③了解Python在未来编程领域的潜在发展方向,为职业规划提供参考。 阅读建议:本文不仅提供了理论知识,还包含了丰富的实战案例,建议读者在学习过程中结合实际项目进行练习,特别是尝试构建自己的异步API服务,并通过调试代码加深理解。同时关注Python社区的发展动态,及时掌握最新的技术和工具。
内容概要:本文档《Rust系统编程实战》详细介绍了Rust在系统编程领域的应用,强调了其内存安全、零成本抽象和高性能的特点。文档分为三个主要部分:核心实战方向、典型项目案例和技术关键点。在核心实战方向中,重点讲解了unsafe编程、FFI(外部函数接口)和底层API调用,涉及操作系统组件开发、网络编程、设备驱动开发、系统工具开发和嵌入式开发等多个领域,并列出了每个方向所需的技术栈和前置知识。典型项目案例部分以Linux字符设备驱动为例,详细描述了从环境搭建到核心代码实现的具体步骤,包括使用bindgen生成Linux内核API的Rust绑定,定义设备结构体,以及实现驱动核心函数。 适合人群:对系统编程有兴趣并有一定编程基础的开发者,尤其是那些希望深入了解操作系统底层机制、网络协议栈或嵌入式系统的工程师。 使用场景及目标:①掌握Rust在不同系统编程场景下的应用,如操作系统组件开发、网络编程、设备驱动开发等;②通过实际项目(如Linux字符设备驱动)的学习,理解Rust与操作系统内核的交互逻辑;③提高对unsafe编程、FFI和底层API调用的理解和运用能力。 阅读建议:由于文档内容较为深入且涉及多个复杂概念,建议读者在学习过程中结合实际操作进行练习,特别是在尝试实现Linux字符设备驱动时,务必按照文档提供的步骤逐步进行,并多加调试和测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值