##题目描述
Description:
Snail Sort
Given an n x n
array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.
array = [[1,2,3],
[4,5,6],
[7,8,9]]
snail(array) #=> [1,2,3,6,9,8,7,4,5]
For better understanding, please follow the numbers of the next array consecutively:
array = [[1,2,3],
[8,9,4],
[7,6,5]]
snail(array) #=> [1,2,3,4,5,6,7,8,9]
This image will illustrate things more clearly:
NOTE: The idea is not sort the elements from the lowest value to the highest; the idea is to traverse the 2-d array in a clockwise snailshell pattern.
NOTE 2: The 0x0 (empty matrix) is represented as [[]]我好像最后还是没考虑空矩阵的处理。。。啊啊啊啊,路漫漫啊
1 helper = PaginationHelper([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘], 4) 2 helper.page_count # should == 2 3 helper.item_count # should == 6 4 helper.page_item_count(0) # should == 4 5 helper.page_item_count(1) # last page - should == 2 6 helper.page_item_count(2) # should == -1 since the page is invalid 7 8 # page_ndex takes an item index and returns the page that it belongs on 9 helper.page_index(5) # should == 1 (zero based index) 10 helper.page_index(2) # should == 0 11 helper.page_index(20) # should == -1 12 helper.page_index(-10) # should == -1 because negative indexes are invalid
##思路分析
突然给我来了道4kyu....写了半天写了一长串,结果答案里别人一句就写完了。。。一句。。我的内心是崩溃的
##代码解析
Python,先贴别人的以便学习
1 #方案1 2 def snail(array): 3 return list(array[0]) + snail(zip(*array[1:])[::-1]) if array else []
1 #方案2 2 def snail(array): 3 ret = [] 4 if array and array[0]: 5 size = len(array) 6 for n in xrange((size + 1) // 2): 7 for x in xrange(n, size - n): 8 ret.append(array[n][x]) 9 for y in xrange(1 + n, size - n): 10 ret.append(array[y][-1 - n]) 11 for x in xrange(2 + n, size - n + 1): 12 ret.append(array[-1 - n][-x]) 13 for y in xrange(2 + n, size - n): 14 ret.append(array[-y][n]) 15 return ret
#方案3 def snail(array): a = [] while array: a.extend(list(array.pop(0))) array = zip(*array) array.reverse() return a
下面是我自己的。。。完全没用到 python 高效的东西,只用了最最基本的函数,太弱智了
1 #为方便本地调试,加了很多 print,提交的时候需要注释掉 2 def snail(array): 3 ans = [] 4 if len(array) == 1: # preven "index out of range"!!! 5 ans = array[0] 6 #print ans 7 return ans 8 len_row = len(array) - 1 9 len_col = len(array[1]) - 1 10 all = (len_row + 1) * (len_col +1 ) 11 #print len_col 12 over_row = 0 13 over_col = 0 14 while over_col <= len_col/2: 15 row = over_row # current row 16 col = over_col 17 while over_col <= col < len_col - over_col: 18 now = array[row][col] 19 ans.append(now) 20 col += 1 21 #print ‘now == ‘, now 22 #print ‘this row over, col ==‘, col 23 24 while over_row <= row < len_row - over_row : 25 now = array[row][col] 26 ans.append(now) 27 row += 1 28 #print ‘now == ‘, now 29 #print ‘this col over, row ==‘, row 30 31 while over_col < col <= len_col - over_col: 32 now = array[row][col] 33 ans.append(now) 34 col -= 1 35 #print ‘now == ‘, now 36 #print ‘this reverse row over, col ==‘, col 37 38 while over_row < row <= len_row - over_row: 39 now = array[row][col] 40 ans.append(now) 41 row -= 1 42 #print ‘now == ‘, now 43 #print ‘this reverse col over, col ==‘, row 44 45 over_row += 1 46 over_col += 1 47 #print ‘print over_row == ‘, over_row, ‘print over_col == ‘, over_col 48 if len(ans) < all: 49 last = int(len_row/2) 50 ans.append(array[last][last]) 51 #print ans 52 return ans
时间: 2024-10-14 00:54:54