为什么Java中数组是协变的而泛型不是?
什么是协变?
Effective Java中写到:
1.数组和泛型有两个主要的不同点。第一个是数组是协变的(covariant),而泛型是不变的(invariant)。
2.协变简单来说就是如果X
是Y
的子类型则X[]
也是 Y[]
的子类型。因为数组是协变的,String
是Object
的子类型,故:
ICPC Asia Urumqi 2017 B.The Difference
Alice was always good at math. Her only weak points were multiplication and subtraction. To help her with that, Bob presented her with the following problem.
He gave her four positive integers. Alice can change their order optionally. Her task is to find an order, denoted by A1,A2,A3
and A4
, with the maximum value of A1×A2 − A3×A4
.
Input
The input contains several test cases and the first line provides an integer t(1 ≤ t ≤ 100)
indicating the number of cases.
ICPC Asia Urumqi 2017 G.The Mountain
All as we know, a mountain is a large landform that stretches above the surrounding land in a limited area. If we as the tourists take a picture of a distant mountain and print it out, the image on the surface of paper will be in the shape of a particular polygon.
From mathematics angle we can describe the range of the mountain in the picture as a list of distinct points, denoted by (x1,y1)
to (xn,yn)
. The first point is at the original point of the coordinate system and the last point is lying on the x-axis
. All points else have positive y
coordinates and incremental x
coordinates. Specifically, all x
coordinates satisfy 0=x1<x2<x3<...<xn
. All y
coordinates are positive except the first and the last points whose y
coordinates are zeroes.
Leecode.1.Two Sum (两数之和)
题目描述:
LeetCode第一题,两数之和
Given an array of integers, return
indices
of the two numbers such that they add up to a specific target.You may assume that each input would have
exactly
one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
…
LeetCode-1128-Number of Equivalent Domino Pairs(等价多米诺骨牌对的数量)
题目描述:
Given a list of
dominoes
,dominoes[i] = [a, b]
is equivalent todominoes[j] = [c, d]
if and only if either(a==c and b==d)
, or(a==d and b==c)
- that is, one domino can be rotated to be equal to another domino.Return the number of pairs
(i, j)
for which0 <= i < j < dominoes.length
, anddominoes[i]
is equivalent todominoes[j]
.
Example 1:
Input: dominoes = [[1,2],[2,1],[3,4],[5,6]]
Output: 1
Constraints:
…Java中的二进制
Java SE 7之后可以使用二进制来定义byte,short,int,long
这些整型变量。要使用二进制来表示整形,需要在数字开头带上0b
或者0B
,如下所示: