File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* ==============================================================================
2
+ * 功能描述:TwoSum
3
+ * 创 建 者:gz
4
+ * 创建日期:2017/4/28 18:20:39
5
+ * ==============================================================================*/
6
+ using System ;
7
+ using System . Collections . Generic ;
8
+ using System . Linq ;
9
+ using System . Text ;
10
+
11
+ namespace HashTable . Lib
12
+ {
13
+ //Given an array of integers, return indices of the two numbers such that they add up to a specific target.
14
+
15
+ //You may assume that each input would have exactly one solution, and you may not use the same element twice.
16
+ /// <summary>
17
+ /// TwoSum
18
+ /// </summary>
19
+ public class TwoSumSln
20
+ {
21
+
22
+ public int LandPerimeter ( int [ , ] grid )
23
+ {
24
+ int rtn = 0 ;
25
+ for ( int i = 0 ; i <= grid . GetUpperBound ( 0 ) ; i ++ )
26
+ {
27
+ for ( int j = 0 ; j <= grid . GetUpperBound ( 1 ) ; j ++ )
28
+ {
29
+ if ( grid [ i , j ] == 1 )
30
+ rtn += 4 - surroundingIsland ( grid , i , j ) ;
31
+ }
32
+ }
33
+ return rtn ;
34
+ }
35
+
36
+ //获取某个网格周围的网格数([0,4])
37
+ //noy: y向网格索引(行)
38
+ //nox:x向网格索引(列)
39
+ private int surroundingIsland ( int [ , ] grid , int noy , int nox )
40
+ {
41
+ int rtnCnt = 0 ;
42
+ if ( nox > 0 )
43
+ {
44
+ if ( grid [ noy , nox - 1 ] == 1 )
45
+ rtnCnt ++ ;
46
+ }
47
+ if ( nox < grid . GetUpperBound ( 1 ) ) //nox小于(列)最大索引吗
48
+ {
49
+ if ( grid [ noy , nox + 1 ] == 1 )
50
+ rtnCnt ++ ;
51
+ }
52
+ if ( noy > 0 )
53
+ {
54
+ if ( grid [ noy - 1 , nox ] == 1 )
55
+ rtnCnt ++ ;
56
+ }
57
+ if ( noy < grid . GetUpperBound ( 0 ) ) //noy小于最大(行)索引吗
58
+ {
59
+ if ( grid [ noy + 1 , nox ] == 1 )
60
+ rtnCnt ++ ;
61
+ }
62
+ return rtnCnt ;
63
+ }
64
+
65
+ }
66
+ }
You can’t perform that action at this time.
0 commit comments