今天在MSDN論壇中看到這樣的一個問題,覺得非常鍛煉思維能力,所以這裡記錄下來作為備份,題目的要求是這樣的:
假設有一組字符串數組{"0","0","1","2","3",“0”,"4","0","0"},如何查找使0等於離它最近的且不為0的元素,如果離它最近的不為0的元素有兩個,則等於上一個元素,即想得到重新賦值後這樣的數組{"1","1","1","2","3",“3”,"4","4","4"}
這裡的實現思路摘自論壇中 zjyh16的回復,實現思路為:
具體實現代碼為:
[] s = String[] { , , , , , , , , [] result = [ ( i = ; i < s.Length; i++ (s[i] != = ( j = ; j < s.Length; j++ (i - j >= (s[i - j] != = s[i - (i + j < (s[i + j] != = s[i + ( i = ; i < result.Length; i++