package com.zyc.test;import java.util.Arrays;/* * 1.有2个数组A,B, B数组中的元素包含在A数组中,请写一段代码把A数组中B没有的元素放道C数组中。 * 2.假如数组中都是数字,而且大小已经排序好,, 请写一段代码最快效率把上一题的元素放到C数组中。 */ public class Test01 { public static void main(String[] args) { fun1(); fun2(); } static void fun1() { int[] a = new int[] { 7, 3, 2, 1, 4, 8, 6, 9, 10, 5 };// 0-10 int[] b = new int[] { 8, 6, 4, 10, 2 };// 偶数 int[] c = new int[a.length - b.length]; Arrays.sort(a); Arrays.sort(b); for (int ai = 0,bi = 0,ci = 0; ai < a.length; ++ai) { if (a[ai] == b[bi]) { ++bi; } else { c[ci] = a[ai]; ++ci; } } System.out.println("fun1,数组c:" + Arrays.toString(c)); } static void fun2() { //与fun1排序后方法相同 } }
用JDK中已提供的方法不是应该很快的嘛
这是怎么回事儿,为什么在a数组中添加一个元素之后在运行时就会报错呢 int[] a = new int[] { 7, 3, 2, 1, 4, 8, 6, 9, 10,5,11 };// 0-10Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at datastruct.Test01.fun1(Test01.java:23) at datastruct.Test01.main(Test01.java:12)
这是怎么回事儿,为什么在a数组中添加一个元素之后在运行时就会报错呢 int[] a = new int[] { 7, 3, 2, 1, 4, 8, 6, 9, 10,5,11 };// 0-10Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at datastruct.Test01.fun1(Test01.java:23) at datastruct.Test01.main(Test01.java:12)
1.有2个数组A,B, B数组中的元素包含在A数组中,请写一段代码把A数组中B没有的元素放道C数组中。 import java.util.Arrays; import java.util.HashSet; import java.util.Set;public class Demo01 { public static void main(String[] args){ Object[] a = new Object[]{1,2,3,4,5,6,7,8,4,3,4,5,3,"a","d","f","e"}; Object[] b = new Object[]{1,2,3,4,5}; Object[] c = new Object[a.length - b.length]; Set setA = new HashSet(Arrays.asList(a)); Set setB = new HashSet(Arrays.asList(b)); setA.removeAll(setB); c = setA.toArray(); System.out.println(Arrays.toString(c)); } }
经典,太棒了!此类题一般会要求不能使用jdk API
经典,太棒了!此类题一般会要求不能使用jdk API set 里不允许有重复的东东,如果数组中有重复的元素呢?改用List吧
第二个问题答的确实不错!比那些使用API的效率高多了
第二个问题答的确实不错!比那些使用API的效率高多了有序的还有二分。直接用类似归并的
这题应该要考你算法方面的知识。 用API的应该省省了。
如果我这样写得话,HR会不会直接给我一个get away!!! public static void main(String[] args) { // TODO Auto-generated method stub /* 1.有2个数组A,B, B数组中的元素包含在A数组中,请写一段代码把A数组中B没有的元素放道C数组中。*/ Integer[] A = {44,11,66,99}; Integer[] B={44,66}; Integer[] C = new Integer[A.length-B.length]; List<Integer> li = Arrays.asList(B); for (int i = 0, x = 0; i < A.length; i++) { boolean ban = li.contains(A[i]); if(ban==false) { C[x]=A[i]; x++; } } for (int i = 0; i < C.length; i++) { System.out.println(C[i]); } }
2、直接用上面1里面说的二分查找。
String B[10] ;
String C[100] ;
// A 、 B的初始化代码 , 略
int k= 0 ;
for(int i=0 ; i< 100 ; i++){
boolean exists = false;
for( int j=0; j< 10 ; j++){
if( A[i].equals( B[j] ) ){
exists = true;
}
}
if( !exists){
C[k] = A[i];
k++;
}}问题2: int A[100] ;
int B[10] ;
int C[100] ;
// A 、 B的初始化代码 , 略
// 假设 B是从小到大排序int k= 0 ;
for(int i=0 ; i< 100 ; i++){
if( A[i]< B[0] && A[i]> B[10]){
C[k] = A[i];
k++;
}}
2、直接用上面1里面说的二分查找。
package com.zyc.test;import java.util.Arrays;/*
* 1.有2个数组A,B, B数组中的元素包含在A数组中,请写一段代码把A数组中B没有的元素放道C数组中。
* 2.假如数组中都是数字,而且大小已经排序好,, 请写一段代码最快效率把上一题的元素放到C数组中。
*/
public class Test01 { public static void main(String[] args) {
fun1();
fun2();
} static void fun1() {
int[] a = new int[] { 7, 3, 2, 1, 4, 8, 6, 9, 10, 5 };// 0-10
int[] b = new int[] { 8, 6, 4, 10, 2 };// 偶数
int[] c = new int[a.length - b.length];
Arrays.sort(a);
Arrays.sort(b);
for (int ai = 0,bi = 0,ci = 0; ai < a.length; ++ai) {
if (a[ai] == b[bi]) {
++bi;
} else {
c[ci] = a[ai];
++ci;
}
}
System.out.println("fun1,数组c:" + Arrays.toString(c));
} static void fun2() {
//与fun1排序后方法相同
}
}
int[] a = new int[] { 7, 3, 2, 1, 4, 8, 6, 9, 10,5,11 };// 0-10Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at datastruct.Test01.fun1(Test01.java:23)
at datastruct.Test01.main(Test01.java:12)
int[] a = new int[] { 7, 3, 2, 1, 4, 8, 6, 9, 10,5,11 };// 0-10Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at datastruct.Test01.fun1(Test01.java:23)
at datastruct.Test01.main(Test01.java:12)
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;public class Demo01 {
public static void main(String[] args){
Object[] a = new Object[]{1,2,3,4,5,6,7,8,4,3,4,5,3,"a","d","f","e"};
Object[] b = new Object[]{1,2,3,4,5};
Object[] c = new Object[a.length - b.length];
Set setA = new HashSet(Arrays.asList(a));
Set setB = new HashSet(Arrays.asList(b));
setA.removeAll(setB);
c = setA.toArray();
System.out.println(Arrays.toString(c));
}
}
set 里不允许有重复的东东,如果数组中有重复的元素呢?改用List吧
用API的应该省省了。
如果我这样写得话,HR会不会直接给我一个get away!!!
public static void main(String[] args) {
// TODO Auto-generated method stub
/* 1.有2个数组A,B, B数组中的元素包含在A数组中,请写一段代码把A数组中B没有的元素放道C数组中。*/
Integer[] A = {44,11,66,99};
Integer[] B={44,66};
Integer[] C = new Integer[A.length-B.length];
List<Integer> li = Arrays.asList(B);
for (int i = 0, x = 0; i < A.length; i++) {
boolean ban = li.contains(A[i]);
if(ban==false)
{
C[x]=A[i];
x++;
}
}
for (int i = 0; i < C.length; i++) {
System.out.println(C[i]);
}
}