思路:可以选择直接调用库函数来对序列进行排序,当然我们也可以手写排序,这里用的库函数
class Solution {
public int[] sortArray(int[] nums) {
Arrays.sort(nums);
return nums;
}
}
思路:可以数组2加入到数组1中,然后对数组1排序,这里直接使用库函数,哈哈 ,有空余时间再写其他的解
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
System.arraycopy(nums2, 0, nums1, m, n);
Arrays.sort(nums1);
}
}
思路:利用叉乘计算三点共线,两个向量,叉乘结果为0,代表夹角为0,算出向量v1和v2,然后求交叉乘,判断是否为0即可。
class Solution {
public boolean isBoomerang(int[][] points) {
int[] v1 = {points[1][0] - points[0][0], points[1][1] - points[0][1]};
int[] v2 = {points[2][0] - points[0][0], points[2][1] - points[0][1]};
return v1[0] * v2[1] - v1[1] * v2[0] != 0;
}
}
思路:求出第1个点与第2个点之间的斜率 求第3....n个点与第一个点之间的斜率 与上题一样
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
int x1 =coordinates[1][0]-coordinates[0][0];
int y1 =coordinates[1][1]-coordinates[0][1];
for (int i = 2; i < coordinates.length; i++) {
int x2 =coordinates[i][0]-coordinates[0][0];
int y2 =coordinates[i][1]-coordinates[0][1];
if (x1 * y2 != x2 * y1) {
return false;
}
}
return true;
}
}