目录
LintCode刷题:
·判断两个数组是否相等
判断长度后,遍历集合直接判断相不相等即可
import java.util.*;
public class Solution {
public boolean equalityOfArrays(int[] arr1, int arr2[]) {
Arrays.sort(arr1);
Arrays.sort(arr2);
if (arr1.length!=arr2.length)
return false;
else for (int i=0;i<arr1.length;i++){
if (arr1[i]!=arr2[i])
return false;
}
return true;
}
}
·寻找缺少的数字
首先算出1-n所有数的总数和,再建立个循环,将每次输入的数减去即可
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum=(n+1)*n/2;
int[] arr = new int[n-1];
for (int i = 0; i <n - 1; i++) {
arr[i]=scanner.nextInt();
sum-=arr[i];
}
System.out.println(sum);
}
}
·命中率评级
else if 判断语句
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int score=scanner.nextInt();
if (score>=90){
System.out.println('A');
}else if (score>=80){
System.out.println('B');
}else if (score>=70){
System.out.println('C');
}else if (score>=60){
System.out.println('D');
}else {
System.out.println('E');
}
}
}
·打印书籍信息
多态的调用
public class Main {
public static void main(String[] args) {
IBook book=new BookImpl();
book.getBook("Gone with the Wind","paper");
}
}
public class BookImpl implements IBook {
@Override
public void getBook(String bookName, String typeName) {
System.out.print("This book name is \"" + bookName + "\", type is " +
typeName + ".");
}
}
public interface IBook {
void getBook(String bookName, String typeName);
}
·计算 1+2+3+4+....n的值
高斯定律求和即可
public class Solution {
public int add(int number) {
int sum = 0;
sum=number*(number+1)/2;
return sum;
}
}
·找寻比周围都大的数
(有疑惑)第一种是创建二维数组,将八个邻居的位置按照二维数组放入,在每次遍历二维数组循环的时候,初始化一个isLargestNumber为ture,先定义一个函数,判断传入的两个邻居是不是在这个数组范围内,然后再将八个邻居一一与自身相比,当邻居大于等于自己时,把这个isLargestNumber赋值为false,然后按照isLargesNumber来赋值,false则为0,ture则为1。
疑惑->他是不停的给grid[i][j]赋值,那么grid[i][j]的值不应该是根据最后一个邻居的大小来决定的吗?
public class Solution {
int[][] DIRECTIONS = {{0, 1},{1, 0},{0, -1},{-1, 0},{1, 1},{1, -1},{-1, 1},{-1, -1}};
public int[][] highpoints(int[][] grid) {
if (grid == null || grid.length == 0) {
return null;
}
int n = grid.length;
int m = grid[0].length;
int[][] resultArr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
boolean isLargestNumber = true;
for (int k = 0; k < 8; k++) {
int neighborI = i + DIRECTIONS[k][0];
int neighborJ = j + DIRECTIONS[k][1];
if (!isValid(neighborI, neighborJ, grid)) {
continue;
}
if (grid[i][j] <= grid[neighborI][neighborJ]) {
isLargestNumber = false;
}
if (isLargestNumber) {
resultArr[i][j] = 1;
}
else {
resultArr[i][j] = 0;
}
}
}
}
return resultArr;
}
private boolean isValid(int x, int y, int[][] grid) {
if (x < 0 || x >= grid.length) {
return false;
}
if (y < 0 || y >= grid[0].length) {
return false;
}
return true;
}
}
第二种定义一个函数传入自身下标,来判断自己的邻居大不大于自己,单凡有一个就会反回false,在判断方法是为了便于判断,再加入一个新函数可以根据下标获取数值的函数,当下标不在的时候才可以返回一个integer里面最小的数,再用这个函数来比较本身和邻居即可。
public class Solution {
private int[][] grid;
private int n;
private int m;
public int[][] highpoints(int[][] grid) {
if (grid == null || grid.length == 0) {
return null;
}
this.grid = grid;
this.m = grid.length;
this.n = grid[0].length;
int[][] resultarr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
boolean isBigger = isLargesNumber(i, j);
if (isBigger) {
resultarr[i][j] = 1;
} else {
resultarr[i][j] = 0;
}
}
}
return resultarr;
}
public int getValue(int i,int j){
if (i < 0 || i >= n || j < 0 || j >= m) {
return Integer.MIN_VALUE;
}else
return grid[i][j];
}
public boolean isLargesNumber(int i,int j){
if (grid[i][j]<=getValue(i-1,j-1)){
return false;
}
if (grid[i][j]<=getValue(i,j-1)){
return false;
}
if (grid[i][j]<=getValue(i+1,j-1)){
return false;
}
if (grid[i][j]<=getValue(i,j+1)){
return false;
}
if (grid[i][j]<=getValue(i+1,j)){
return false;
}
if (grid[i][j]<=getValue(i-1,j)){
return false;
}
if (grid[i][j]<=getValue(i-1,j+1)){
return false;
}
if (grid[i][j]<=getValue(i+1,j+1)){
return false;
}
else
return true;
}
public static void main(String[] args) {
Solution solution=new Solution();
int[][] DIRECTIONS={{1,2,3,4},
{1,1,1,1},
{0,0,0,0},
{0,0,0,9}};
DIRECTIONS=solution.highpoints(DIRECTIONS);
for (int i=0;i<DIRECTIONS.length;i++){
for (int j=0;j<DIRECTIONS[0].length;j++){
System.out.print(DIRECTIONS[i][j]+",");
}
System.out.println();
}
}
}
学习时间:
2021-8-24 9:00-11:45、13:30-17:00
学习产出:
调试项目
刷题*6
学习博客*1