学习内容;复习六七八章 学习九,十章,十二章。
树集合练习;
import java.util.HashSet;
import java.util.Random;
import java.util.TreeMap;
import java.util.TreeSet;
public class eig {
public static void main(String[] args) {
Random c=new Random();
HashSet<Integer> A=new HashSet<Integer>();
for (int b=0;b<10;b++){
A.add(c.nextInt(99));
}
TreeSet<Integer>B=new TreeSet<Integer>(A);
System.out.println(B);
}
}
ArrayList练习;
public class five {
private static Object List;
public static void main(String[] args) {
ss jj=new ss();
for(int b=1;b<=10;b++){
jj.input(b);
}
jj.remove(4);
System.out.println(jj.aa);
ArrayList<String>qq=new ArrayList<String>();
qq.add("啦啦啦");
qq.add("哈哈哈");
qq.add("嘤嘤嘤");
qq.add("哦哦哦");
Collections.shuffle(qq);
System.out.println(qq.get(3));
}
TreeMap练习;
import java.io.*;
import java.nio.Buffer;
import java.util.Map;
import java.util.TreeMap;
public class ys {
public static void main(String[] args)throws Exception {
TreeMap<String,Integer>nb=new TreeMap<String , Integer>();
nb.put("啦啦啦",99);
nb.put("嗡嗡嗡",90);
nb.put("哈哈哈",44);
System.out.println(nb);
泛型练习;
class fx2<U>{
U j;
public U dd(U jj){
j=jj;
return j;
}
public void cc(fx2<? extends String> a){
System.out.println("值为"+a.j);
}
}
public class Fx {
public static void main(String[] args) {
// fx <Integer>abc=new fx<Integer>();
fx2 <String>aab=new fx2<String>();
// System.out.println(abc.bb(888));
aab.dd("玛卡巴卡");
aab.cc(aab);
}
给定一个矩阵 A, 返回 A 的转置矩阵。
矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引。
示例 1:
输入:[[1,2,3],[4,5,6],[7,8,9]]
输出:[[1,4,7],[2,5,8],[3,6,9]]
示例 2:
输入:[[1,2,3],[4,5,6]]
输出:[[1,4],[2,5],[3,6]]
class Solution {
public int[][] transpose(int[][] A) {
int[][] target = new int[A[0].length][A.length];
for (int i = 0; i < A[0].length; i++) {
for (int j = 0; j < A.length; j++) {
target[i][j] = A[j][i];
}
}
return target;
}
}
数组中占比超过一半的元素称之为主要元素。给定一个整数数组,找到它的主要元素。若没有,返回-1。
示例 1:
输入:[1,2,5,9,5,9,5,5,5]
输出:5
示例 2:
输入:[3,2]
输出:-1
示例 3:
输入:[2,2,1,1,1,2,2]
输出:2
class Solution {
public int majorityElement(int[] nums) {
for(int a=0;a<nums.length;a++){
int b=0;
for(int c=0;c<nums.length;c++){
if(nums[a]==nums[c]){
b++;
}
}
if(b>nums.length/2){
return nums[a];
}
}
return -1;
}
}