LeetCode.Bash 相关题

本文介绍了使用Bash脚本来处理文本文件中电话号码的有效性验证及读取特定行的技巧。通过具体的示例,展示了如何匹配两种格式的有效电话号码:(xxx)xxx-xxxx和xxx-xxx-xxxx,并读取文件的第10行内容。

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

193. Valid Phone Numbers

Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.

You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

You may also assume each line in the text file must not contain leading or trailing white spaces.

Example:

Assume that file.txt has the following content:

987-123-4567
123 456 7890
(123) 456-7890

Your script should output the following valid phone numbers:

987-123-4567
(123) 456-7890

Bash 语句

# Read from the file file.txt and output all valid phone numbers to stdout.
sed -nr '/^(\([0-9]{3}\) ){1}[0-9]{3}-[0-9]{4}$|^([0-9]{3}-){2}[0-9]{4}$/p' file.txt

195. Tenth Line

Given a text file file.txt, print just the 10th line of the file.

Example:

Assume that file.txt has the following content:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

Your script should output the tenth line, which is:

Line 10

Note:
1. If the file contains less than 10 lines, what should you output?
2. There's at least three different solutions. Try to explore all possibilities.

Bash 语句

# Read from the file file.txt and output the tenth line to stdout.
sed -n '10p' file.txt

 

### 如何在本地环境中设置和运行Leetcode目 要在本地环境中设置并运行LeetCode目,可以通过以下方式实现完整的开发流程: #### 使用IntelliJ IDEA插件运行LeetCode目 通过安装 **LeetCode Editor** 插件可以在 IntelliJ IDEA 中直接打开 LeetCode 目,并自动生成代码框架。然而,默认生成的文件名可能包含中文字符,这会引发编译错误[^1]。因此建议修改命名模板为英文或其他可识别格式。 对于 C++ 用户来说,如果希望将这些生成的代码迁移到本地环境,则需额外关注输入/输出部分的设计。因为 LeetCode 的在线评测系统通常隐藏了测试用例的具体细节,而我们需要手动模拟类似的交互逻辑来验证程序行为[^2]。 #### 设置C++项目的构建工具链 针对Windows平台上的开发者而言,推荐采用MinGW-w64作为GCC系列编译器解决方案之一;与此同时也要记得调整VS Code等相关IDE内的配置项使其能够正确寻址至所安装好的外部依赖库路径下[^3]。 当完成上述准备工作之后就可以着手编写具体的解决方法啦! 以下是基于G++命令行操作的一个简单例子展示如何创建项目结构以及执行整个工作流: ```bash mkdir leetcode_project && cd $_ touch main.cpp Makefile run.sh input.txt output.txt nano main.cpp # 编辑源码文件 ``` 接着定义Makefile以便简化后续重复性的任务自动化处理过程: ```makefile all: executable test executable: g++ -std=c++17 -Wall -Wextra -pedantic-errors -o app main.cpp test: ./app <input.txt>output.txt diff expected_output.txt output.txt || echo "Test Failed" clean: rm -f *.o app core.* output.txt .PHONY : all clean test ``` 最后补充Batch脚本用于一键启动调试环节(适用于CMD窗口): ```batch @echo off g++ -std=c++17 -Wall -Wextra -pedantic-errors -o app.exe main.cpp if %errorlevel% neq 0 exit /b %errorlevel% type NUL >output.txt app.exe<input.txt>output.txt fc output.txt expected_output.txt||echo Test failed. pause del app.exe exit /b ``` 以上步骤展示了从零开始建立适合个人习惯的工作区布局方案的同时还兼顾到了跨平台兼容性考量因素。 #### 添加必要的头文件和支持功能 无论选择哪种具体技术栈组合形式都需要确保最终产物满足基本的功能需求标准即支持标准输入输出重定向机制从而便于后期维护扩展升级等工作顺利开展下去[^4]。 例如下面这段示范性质较强的伪代码片段就很好地诠释出了这一设计理念精髓所在之处: ```c++ #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> twoSum(vector<int>& nums, int target); }; int main(){ freopen ("input.txt","r",stdin); freopen ("output.txt","w",stdout); /* Your code here */ } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值