设计用于计算阵列中n个不同的二维点的算法

时间:2019-01-19 12:57:10

标签: java algorithm sorting

spring-boot-*

给出两个包含n个不同2D点的数组a和b,设计一种算法来计算两个数组中包含的点数 (难以理解代码) 我对此代码有多个疑问:

  1. 我们为什么要创建嵌套类?
  2. 为什么要在body中添加i和j,而不是在for语句中增加它们?
  3. main方法将如何创建两个Point数组的对象?

(如果有人对这个问题有更好的解决方案,我们将不胜感激。)

1 个答案:

答案 0 :(得分:0)

我使用C ++,但我可以回答您的问题。该算法还可以,但是您应该改写您的问题以反映您在理解该算法时遇到困难

1)为什么我们要创建嵌套类?
为避免类型冲突

[18-Jan-2019 11:16:10 America/Chicago] host: xxx.xxx.com.au
[18-Jan-2019 11:16:10 America/Chicago] username: xxxxxxx196
[18-Jan-2019 11:16:10 America/Chicago] pri: -----BEGIN OPENSSH PRIVATE KEY-----
[the contents are here]
-----END OPENSSH PRIVATE KEY-----

[18-Jan-2019 11:16:10 America/Chicago] pub: ssh-rsa AAAAB3Nz [the contents are here] cKeOzO7St 

[18-Jan-2019 11:16:10 America/Chicago] key: phpseclib\Crypt\RSA Object
(
    [zero] => phpseclib\Math\BigInteger Object
        (
            [value] => 0x
            [engine] => internal (64-bit, OpenSSL)
        )

    [one] => phpseclib\Math\BigInteger Object
        (
            [value] => 0x01
            [engine] => internal (64-bit, OpenSSL)
        )

    [privateKeyFormat] => 0
    [publicKeyFormat] => 7
    [modulus] => 
    [k] => 
    [exponent] => 
    [primes] => 
    [exponents] => 
    [coefficients] => 
    [hashName] => sha1
    [hash] => phpseclib\Crypt\Hash Object
        (
            [hashParam] => sha1
            [b] => 64
            [l] => 20
            [hash] => sha1
            [key] => 
            [computedKey] => 
            [opad] => 
            [ipad] => 
        )

    [hLen] => 20
    [sLen] => 
    [mgfHash] => phpseclib\Crypt\Hash Object
        (
            [hashParam] => sha1
            [b] => 64
            [l] => 20
            [hash] => sha1
            [key] => 
            [computedKey] => 
            [opad] => 
            [ipad] => 
        )

    [mgfHLen] => 20
    [encryptionMode] => 1
    [signatureMode] => 1
    [publicExponent] => 
    [password] => 
    [components] => Array
        (
        )

    [configFile] => /var/www/vagrant/.../vendor/phpseclib/phpseclib/phpseclib/Crypt/../openssl.cnf
    [comment] => 
)



[18-Jan-2019 11:16:13 America/Chicago] OH NOES! Login Failed



[18-Jan-2019 11:16:13 America/Chicago] LOGS: <pre><-
00000000  53:53:48:2d:32:2e:30:2d:4d:61:76:65:72:69:63:6b  SSH-2.0-Maverick
00000010  5f:53:53:48:44:0d:0a                             _SSHD..

->
00000000  53:53:48:2d:32:2e:30:2d:70:68:70:73:65:63:6c:69  SSH-2.0-phpsecli
00000010  62:5f:32:2e:30:20:28:6f:70:65:6e:73:73:6c:29:0d  b_2.0 (openssl).
00000020  0a                                               .

-> NET_SSH2_MSG_KEXINIT (since last: 1.1995, network: 0.0001s)
00000000  87:cd:92:30:d8:01:2e:6a:3b:11:04:2d:41:7e:31:07  ...0...j;..-A~1.
00000010  00:00:00:7e:64:69:66:66:69:65:2d:68:65:6c:6c:6d  ...~diffie-hellm
00000020  61:6e:2d:67:72:6f:75:70:31:2d:73:68:61:31:2c:64  an-group1-sha1,d
00000030  69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72  iffie-hellman-gr
00000040  6f:75:70:31:34:2d:73:68:61:31:2c:64:69:66:66:69  oup14-sha1,diffi
00000050  65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f:75:70:2d  e-hellman-group-
00000060  65:78:63:68:61:6e:67:65:2d:73:68:61:31:2c:64:69  exchange-sha1,di
00000070  66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f  ffie-hellman-gro
00000080  75:70:2d:65:78:63:68:61:6e:67:65:2d:73:68:61:32  up-exchange-sha2
00000090  35:36:00:00:00:29:72:73:61:2d:73:68:61:32:2d:32  56...)rsa-sha2-2
000000a0  35:36:2c:72:73:61:2d:73:68:61:32:2d:35:31:32:2c  56,rsa-sha2-512,
000000b0  73:73:68:2d:72:73:61:2c:73:73:68:2d:64:73:73:00  ssh-rsa,ssh-dss.
000000c0  00:00:e9:61:72:63:66:6f:75:72:32:35:36:2c:61:72  ...arcfour256,ar
000000d0  63:66:6f:75:72:31:32:38:2c:61:65:73:31:32:38:2d  cfour128,aes128-
000000e0  63:74:72:2c:61:65:73:31:39:32:2d:63:74:72:2c:61  ctr,aes192-ctr,a
000000f0  65:73:32:35:36:2d:63:74:72:2c:74:77:6f:66:69:73  es256-ctr,twofis
00000100  68:31:32:38:2d:63:74:72:2c:74:77:6f:66:69:73:68  h128-ctr,twofish
00000110  31:39:32:2d:63:74:72:2c:74:77:6f:66:69:73:68:32  192-ctr,twofish2
00000120  35:36:2d:63:74:72:2c:61:65:73:31:32:38:2d:63:62  56-ctr,aes128-cb
00000130  63:2c:61:65:73:31:39:32:2d:63:62:63:2c:61:65:73  c,aes192-cbc,aes
00000140  32:35:36:2d:63:62:63:2c:74:77:6f:66:69:73:68:31  256-cbc,twofish1
00000150  32:38:2d:63:62:63:2c:74:77:6f:66:69:73:68:31:39  28-cbc,twofish19
00000160  32:2d:63:62:63:2c:74:77:6f:66:69:73:68:32:35:36  2-cbc,twofish256
00000170  2d:63:62:63:2c:74:77:6f:66:69:73:68:2d:63:62:63  -cbc,twofish-cbc
00000180  2c:62:6c:6f:77:66:69:73:68:2d:63:74:72:2c:62:6c  ,blowfish-ctr,bl
00000190  6f:77:66:69:73:68:2d:63:62:63:2c:33:64:65:73:2d  owfish-cbc,3des-
000001a0  63:74:72:2c:33:64:65:73:2d:63:62:63:00:00:00:e9  ctr,3des-cbc....
000001b0  61:72:63:66:6f:75:72:32:35:36:2c:61:72:63:66:6f  arcfour256,arcfo
000001c0  75:72:31:32:38:2c:61:65:73:31:32:38:2d:63:74:72  ur128,aes128-ctr
000001d0  2c:61:65:73:31:39:32:2d:63:74:72:2c:61:65:73:32  ,aes192-ctr,aes2
000001e0  35:36:2d:63:74:72:2c:74:77:6f:66:69:73:68:31:32  56-ctr,twofish12
000001f0  38:2d:63:74:72:2c:74:77:6f:66:69:73:68:31:39:32  8-ctr,twofish192
00000200  2d:63:74:72:2c:74:77:6f:66:69:73:68:32:35:36:2d  -ctr,twofish256-
00000210  63:74:72:2c:61:65:73:31:32:38:2d:63:62:63:2c:61  ctr,aes128-cbc,a
00000220  65:73:31:39:32:2d:63:62:63:2c:61:65:73:32:35:36  es192-cbc,aes256
00000230  2d:63:62:63:2c:74:77:6f:66:69:73:68:31:32:38:2d  -cbc,twofish128-
00000240  63:62:63:2c:74:77:6f:66:69:73:68:31:39:32:2d:63  cbc,twofish192-c
00000250  62:63:2c:74:77:6f:66:69:73:68:32:35:36:2d:63:62  bc,twofish256-cb
00000260  63:2c:74:77:6f:66:69:73:68:2d:63:62:63:2c:62:6c  c,twofish-cbc,bl
00000270  6f:77:66:69:73:68:2d:63:74:72:2c:62:6c:6f:77:66  owfish-ctr,blowf
00000280  69:73:68:2d:63:62:63:2c:33:64:65:73:2d:63:74:72  ish-cbc,3des-ctr
00000290  2c:33:64:65:73:2d:63:62:63:00:00:00:39:68:6d:61  ,3des-cbc...9hma
000002a0  63:2d:73:68:61:32:2d:32:35:36:2c:68:6d:61:63:2d  c-sha2-256,hmac-
000002b0  73:68:61:31:2d:39:36:2c:68:6d:61:63:2d:73:68:61  sha1-96,hmac-sha
000002c0  31:2c:68:6d:61:63:2d:6d:64:35:2d:39:36:2c:68:6d  1,hmac-md5-96,hm
000002d0  61:63:2d:6d:64:35:00:00:00:39:68:6d:61:63:2d:73  ac-md5...9hmac-s
000002e0  68:61:32:2d:32:35:36:2c:68:6d:61:63:2d:73:68:61  ha2-256,hmac-sha
000002f0  31:2d:39:36:2c:68:6d:61:63:2d:73:68:61:31:2c:68  1-96,hmac-sha1,h
00000300  6d:61:63:2d:6d:64:35:2d:39:36:2c:68:6d:61:63:2d  mac-md5-96,hmac-
00000310  6d:64:35:00:00:00:04:6e:6f:6e:65:00:00:00:04:6e  md5....none....n
00000320  6f:6e:65:00:00:00:00:00:00:00:00:00:00:00:00:00  one.............

<- NET_SSH2_MSG_KEXINIT (since last: 0.0717, network: 0.0716s)
00000000  b4:b6:d9:00:14:cc:a9:46:e4:a6:82:00:62:fb:ea:90  .......F....b...
00000010  00:00:00:59:64:69:66:66:69:65:2d:68:65:6c:6c:6d  ...Ydiffie-hellm
00000020  61:6e:2d:67:72:6f:75:70:31:2d:73:68:61:31:2c:64  an-group1-sha1,d
00000030  69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72  iffie-hellman-gr
00000040  6f:75:70:2d:65:78:63:68:61:6e:67:65:2d:73:68:61  oup-exchange-sha
00000050  31:2c:64:69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e  1,diffie-hellman
00000060  2d:67:72:6f:75:70:31:34:2d:73:68:61:31:00:00:00  -group14-sha1...
00000070  07:73:73:68:2d:72:73:61:00:00:00:36:61:65:73:32  .ssh-rsa...6aes2
00000080  35:36:2d:63:62:63:2c:61:65:73:31:39:32:2d:63:62  56-cbc,aes192-cb
00000090  63:2c:61:65:73:31:32:38:2d:63:62:63:2c:33:64:65  c,aes128-cbc,3de
000000a0  73:2d:63:62:63:2c:62:6c:6f:77:66:69:73:68:2d:63  s-cbc,blowfish-c
000000b0  62:63:00:00:00:36:61:65:73:32:35:36:2d:63:62:63  bc...6aes256-cbc
000000c0  2c:61:65:73:31:39:32:2d:63:62:63:2c:61:65:73:31  ,aes192-cbc,aes1
000000d0  32:38:2d:63:62:63:2c:33:64:65:73:2d:63:62:63:2c  28-cbc,3des-cbc,
000000e0  62:6c:6f:77:66:69:73:68:2d:63:62:63:00:00:00:2b  blowfish-cbc...+
000000f0  68:6d:61:63:2d:73:68:61:31:2c:68:6d:61:63:2d:73  hmac-sha1,hmac-s
00000100  68:61:31:2d:39:36:2c:68:6d:61:63:2d:6d:64:35:2c  ha1-96,hmac-md5,
00000110  68:6d:61:63:2d:6d:64:35:2d:39:36:00:00:00:2b:68  hmac-md5-96...+h
00000120  6d:61:63:2d:73:68:61:31:2c:68:6d:61:63:2d:73:68  mac-sha1,hmac-sh
00000130  61:31:2d:39:36:2c:68:6d:61:63:2d:6d:64:35:2c:68  a1-96,hmac-md5,h
00000140  6d:61:63:2d:6d:64:35:2d:39:36:00:00:00:09:6e:6f  mac-md5-96....no
00000150  6e:65:2c:7a:6c:69:62:00:00:00:09:6e:6f:6e:65:2c  ne,zlib....none,
00000160  7a:6c:69:62:00:00:00:00:00:00:00:00:00:00:00:00  zlib............
00000170  00                                               .

-> NET_SSH2_MSG_KEXDH_INIT (since last: 0.0028, network: 0.0001s)
00000000  00:00:00:81:00:9f:25:52:1c:07:10:e0:0f:e6:37:53  ......%R......7S
00000010  db:93:e5:ab:f4:99:90:34:c7:97:21:25:34:c6:3c:fa  .......4..!%4...
00000020  34:5b:16:b6:78:fb:a9:82:0c:92:e0:09:ef:ef:a3:04  4[..x...........
00000030  33:11:7c:60:77:ad:c3:aa:24:3e:f4:99:4f:97:7a:95  3.|`w...$>..O.z.
00000040  7f:f6:0f:e9:0a:86:ea:ec:d0:76:53:6a:c6:c7:2a:fb  .........vSj..*.
00000050  7f:46:41:fb:ac:71:cc:3c:9c:d8:73:b3:94:f3:de:05  .FA..q....s.....
00000060  65:eb:fb:3c:78:16:0c:6e:99:3d:1c:a9:82:e2:ce:e0  e...x..n.=......
00000070  3b:83:9f:91:75:49:ba:e7:8a:60:36:3f:43:e9:9d:9d  ;...uI...`6?C...
00000080  0c:e8:4d:50:02                                   ..MP.

<- NET_SSH2_MSG_KEXDH_REPLY (since last: 0.8275, network: 0.8275s)
00000000  00:00:01:17:00:00:00:07:73:73:68:2d:72:73:61:00  ........ssh-rsa.
00000010  00:00:03:01:00:01:00:00:01:01:00:ce:c3:52:89:cb  .............R..
00000020  72:73:bd:de:e4:60:48:7d:34:25:a8:c8:48:64:a3:59  rs...`H}4%..Hd.Y
00000030  6c:3b:19:e0:52:e1:da:c8:c5:df:97:a9:5a:38:dd:ca  l;..R.......Z8..
00000040  da:34:92:9d:8d:89:87:83:c4:d2:77:0b:1d:83:c7:d5  .4........w.....
00000050  2d:46:78:14:32:65:09:ed:eb:1c:38:59:00:1f:0d:1f  -Fx.2e....8Y....
00000060  c0:56:08:8c:b2:79:f3:68:04:07:f5:fe:7d:26:fa:00  .V...y.h....}&..
00000070  41:ae:1c:b7:78:ad:cc:98:03:5f:e0:be:01:7a:b5:19  A...x...._...z..
00000080  05:e1:7d:3d:2a:22:fc:05:33:d3:71:77:d0:5a:92:4b  ..}=*"..3.qw.Z.K
00000090  e2:58:18:6a:70:eb:23:5d:38:4b:a1:9c:33:af:e4:67  .X.jp.#]8K..3..g
000000a0  b5:bc:eb:10:2f:15:9a:31:35:95:76:13:b5:fb:3e:6b  ..../..15.v...>k
000000b0  72:10:3a:2f:fa:7a:2a:bf:fb:29:75:a0:b1:0b:c7:7b  r.:/.z*..)u....{
000000c0  11:e0:58:e0:a9:2e:22:c0:fc:d6:94:df:30:56:5f:21  ..X...".....0V_!
000000d0  71:15:c8:fc:97:1c:47:43:f2:ed:7e:b4:b4:6f:32:02  q.....GC..~..o2.
000000e0  93:c0:49:16:dd:64:21:d3:14:8d:4e:9b:51:29:91:e3  ..I..d!...N.Q)..
000000f0  a5:89:a6:01:3d:6d:6f:61:ee:76:30:e9:fc:e0:b2:01  ....=moa.v0.....
00000100  43:4d:c4:ad:51:82:fb:ba:a7:2f:4d:00:64:85:57:7e  CM..Q..../M.d.W~
00000110  0b:7a:b7:2a:c1:e8:6d:4a:1a:ea:4d:00:00:00:80:45  .z.*..mJ..M....E
00000120  3a:db:d1:1c:1b:f7:ec:0a:46:1e:23:1b:61:37:6f:fa  :.......F.#.a7o.
00000130  2b:08:7f:1e:a1:50:9f:ab:ca:59:8d:a6:68:1b:9e:8e  +....P...Y..h...
00000140  4f:74:23:04:b7:46:34:d4:07:71:95:1d:69:d3:ec:74  Ot#..F4..q..i..t
00000150  2f:43:f3:66:e1:fe:8d:b6:cd:a6:f4:06:04:0c:d3:79  /C.f...........y
00000160  49:2f:0f:ca:1a:a4:6a:a0:6a:5b:af:cb:8b:82:46:ca  I/....j.j[....F.
00000170  08:77:9f:90:1c:3d:a3:73:20:ee:eb:36:29:56:32:0f  .w...=.s ..6)V2.
00000180  86:a7:6c:40:f1:14:ff:ff:6e:4b:84:eb:30:a9:a0:79  ..l@....nK..0..y
00000190  e0:5f:1c:e3:03:6f:0d:15:3f:33:27:e4:8c:4f:17:00  ._...o..?3'..O..
000001a0  00:01:0f:00:00:00:07:73:73:68:2d:72:73:61:00:00  .......ssh-rsa..
000001b0  01:00:16:69:d8:22:03:29:48:ba:3c:60:71:b9:88:6e  ...i.".)H..`q..n
000001c0  61:7b:52:fc:70:8a:90:d4:2b:6d:ef:4f:97:40:f1:89  a{R.p...+m.O.@..
000001d0  01:01:37:5d:32:52:e2:b3:3a:a3:61:01:b1:d8:40:53  ..7]2R..:.a...@S
000001e0  9c:f3:35:4d:6c:e5:88:40:91:42:6a:38:d1:3a:91:45  ..5Ml..@.Bj8.:.E
000001f0  9a:8f:7d:41:eb:9c:95:fc:58:d5:52:67:3d:da:8a:2b  ..}A....X.Rg=..+
00000200  7a:63:8c:93:43:dc:4c:f5:be:76:17:2c:67:c8:5b:be  zc..C.L..v.,g.[.
00000210  f2:64:0a:4f:a2:99:e7:94:d7:2d:97:cc:71:51:cb:25  .d.O.....-..qQ.%
00000220  fe:a9:5b:17:2a:ae:a5:c7:fa:6d:46:ab:15:00:7f:64  ..[.*....mF....d
00000230  e1:59:31:c6:fe:24:66:f9:04:77:2f:ba:c8:3c:de:87  .Y1..$f..w/.....
00000240  04:c0:06:08:4b:06:6e:29:4f:f0:28:d8:25:f9:8d:82  ....K.n)O.(.%...
00000250  e8:55:d7:5c:5a:31:df:01:96:d6:40:73:f4:c0:d2:fb  .U.\Z1....@s....
00000260  51:16:f2:7b:c0:1d:b8:ac:db:39:04:82:09:9c:48:cf  Q..{.....9....H.
00000270  f0:d8:c1:36:13:ce:e4:63:87:9f:82:5c:b8:22:d7:9c  ...6...c...\."..
00000280  fc:61:ba:20:c3:96:01:ba:ec:57:7a:73:59:96:27:a3  .a. .....WzsY.'.
00000290  37:bd:c9:72:cb:c2:c4:09:f9:7c:54:2d:fd:be:67:49  7..r.....|T-..gI
000002a0  3a:84:a7:28:ff:07:2a:3b:13:59:a1:08:3f:0d:34:85  :..(..*;.Y..?.4.
000002b0  4f:56                                            OV

-> NET_SSH2_MSG_NEWKEYS (since last: 0.0019, network: 0.0001s)


<- NET_SSH2_MSG_NEWKEYS (since last: 0.4071, network: 0.407s)


-> NET_SSH2_MSG_SERVICE_REQUEST (since last: 0.0006, network: 0.0001s)
00000000  00:00:00:0c:73:73:68:2d:75:73:65:72:61:75:74:68  ....ssh-userauth

<- NET_SSH2_MSG_SERVICE_ACCEPT (since last: 0.4098, network: 0.4097s)
00000000  00:00:00:0c:73:73:68:2d:75:73:65:72:61:75:74:68  ....ssh-userauth

</pre>
[18-Jan-2019 11:16:13 America/Chicago] ERRORS: Array
(
)

可以在没有+- IntersectionOfTwoSets (class) ------+ | | | | o- Point (class) | | | | | o- intersectionOf (function) | | | +--------------------------------------+ 的情况下实现,但是请注意,IntersectionOfTwoSets是一个非常通用的名称,可能已经在您打算添加到项目中的任何库中实现。在Point中实现Point使您的实现独一无二(C ++中称为IntersectionOfTwoSets的概念,认为使用该概念是良好的编程习惯)


  

标准的 for循环 语法为: namespace for ; < em> init ; condition

请注意,缺少循环的增量部分,而是在循环中找到它


2)如何在intersectionOf方法中实现i ++和j ++?
increment就是i++;

这是代码的简化版本

i += 1

让我们在一组整数上测试算法

given two sets a & b
sort a & b
initialize empty array (result)
loop (...)
| if (a[i] == b[j])
|  add a[i] to result, then increment i & j
| if (a[i] < b[j])
|  increment i
| if (b[j] < a[i])
|  increment j
| if (i >= a.size()) or (j >= b.size())
|  stop
return result

它也应该在一组点上起作用

3)主要方法将如何创建两个Point数组的对象?
在C ++中,语法为:

let a: [2, 1, 10, 9]
let b: [1, 5, 2, 7, 6]

let result: []

Arrays.sort(a); // a: [1, 2, 9, 10]
Arrays.sort(b); // b: [1, 2, 5, 6, 7]

loop(...)
| 1: add 1 to result, increment i & j
| 2: add 2 to result, increment i & j
| 3: (j == 2) increment only j (5 < 9)
| 4: (j == 3) increment only j (6 < 9)
| 5: (j == 4) increment only j (7 < 9)
| 6: (j == 5) stop because j >= b.size()

return result // [1, 2]

但是在Java中,我几乎可以确定是:

IntersectionOfTwoSets::Point a[n], b[n];
or
List<IntersectionOfTwoSets::Point> a, b;