2019/11/02【搜索插入位置】&【有效的數獨】

題目一:【搜索插入位置】力扣——35

給定一個排序數組和一個目標值,在數組中找到目標值,並返回其索引。如果目標值不存在於數組中,返回它將會被按順序插入的位置。

你可以假設數組中無重複元素。

示例 1:

輸入: [1,3,5,6], 5
輸出: 2
示例 2:

輸入: [1,3,5,6], 2
輸出: 1
示例 3:

輸入: [1,3,5,6], 7
輸出: 4
示例 4:

輸入: [1,3,5,6], 0
輸出: 0

#!/bin/bash
#搜索插入位置
#author:yzt 2019-11-02
#
read -t 30 -p "請輸入一個排序數組:" nums
read -t 10 -p "請輸入一個目標值:" target
echo "$nums"|sed 's#\[\|\]##g'|sed 's#,#\n#g' >tmp.txt
count1=`cat tmp.txt|wc -l`
count2=`grep -n "$target" tmp.txt|head -n1`
flag=false
if [ -z $count2 ];then
        for i in `cat tmp.txt`
        do
                if [ $i -gt $target ];then
                        flag=true
                        point=`grep -n "$i" tmp.txt|head -n1|awk -F : '{print $1}'`
                        point=$[$point-1]
                        echo "$point"
                        exit 0
                fi
        done
        if [ $flag = false ];then
                echo "$count1"
        fi
else
        point=`echo "$count2"|awk -F : '{print $1}'`
        echo "$point"
fi

腳本邏輯:

1、此需求分爲如下三種情況:

第一:目標值包含在數組中,則直接使用grep -n導出對應數字的行數即可

第二:目標值不在數組中且比數組中的元素要小,此時直接輸出0即可

第三:目標值不在數組中且比數組中的元素要大,此時輸出數組元素個數即可

腳本效果:

[root@localhost leetcode]# ./sousuocharuweizhi.sh
請輸入一個排序數組:[2,3,4]
請輸入一個目標值:1
0
[root@localhost leetcode]# ./sousuocharuweizhi.sh
請輸入一個排序數組:[2,3,4]
請輸入一個目標值:5
3
[root@localhost leetcode]# ./sousuocharuweizhi.sh
請輸入一個排序數組:[2,3,4]
請輸入一個目標值:3
2
[root@localhost leetcode]# ./sousuocharuweizhi.sh
請輸入一個排序數組:[2,3,5,6]
請輸入一個目標值:4
2

 

題目二:【有效的數獨】

判斷一個 9x9 的數獨是否有效。只需要根據以下規則,驗證已經填入的數字是否有效即可。

數字 1-9 在每一行只能出現一次。
數字 1-9 在每一列只能出現一次。
數字 1-9 在每一個以粗實線分隔的 3x3 宮內只能出現一次。


上圖是一個部分填充的有效的數獨。

數獨部分空格內已填入了數字,空白格用 '.' 表示。

示例 1:

輸入:
[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
輸出: true
示例 2:

輸入:
[
  ["8","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
輸出: false
解釋: 除了第一行的第一個數字從 5 改爲 8 以外,空格內其他數字均與 示例1 相同。
     但由於位於左上角的 3x3 宮內有兩個 8 存在, 因此這個數獨是無效的。
說明:

一個有效的數獨(部分已被填充)不一定是可解的。
只需要根據以上規則,驗證已經填入的數字是否有效即可。
給定數獨序列只包含數字 1-9 和字符 '.' 。
給定數獨永遠是 9x9 形式的。

 

#!/bin/bash
#有效的數獨
#author:yzt 2019-11-02
#
for i in `cat tmp.txt`
do
        count1=`echo "$i"|sed 's#\[\|\]##g'|sed 's#"\|\.##g'|sed 's#,#\n#g'|grep -v "^$" |sort |uniq -c|awk '$1>1{print $2}'|head -n1`
        if [ ! -z $count1 ];then
                echo "false"
                exit 1
        fi
done
for((aa=1;aa<10;aa++))
do
        count2=`cat tmp.txt |sed 's#\[\|\]##g'|sed 's#"\|\.##g'|awk -F, -v j=$aa '{print $j}' |grep -v "^$"|sort|uniq -c|awk '$1>1{print $2}'|head -n1`
        if [ ! -z $count2 ];then
                echo "false"
                exit 2
        fi
done
fenxi(){
for((bb=1;bb<9;bb=bb+3))
do
        cc=$[$bb+1]
        dd=$[$cc+1]
        content=`cat $1|sed 's#\[\|\]##g'|sed 's#"\|\.##g'|awk -F , -v i=$bb -v j=$cc -v k=$dd '{print $i,$j,$k}'`
        for i in  $content
        do
                echo "$i"
        done > tmp3.txt
        count3=`cat tmp3.txt |grep -v "^$"| sort |uniq -c|awk '$1>1{print $2}'`
        if [ ! -z $count3 ];then
                echo "false"
                exit 3
        fi
done
}

cat /dev/null > tmp2.txt
for((ii=1;ii<10;ii=ii+3))
do
        jj=$[$ii+2]
        sed -n "$ii,$jj p" tmp.txt > tmp2.txt
        fenxi tmp2.txt
done
echo "true"

 腳本邏輯:

1、行判斷:對每一行的內容進行判斷,前後使用grep -v 去掉空行,使用sort排序,使用uniq獲取重複行,最後使用awk獲取重複行

2、列判斷,使用awk獲取文檔的每一列,接着判斷是否有重複數

3、小正方形判斷:先使用sed獲取三行;接着使用for循環中搭配awk獲取三列內容就能遍歷所有的數獨;最後也是使用一樣的方式進行判斷

 

腳本效果:

[root@localhost leetcode]# ./youxiaodeshudu.sh
true
[root@localhost leetcode]# cat tmp.txt
["5","3",".",".","7",".",".",".","."]
["6",".",".","1","9","5",".",".","."]
[".","9","8",".",".",".",".","6","."]
["8",".",".",".","6",".",".",".","3"]
["4",".",".","8",".","3",".",".","1"]
["7",".",".",".","2",".",".",".","6"]
[".","6",".",".",".",".","2","8","."]
[".",".",".","4","1","9",".",".","5"]
[".",".",".",".","8",".",".","7","9"]
[root@localhost leetcode]# ./youxiaodeshudu.sh
false
[root@localhost leetcode]# cat tmp.txt
["5","3","3",".","7",".",".",".","."]
["6",".",".","1","9","5",".",".","."]
[".","9","8",".",".",".",".","6","."]
["8",".",".",".","6",".",".",".","3"]
["4",".",".","8",".","3",".",".","1"]
["7",".",".",".","2",".",".",".","6"]
[".","6",".",".",".",".","2","8","."]
[".",".",".","4","1","9",".",".","5"]
[".",".",".",".","8",".",".","7","9"]
[root@localhost leetcode]# ./youxiaodeshudu.sh
false
[root@localhost leetcode]# cat tmp.txt
["5","3",".",".","7",".",".",".","."]
["6",".",".","1","9","5",".",".","."]
[".","9","8",".",".",".",".","6","."]
["8",".",".",".","6",".",".",".","3"]
["4",".",".","8",".","3",".",".","1"]
["7",".",".",".","2",".",".",".","6"]
[".","6",".",".",".",".","2","8","."]
[".",".","6","4","1","9",".",".","5"]
[".",".",".",".","8",".",".","7","9"]

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章