SHELL18. 域名进行计数排序处理
描述
1 m.nowcoder.com
Bash 解法, 执行用时: 3ms, 内存消耗: 412KB, 提交时间: 2022-04-13
#!/bin/bash #!/bin/awk declare -A map while read line do tmp=(${line//\// }) ((map[${tmp[1]}]++)) done < nowcoder.txt function InsertSort(){ tmp=() for ve in ${map[*]} do tmp[${#tmp[*]}]=$ve done q=${#tmp[*]} for ((i=0;i<$q;i++)) do for ((j=$i+1;j<$q;j++)) do if [ ${tmp[$i]} -lt ${tmp[$j]} ]; then t=${tmp[$i]} tmp[$i]=${tmp[$j]} tmp[$j]=$t fi done done } InsertSort for ((i=0; i<$q; i++)) do for ve in ${!map[*]} do if [ ${tmp[$i]} -eq ${map[$ve]} ]; then printf "${map[$ve]} $ve\n" fi done done
Bash 解法, 执行用时: 3ms, 内存消耗: 412KB, 提交时间: 2021-12-14
# awk # awk -F "/" '{ # arr[$3]++ # } END { # for (i in arr) { # printf("%d %s\n", arr[i], i) # } # }' | sort -r # shell 运行速度极快 declare -A map while read line do tmp=(${line//\// }) ((map[${tmp[1]}]++)) done < nowcoder.txt function InsertSort(){ tmp=() for ve in ${map[*]} do tmp[${#tmp[*]}]=$ve done q=${#tmp[*]} for ((i=0;i<$q;i++)) do for ((j=$i+1;j<$q;j++)) do if [ ${tmp[$i]} -lt ${tmp[$j]} ];then t=${tmp[$i]} tmp[$i]=${tmp[$j]} tmp[$j]=$t fi done done } InsertSort for ((i=0; i<$q; i++)) do for ve in ${!map[*]} do if [ ${tmp[$i]} -eq ${map[$ve]} ];then printf "${map[$ve]} $ve\n" fi done done
Bash 解法, 执行用时: 3ms, 内存消耗: 416KB, 提交时间: 2021-12-17
# awk # awk -F "/" '{ # arr[$3]++ # } END { # for (i in arr) { # printf("%d %s\n", arr[i], i) # } # }' | sort -r # shell 运行速度极快 declare -A map while read line do tmp=(${line//\// }) ((map[${tmp[1]}]++)) done < nowcoder.txt function InsertSort(){ tmp=() for ve in ${map[*]} do tmp[${#tmp[*]}]=$ve done q=${#tmp[*]} for ((i=0;i<$q;i++)) do for ((j=$i+1;j<$q;j++)) do if [ ${tmp[$i]} -lt ${tmp[$j]} ];then t=${tmp[$i]} tmp[$i]=${tmp[$j]} tmp[$j]=$t fi done done } InsertSort for ((i=0; i<$q; i++)) do for ve in ${!map[*]} do if [ ${tmp[$i]} -eq ${map[$ve]} ];then printf "${map[$ve]} $ve\n" fi done done
Bash 解法, 执行用时: 3ms, 内存消耗: 416KB, 提交时间: 2021-11-26
declare -A map while read line do tmp=(${line//\// }) ((map[${tmp[1]}]++)) done < nowcoder.txt function InsertSort(){ tmp=() for ve in ${map[*]} do tmp[${#tmp[*]}]=$ve done q=${#tmp[*]} for ((i=0;i<$q;i++)) do for ((j=$i+1;j<$q;j++)) do if [ ${tmp[$i]} -lt ${tmp[$j]} ];then t=${tmp[$i]} tmp[$i]=${tmp[$j]} tmp[$j]=$t fi done done } InsertSort for ((i=0; i<$q; i++)) do for ve in ${!map[*]} do if [ ${tmp[$i]} -eq ${map[$ve]} ];then printf "${map[$ve]} $ve\n" fi done done
Bash 解法, 执行用时: 3ms, 内存消耗: 428KB, 提交时间: 2022-01-06
# 这个就类似字符串重复次数,并排序 declare -A map while read line do tmp=(${line//\// }) #替换后http: www.nowcoder.com index.html name=${map[${tmp[1]}]} # echo $name if [ -z $name ] then map[${tmp[1]}]=1 else ((map[${tmp[1]}]++)) fi done < nowcoder.txt # 构造数组 arr=() for key in ${!map[@]} do arr[${#arr[@]}]=${map[$key]} done len=${#arr[@]} # 对数组排序(降序) for (( i=0;i<len;i++ )) do for (( j=i+1;j<len;j++ )) do if [ ${arr[$i]} -lt ${arr[$j]} ] then tmp=${arr[$i]} arr[$i]=${arr[$j]} arr[$j]=$tmp fi done done # 按数组排序顺序输出 for (( k=0;k<len;k++ )) do for key in ${!map[@]} do if [ ${map[${key}]} -eq ${arr[$k]} ] then echo ${map[$key]} ${key} fi done done