LeetCode Bash练习

195. Tenth Line

复制代码
#!/bin/bash
i=0
cat file.txt | while read line
do
    #echo $line
    if [ $i -eq 9 ]
        then echo $line
    fi
    
    let i=i+1
done
    
复制代码

 194. Transpose File

复制代码
# Read from the file file.txt and print its transposed content to stdout.
#########################################################################
# File Name: 194.sh
# Author: atrp
# mail: scau_zjl@163.com
# Created Time: Thu 04 May 2017 09:02:39 AM EDT
#########################################################################
#!/bin/bash
i=1
function getLen() {
    num_column=$#
}
read line < file.txt
getLen $line
while [ 0 -lt 1 ]
do
    column=`cut -d' ' -f$i ./file.txt`
    echo ${column}
    if [ $i -eq ${num_column} ]
    then
        break
    fi
    let i++
done
复制代码

 

193. Valid Phone Numbers

#########################################################################
# File Name: 193.sh
# Author: atrp
# mail: scau_zjl@163.com
# Created Time: Wed 03 May 2017 11:08:51 PM EDT
#########################################################################
#!/bin/bash
grep -P "^\(\d{3}\)\s\d{3}-\d{4}$|^\d{3}-\d{3}-\d{4}$" file.txt

 

192. Word Frequency

复制代码
###########################################################################
# File Name: 192.sh
# Author: atrp
# mail: scau_zjl@163.com
# Created Time: Thu 04 May 2017 06:38:21 AM EDT
#########################################################################
#!/bin/bash
declare -A map1=()
function calc() {
    #echo $@
    for i in $@
    do 
        let map1["$i"]++
        #echo ${map1["$i"]}
    done
}
function out() {
    times=0
    while [ $times -lt ${#map1[@]} ]
    do
        max=0
        id=0
        for i in ${!map1[@]}
        do
            if [ ${max} -lt ${map1[$i]} ]
            then
                max=${map1[$i]}
                id=$i
            fi
        done
        echo "${id} ${max}"
        map1[${id}]=0

        let times++
    done
}
while read line
do
    #echo $line
    calc $line
done < words.txt

out
复制代码

 

posted @   JL_Zhou  阅读(229)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示