Code Monkey home page Code Monkey logo

blog-java-ml's People

Contributors

seasons123 avatar

Watchers

 avatar  avatar

blog-java-ml's Issues

输入任意行,每行输入两个数字,计算每行的和,并在最后输出

package a;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class a{
    public static void main(String[]args){    
    	 Scanner c=new Scanner(System.in);
         List<String> lista=new ArrayList<String> ();
         do{
           String str=c.nextLine();
           if(str.equals("")){
             break;
           }
           lista.add(str);
         }while(c.hasNextLine());

         for(int i=0;i<lista.size();i++){
            char[] chara=lista.get(i).toCharArray();
            int sum=Integer.valueOf(chara[0])-48+Integer.valueOf(chara[2])-48;
            //int sum=(int)chara[0]-48+(int)chara[2]-48;  也可以用这个方法
            System.out.println(sum);
         }
         
    }
}

java实现输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

package JingDian;

import java.util.Scanner;


public class charKind 
{
  
public static void main(String[] args)
   {
    
       System.out.println("请输入字符串:");
       Scanner sc = new Scanner(System.in);
       //注:Scanner类中的next()方法遇到空格就不进去了,比如说输入haha nene就只会得到haha,
      空格后面的都被忽略了,因此要用nextLine()
       String str = sc.nextLine();
       //用来检测中文的正则表达式。
       //[\u4e00-\u9fa5]这两个unicode值正好是Unicode表中的汉字的头和尾。
       String reg1 = "[\u4e00-\u9fa5]";
       int count1 = 0;
       //用来检测字母的正则表达式
       String reg2 = "[a-zA-Z]";
       int count2 = 0;
       //用于统计空格数
       int count3 = 0;
       
       //用于统计数字个数
       String reg4 = "[0-9]";
       int count4 = 0;
       //获得的键盘输入都是String,因为要 将string中的每个字符进行匹配所以将每个char都存入String数组中
       char[] charArr = str.toCharArray();
       String[] strArr = new String[charArr.length];
       for(int i=0;i<charArr.length;i++)
       {
           strArr[i] =String.valueOf(charArr[i]) ;
           if(strArr[i].matches(reg1))
           {
               count1++;
           }
           if(strArr[i].matches(reg2))
           {
               count2++;
           }
           if(strArr[i].matches(" "))
           {
               count3++;
           }
           if(strArr[i].matches(reg4))
           {
               count4++;
           }
       }
       System.out.println("汉字的个数:"+count1);
       System.out.println("字母的个数:"+count2);
       System.out.println("空格的个数:"+count3);
       System.out.println("数字的个数:"+count4);
   }
}

oracle BST

一、java:
(1)
java poll函数的作用
assert函数
(2)下面是编程中常用的(基础中的基础)
判断一个元素是否在list中 用contains
Java 中int、String的类型转换
list 如何清空数据 :使用list.clear()方法清空集合,释放内存
Java中List转换为数组,数组转List
java创建int数组
(3)java如何删除掉数组中的某个元素
比如一个数组:

String arrays={"1","2","3","5",“6”};
for(String s:arrays){
   if(s.equals("2")){
        //移除掉元素2
   }
}
String str = dianjia2,wang,dianjia;
String str1 = dianjia2,dianjia;
如上面两个字符串,如何根据str1的字段来删除str中相同的字段,最后得到的结果是wang.
解决代码:
public static void main(String[] args){
        String[] a = new String[]{"1","5","3","7"};
        String[] b = new String[]{"2","3","4"};
        String[] arrResult = arrContrast(a, b);       
        for (String strResult : arrResult) {
            System.out.println("最后的结果:----------->" + strResult);
        }
    }
    
    //处理数组字符
    private static String[] arrContrast(String[] arr1, String[] arr2){
        List<String> list = new LinkedList<String>();
        for (String str : arr1) {                //处理第一个数组,list里面的值为1,5,3,7
            if (!list.contains(str)) {
                list.add(str);
            }
        }
        for (String str : arr2) {      //如果第二个数组存在和第一个数组相同的值,就删除
            if(list.contains(str)){
                list.remove(str);
            }
        }
        String[] result = {};   //创建空数组
        return list.toArray(result);    //List to Array
    }

二、Linux
linux netstat utility
Sound Juicer
GNOME
命令行中出现<
创建一个文件

三、数据库
数据库select是那个阶段 DDL
数据库set语句哪个最高效
查询语句可以查空格么
关于sql中求平均值出现null值的解决方案
sql中except、intersect用法
select by condition
MySQL 子查询(subquery)语法与用法实例
数据库内连接与外连接

已知前序后序遍历,求中序遍历

已知前序后序遍历,求中序遍历:

这种情况并不能完全的求出所有情况的二叉树中序遍历。因为由前序和后序并不能完全确定一颗树。但是,如果已知是满二叉树或者完全二叉树的话,我想是可以的。

前序:中左右 后序:左右中
思路是这样的:     前序的第一个节点和后序的最后一个节点一定是根节点,
    如果前序的第二个节点与后序的倒数第二个节点不相等,那么前序的第二节点是根的左子树,后序的倒数第二节点是根的右子树    (但是 如果前序的第二个节点与后序的倒数第二个节点相等,那就不能确定到底是左子树还是右子树了,因为缺省一个子树中左与左中,中右与右中都会相等。所以这就需要题目有要求该树的特征了)    然后再后序中找到左子树根节点,在后序中从左子树根节点结合前序左子树根节点开始递归上述得到根节点左子树的子树 同样在前序中找到右子树根节点,在前序中从右子树根节点结合后序右子树根节点开始递归上述得到根节点右子树的子树

将整数转换成二进制的java小程序

首先我们知道,将整数转换成二进制是将整数除二取余将最后得到的余数从下向上写,组成得到的二进制数。
image

public class erjinzhi {
	public static void main(String[] args){
		int a=42;
		String str="";
		while(a>0){
		    str=a%2+str;
		    a=(int)(a/2);
		}
		System.out.println(str);
	}
}

输出:101010

一道2018美团java编程题

题目:在一个网页中有很多超链,在没点击时都是蓝色,点击之后都变为紫色。现在模仿这样一个过程:首先输入一个整数n(表示要输入多少个超链),然后依次输入n个超级链接的名字(字符串类型)。然后再输入一个整数,再输入已经点击的超链的名字(可以重复)。如下图所示:
image

统计第一个空字符前面的字符长度(java实现)

package JingDian;

public class diguichar {
    public static void main(String[] args){
        String[] buf = {"a","b","c","d","e","f","\0","x","y","z"};
        mystrlen k = new mystrlen();
        System.out.println(k.strlen(buf, 2));
        
    }
}

class mystrlen{
    public int strlen(String[] buf,int N){ 
        for(int i=0;i<buf.length;i++){
        if(buf[0]=="\0"||N==0)
            return 0;
        else if(N==1)
            return 1;
          else if(buf[i]=="\0"&&N>=i)
              return i;
          
        }
        return  N;
        
    }
}

关于maven常用命令

(1) mvn -v :检查 maven是否安装成功
(2)进入到maven根目录,执行mvn compile命令会在根目录生成target文件
(3)执行mvn clean可将根目录下生成的target文件移除
(4)mvn install命令

java输出任意两个日期之间有多少天

package JingDian;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class howmanyDays {
    public static void main(String[] args){
        String d1 = "2013-06-12";
        String d2 = "2013-04-08";
        dateDays howmany = new dateDays();
        howmany.calculate(d1, d2);
    
    }

}

class dateDays{
    String dateFormat = "yyyy-MM-dd";
    SimpleDateFormat format= new SimpleDateFormat(dateFormat);
    //TrueDate方法用于把输入的String类型的日期转化为Date型(即转化为日期格式)
    public Date TrueDate(String str){
        if(str == null)
            return null;
        try
        {
            //SimpleDateFormat类中的parse()方法解析字符串的文本,生成 Date。format() 将给定的
 Date格式化为字符串
            return format.parse(str);
        }catch(ParseException e)
        {
            e.printStackTrace();
        }
        return null;
    }
    public void calculate(String date1,String date2){
        if(date1.equals(date2))
        {
           System.out.println("两个日期相等");
           return;
        }
        if(date1.compareTo(date2)>0)
        { 
            //确保data2中存放的是较大的那个日期
            String tmp;
            tmp = date1;
            date1 = date2;
            date2 = tmp;
        }
        //转化为Date
        Date Ddate1 = TrueDate(date1);
        Date Ddate2 = TrueDate(date2);
        //Date类中的getTime()方法返回long型整数,即从GMT1970-01-01 00:00:00到该Date对象之间的
时间差,毫秒为单位
         int t = (int) ((Ddate2.getTime()-Ddate1.getTime())/1000/60/60/24);
         System.out.println(t);
        }
    }
}

compareto方法

Machine learning--Andrew Ng--Chapter 2

第二章
一、第六课时-模型描述-supervised learning algorithm
学习整个监督学习算法supervised learning algorithm的过程,监督学习包括线性回归 regression problem和离散问题discrete problem
Model Representation
image
image
To establish notation for future use, we’ll use x(i) to denote the “input” variables (living area in this
example), also called input features, and y(i) to denote the “output” or target variable that we are trying to
predict (price). A pair (x(i),y(i)) is called a training example, and the dataset that we’ll be using to learn—a
list of m training examples (x(i),y(i));i=1,...,m—is called a training set. Note that the superscript “(i)” in the
notation is simply an index into the training set, and has nothing to do with exponentiation. We will also
use X to denote the space of input values, and Y to denote the space of output values. In this example, X
= Y = ℝ. To describe the supervised learning problem slightly more formally, our goal is, given a training
set, to learn a function h : X → Y so that h(x) is a “good” predictor for the corresponding value
of y. For historical reasons, this function h is called a hypothesis. Seen pictorially, the process is therefore
like this:
image
image
When the target variable that we’re trying to predict is continuous, such as in our housing example, we
call the learning problem a regression problem. When y can take on only a small number of discrete
values (such as if, given the living area, we wanted to predict if a dwelling is a house or an apartment,
say), we call it a classification problem.

java:保留两位小数

import java.text.DecimalFormat;  
DecimalFormat    df   = new DecimalFormat("#.00");   

double d1 = 3.23456  
double d2 = 0.0;
double d3 = 2.0;
df.format(d1); 
df.format(d2); 
df.format(d3); 

结果

3.23
0.00 
2.00

Java集合List模拟“洗牌”操作

Collection工具类为操作List集合提供了几个有用的方法:
reverse()、shuffle()、sort()、swap()、rotate()。
小例子: 使用shuffle(),方法模拟洗牌操作,并输出。

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class ShuffleCards {
   private String[] types = {"方块","草花","黑桃","红心"};
   private String[] values = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
   private List<String> cards = new LinkedList<String>();
   //private int length;
   public void initCards(){
       for(int i=0;i<types.length;i++){
           for(int j=0;j<values.length;j++){
               cards.add(types[i]+values[j]);
           }
       }
       
       Collections.shuffle(cards);
       ListIterator lit = cards.listIterator();
       while(lit.hasNext()){
           System.out.println(lit.next());
       } 
   }

   
   public static void main(String[] args){
       ShuffleCards sc = new ShuffleCards();
       sc.initCards();
      
      
   }
}

ac代码。。小米的题小坑

座位问题,就遍历字符串就行。。
/开始写代码/
static boolean fun(String table, int n) {
char[] seats = table.toCharArray();
if(seats.length == 0 && n > 0)
return false;
else if(n == 0)
return true;
for(int i = 0; i < seats.length-1;) {
if(seats[i] == '1') {
i += 2;
continue;
}
if(i == 0 && seats[i+1] == '0') {
n --;
i += 2;
} else if(i > 0 && seats[i-1] == '0' && seats[i+1] == '0') {
n --;
i += 2;
} else {
i ++;
}
if(n == 0)
return true;
}
return false;
}
/结束写代码/
杨辉三角。。从第一行开始判断每一行出现没这个数。。。。但是ac了(所有的cases都在前100行)
/开始写代码/
static long fun(long x) {
if(x == 1)
return 1;

    return getYFTriangleLine(100, x);
}
static long getYFTriangleLine(int lines, long target){
    long[] a = new long[lines + 1];
    long previous = 1;
    for (int i = 1; i <= lines; i ++){
        for (int j = 1; j <= i; j++){
            long current = a[j];
            a[j] = previous + current;
            previous = current;
            if(a[j] == target)
                return i;
        }
    }
    return -1;
}

/结束写代码/
动态规划求俩字符串的最大公共长度。。
/开始写代码/
static boolean fun(String str) {
if(str == null || "".equals(str))
return true;
int l = str.length();
String rstr = new StringBuffer(str).reverse().toString();
int[][] dp = new int[l+1][l+1];
dp[0][0] = 0;
for(int i = 0; i < l; i ++) {
dp[0][i] = 0;
dp[i][0] = 0;
}

    for(int i = 1;i < l+1; i ++)
        for(int j = 1; j < l+1; j ++) {
            if(str.charAt(i-1) == rstr.charAt(j-1))
                dp[i][j] = dp[i-1][j-1] +1;
            else {
                dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
            }
        }
    return l - dp[l][l] <= 1;
}

/结束写代码/

用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。

package JingDian;

import java.util.Iterator;
import java.util.TreeSet;

public class numberRandom {
    String[] stra = {"1","2","2","3","4","5"};
    int n = stra.length;
    boolean[] visited = new boolean[n];
    String result = "";
    TreeSet<String> ts = new TreeSet<String>();
    int[][] a = new int[n][n];
    private  void searchMap()
    {
        
        for(int i=0;i<n;i++)
        {
           for(int j=0;j<n;j++)
           {
               if(i==j)
               {
                   //图中对角线部分是无法访问的
                   a[i][j]=0;
                   }else{
                       a[i][j]=1;
                   }
           }            
        }
        //3和5不能相连
        a[3][5]=0;
        a[5][3]=0;
        //开始遍历。实际效果是这里依次决定排列的第一位。先找所有的排列第一位是1的,再找所有的排
列第一位是2的,......
        for(int i=0;i<n;i++)
        {
            search(i);
        }
        Iterator<String> it = ts.iterator();
        while(it.hasNext())
        {
            String str =it.next();
            //4不能在第三位
            if(str.indexOf("4")!=2){
                System.out.println(str);
            }
        }
      }
    //这是个深度优先的遍历
    private void search(int startIndex){
        visited[startIndex] = true; 
        result = result + stra[startIndex];
        if(result.length() ==n)
        {
            ts.add(result);
        }
        for(int j=0;j<n;j++)
        {
            if(a[startIndex][j]==1&&visited[j]==false)
            {
                search(j);
            }else
            {
                continue;
            }
        }
        //一个result结束后踢掉最后一个,寻找别的可能性,若没有的话,则继续向前踢掉当前最后一个
        result = result.substring(0,result.length()-1);
        visited[startIndex] = false;
        
    }
    
    
    public static void main(String[] args){
        new numberRandom().searchMap();
        
    }

}

有两处循环加递归,比较绕,debug下吧

思路:
1 、把问题归结为图结构的遍历问题。实际上6个数字就是六个结点,把六个结点连接成无向连通
图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这 6个数字的排列组合结果
集。
2、 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足该条
件,然后再遍历图。
3、 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果
4、4不能在第三位: 仍旧在结果集中去除满足此条件的结果。

Machine learning--Andrew Ng--Chapter 1

第一章

一 、使用OCtave或者Matlab进行机器学习算法的编成更高效。可以使用java或python,只不过它们对算法实现起来会更复杂。建议使用OCtave作为你的学习工具和原型工具。

这样能快的实现一个算法
1、先使用OCtava建立学习算法原型
2、只有在这个算法可以工作后,再将其迁移到java或其它编译环境中

这个课程不建议用R的原因:
1

二、前两节介绍
1、什么是机器学习/机器学习的应用场景
1
2、机器学习的总体分类。
2

三 监督学习[Supervised Learning]
In supervised learning, we are given a data set and already know what our correct output should look like,
having the idea that there is a relationship between the input and the output. Supervised learning
problems are categorized into "regression" and "classification" problems. In a regression problem, we are
trying to predict results within a continuous output, meaning that we are trying to map input variables to
some continuous function. In a classification problem, we are instead trying to predict results in a discrete
output. In other words, we are trying to map input variables into discrete categories.
Example 1:
Given data about the size of houses on the real estate market, try to predict their price. Price as a function of size is a continuous output, so this is a regression problem.
We could turn this example into a classification problem by instead making our output about whether the house "sells for more or less than the asking price." Here we are classifying the houses based on price into two discrete categories.
Example 2:
(a)Regression - Given a picture of a person, we have to predict their age on the basis of the given picture
(b)Classification - Given a patient with a tumor, we have to predict whether the tumor is malignant or benign.

四 非监督学习[Unsupervised learning]
Unsupervised learning allows us to approach problems with little or no idea what our results should look
like. We can derive structure from data where we don't necessarily know the effect of the variables.
We can derive this structure by clustering the data based on relationships among the variables in the
data.
With unsupervised learning there is no feedback based on the prediction results. Example:
Clustering: Take a collection of 1,000,000 different genes, and find a way to automatically group these genes into groups that are somehow similar or related by different variables, such as lifespan, location,
roles, and so on.
Non-clustering: The "Cocktail Party Algorithm", allows you to find structure in a chaotic environment. (i.e.
identifying individual voices and music from a mesh of sounds at a cocktail party).

2014金山网络笔试

Java笔试题(金山网络)
题目1:工厂两条生产线,三个工人,生产线上可以最多存放m个产品,当生产线满时,机器停止生产,等产品线不满时才继续生产,每条产线上一次只能允许一个工人取产品,编程实现该过程,使整个生产线能流畅 运行。 

import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;
//多线程(生产者消费者问题)class Producer extends Thread {
    //使用阻塞队列(BlockingQueue)控制线程通信
    private BlockingQueue<String>bq;
    public Producer(BlockingQueue<String> bq){
        this.bq = bq;
    }
    public void run(){
        //用于生产的元素
        String[] strArr = new String[]{
                "Java",
                "Structs",
                "Spring"
        };
        for(int i = 0;i<9;i++){
            System.out.println(getName()+"生产者准备生产集合元素");
            try{
                Thread.sleep(200);
                //尝试放入元素,如果队列已满,则线程被阻塞
                bq.put(strArr[i%3]);
            }catch(Exception ex){
                ex.printStackTrace();
            }
            System.out.println(getName()+"生产完成:"+bq);
        }
    }
}
class Consumer extends Thread{
    private BlockingQueue<String>bq;
    public Consumer(BlockingQueue<String>bq){
        this.bq = bq;
    }
    public void run(){
        //每条生产线上只允许一个人去取(同步代码块“加锁-修改-释放锁”)
        synchronized(bq){
        while(true){
            System.out.printf(getName()+"消费者准备消费集合元素");
            try{
                Thread.sleep(200);
                //尝试取出元素,如果队列已空,则阻塞该线程                bq.take();
            }catch(Exception ex){
                ex.getStackTrace();
            }
            System.out.println(getName()+"消费完成"+bq);
           }
        }//同步代码块结束,该线程释放同步锁    }
}
public class UseBlockingQueue {
    public static void main(String[] args){
        //创建一个容量为1的BlockingQueue
       int m=10;
        BlockingQueue<String>bq = new ArrayBlockingQueue<>(m);
        //启动2个生产者线程
        new Producer(bq).start();
        new Producer(bq).start();
 
        //启动一个消费者线程
        new Consumer(bq).start();
    }
}

 题目二:写一个java程序实现ArrayList类,最简单必须实现add()、remove().....
发现网上写成 什么样的都有,于是参考了一下jdk源码,简单实现了一个:    

import java.util.AbstractList;import java.util.List;
public class shixianArrayList<E> extends AbstractList<E> implements List<E>{
    private transient Object[] elementData;
    private int size=0; 
    
    public shixianArrayList(int initialCapacity){
        super();
        if(initialCapacity<0){
            return ;
        }
        this.elementData = new Object[initialCapacity];
    }
    //默认长度为10
    public shixianArrayList(){
        this(10);
    }

    @Override
    public E get(int index) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public int size() {
        // TODO Auto-generated method stub
        return size;
    }
    
    public boolean isEmpty(){
        return size ==0;
    }
    
    public boolean contains(Object o){
        return indexOf(o) >=0;
    }
    
    public int indexOf(Object o){
        if(o == null){
            for(int i=0;i<size;i++){
                if(elementData[i]==null){
                    return i;
                }
            }
        }else{
            for(int i=0;i<size;i++){
                if(o.equals(elementData[i]))
                    return i;
            }
        }
        return -1;
    }
    //这里是不带索引的add(),带索引的也可以有
    public boolean add(E e){
        
        elementData[size++] = e;
        return true;
    }
    
    //这里是直接按照元素删除,按照索引删除的也可以有
    public boolean remove(Object o){
        if(o == null){
            for(int index=0;index<size;index++){
                if(elementData[index] == null){
                    fastRemove(index);
                    return true;
                        }
            }
        }else{
            for(int index=0;index<size;index++){
                if(o.equals(elementData[index])){
                    fastRemove(index);
                    return true;
                }
            }
        }
        return false;
    }
    
    
    private void fastRemove(int index) {
        modCount++;
        int numMoved = size-index-1;
        if(numMoved>0)
            System.arraycopy(elementData, index+1,elementData , index,numMoved);
        elementData[--size] = null; //让gc来做这个工作        
    }

    public static void main(String[] args){
        String[] books = {
                "java","structs"
        };
        shixianArrayList<String> booklist = new shixianArrayList<String>();
        for(int i =0;i<books.length;i++){
            booklist.add(books[i]);
        }
        //booklist.remove(books[1]);        
        System.out.println(booklist.toStringFunc());
    }
    //elementData为Object[]类型,不能直接打印
    public String toStringFunc(){
        String tempstr="";
        for(int i=0;i<this.size;i++){
            if(this.elementData[i]!=null){
            tempstr+=this.elementData[i];
           }
        }
       return tempstr;    
    } 
}

题目三:已知两个字符串由不同的字母组成,一长一短,长的为A,短的为B。若所有在B中出现的字符都在A中出现,则返回true,否则返回false。假设短的长度为m,长的长度为n,要求算法的时间复杂度不能大于O(m+n)。 
解法一:最先想到的解法,对字符串B中的每个字母在A中都遍历一遍(但是时间复杂度为O(m*n))

public class BinA {
    public static void main(String[] args){
        String[] stra = {"a","a","b","b","b","b","c","d","e","e"};
        String[] strb = {"a","a","c","d","e"};
        Contain A = new Contain();
        
        System.out.println(A.containStr(stra, strb));
      
    }
}
    class Contain{
      public  boolean containStr(String[] stra,String[] strb){ 
          int count=0;
          //因为是要把strb中的每个元素拿出来跟stra中的每个元素比,所以要把strb的放在外面的循环
             for(int i=0;i<strb.length;i++)
             {
                 for(int j=0;j<stra.length;j++)
                 {
                     //因为数组中可能有重复元素,所以只要在stra中找到一个与strb相等的就就跳出来,才能保证count值=length
                     if(strb[i]==stra[j]){
                         count++;
                     j=stra.length;
                     }
                 }
             }
             //count用作计数器,恰好比较了strb。length次说明在stra中每个strb中的数都有相等的了
             if(count==strb.length){
                 return true;
             }
          return false;
      
    }
}
 

 解法二:设一个哈希表,对字符串A的字符遍历,将每个字符对应的哈希表中的值设为1。然后对B中的字符进行遍历,如果所有字符对应的hash值都为1,则返回true,否则返回false。时间复杂度为O(m+n)。

package JingDian;
import java.util.HashMap;
public class HashBinA {
    
    public static void main(String[] args){
        String[] stra = {"a","a","b","b","b","b","c","d","e","e"};
        String[] strb = {"a","b","c","d","e"};
        hashSolve hs = new hashSolve();
        System.out.println(hs.hashBinA(stra, strb));
    
        
    }

}
class hashSolve{
    public boolean hashBinA(String[] stra,String[] strb){
        HashMap<String, Integer> hm = new HashMap<String, Integer>();
        int count=0;
        for(int i=0;i<stra.length;i++)
        {
            hm.put(stra[i], 1);
        }
        for(int j=0;j<strb.length;j++)
        {
            //若strb中存在stra中没有的keyget()方法会返回空而报空指针错误,因此要判断是否为空
            if(hm.get(strb[j])!=null&&hm.get(strb[j])==1){
                count += 1;
                
               }
            }
        //count只是一个计数器,如果每一个strb中字母得到的value都为一(都在stra中)则其长度为strb。length
        if(count == strb.length)
        {
            return true;
        }
        
        return false;
    }
    
}

假如现在有一堆长度大于3小于9的电话号码,用座机呼叫,如果出现这样的号码【123和12345】那么12345将永远不会被拨出,因为拨到123的时候电话已经呼出了,试写一个函数输出所有不能被呼出的电话号码

解题:
假如现在有一堆长度大于3小于9的电话号码,用座机呼叫,如果出现这样的号码【123和12345】那么
12345将永远不会被拨出,因为拨到123的时候电话已经呼出了,试写一个函数输出所有不能被呼出的电话号
码【指定传入参数类型如示例所示】
函数参数示例: public void function(int [] numbers){}

给你两个集合,要求{A} + {B}。 注:同一个集合中不会有两个相同的元素

(1)方法一:直接硬来,申请一个数组装俩集合中的元素,然后什么都不管直接排序。
如果两个元素相同,那么肯定会挨着,在输出的时候进行判断,相等的只输出一个

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            int n = scan.nextInt();
            int m = scan.nextInt();
            int arr[] = new int[n + m];
            for (int i = 0; i <arr.length; i++) {
                arr[i] = scan.nextInt();
            }
            Arrays.sort(arr);
            System.out.print(arr[0]);
            for (int i = 1; i < arr.length; i++) {
                if(arr[i]!=arr[i-1]){
                    System.out.print(" "+arr[i]);
                }
            }
            System.out.println();
        }
    }
}

(2)方法2:用Java自带的工具。因为本身是玩Java的,首先想到的肯定是用Java自带的set集合,申请一个set直接装两个集合A,B的元素(自动去重复了就), 然后再对其进行排序,就OK了

import java.util.HashSet;
import java.util.Scanner;
public class Main {
	/**
	 * @Title_strArr2Str
	 * @Describe_将一个字符串数组变成一个字符串_两个字符串中间穿插上指定的字符
	 * @Author_Stone6762
	 * @param arr字符串数组
	 * @param signs中间想要插入的字符
	 * @return
	 */
	public static String arr2String( Object[] arr, String signs) {
		String aim = "";
		for (int i = 0; i < arr.length; i++) {
			if (i == 0) {
				aim += arr[i].toString();
				continue;
			}
			aim += signs + arr[i];
		}

		return aim;
	}
	private static void chooseSort(Integer[] arr) {
		for (int i = 0; i < arr.length - 1; i++) {
			int min = i;
			for (int j = i + 1; j < arr.length; j++) {
				if (arr[j] < arr[min]) {
					min = j;
				}
			}
			int temp = arr[i];
			arr[i] = arr[min];
			arr[min] = temp;
		}
	}
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		while (scan.hasNext()) {
			int n=scan.nextInt();
			int m=scan.nextInt();
			HashSet<Integer>set=new HashSet<Integer>();
			for (int i = 0; i <m+n; i++) {
				set.add(scan.nextInt());				
			}
			Integer []arr=set.toArray(new Integer[0]);
			chooseSort(arr);
			System.out.println(arr2String(arr, " "));
		}
	}

}

sap labs java MST

乐观锁、悲观锁

什么是泛型,它在class阶段会被编译成什么

final finally finalize各自用法和区别

有垃圾回收机制了,为什么还会有这个finalize

异常类的分类和各自特点

==和equals

接口和抽象类

ArrayLIst和LinkLIst

hashCode

restful:RESTful API是目前比较成熟的一套互联网应用程序的API设计理论

spring框架

JavaScript中的继承

闭包.一般闭包用在什么场景

ConcurrentHashMap

java编程:对输入的数组元素(包括数字和字母)进行去重后输出

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.