1.冒泡排序
public static int[] bubbleSort(int[] arr){
int temp;
for (int j = 0; j <arr.length-1; j++) {
for (int i = 0; i <arr.length-1-j ; i++) {
if(arr[i]>arr[i+1]){
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
return arr;
}
2.二分查找法
public static int binarySearch(int a,int[] arr){
int start=0;
int end=arr.length-1;
while(start<=end){
int mid=start+(end-start)/2;
if(a==arr[mid]){
return mid;
}else if(a<arr[mid]){
end=mid-1;
}else{
start=mid+1;
}
}
return -1;
}
3.插入排序
public static int[] insertSort(int[] arr){
for (int i = 1; i <=arr.length-1 ; i++) {
int tmp=arr[i];
int index=i-1;
while(index>=0&&tmp<arr[index]){
arr[index+1]=arr[index];
index--;
}
arr[index+1]=tmp;
}
return arr;
}
4.选择排序
public static void selectionSort(int[] arr){
if(arr==null||arr.length<2){
return;
}
for (int i = 0; i <arr.length ; i++) {
int minIndex=i;
for(int j=i+1;j<arr.length;j++){
if(arr[j]<arr[minIndex]){
minIndex=j;
}
}
if(minIndex!=i) {
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
5.数据里有{1,2,3,4,5,6,7,8,9},请随机打乱顺序,生成一个新的数组(请以代码实现)
public static int[] srand(int arr[]){
int b[]=new int[arr.length];
for (int i = 0; i <arr.length ; i++) {
int index = (int) (Math.random() * (arr.length - i));
b[i]=arr[index];
int temp=arr[arr.length-1-i];
arr[arr.length-1-i]=arr[index];
arr[index]=temp;
}
return b;
}
6.一个问题的最优解是什么意思
一般情况下,解决一个问题的算法流程,在时间复杂度的指标上,一定要尽可能的低,先满足了时间复杂度最低这个指标之后,使用最少的空间的算法流程,叫这个问题的最优解。 一般说最优解是忽略掉常数项这个因素的,因为这个层次只决定了实现层次的优化和考虑,而和怎么解决这个问题的思想无关。
7.如何不用额外变量交换两个数
public static boolean swap(int arr[],int i,int j){
if(i!=j) {
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
return true;
}
return false;
}
8.一个数组中只有一个出现了奇数次的数,找出这个数
思路:异或运算满足交换律和结合律。根据N^N=0 0^N=N 出现偶数次的数最后异或后肯定为0,如果只有1个出现奇数次的数,那最后数组所有元素的异或运算就是这个奇数的值。
public static void printOddTimesNum(int[] arr){
int eor=0;
for (int i = 0; i <arr.length ; i++) {
eor^=arr[i];
}
System.out.println("eor="+eor);
}
9.提取整形数最右侧的1
n&(~n+1)
public static void main(String[] args) {
int a=((int)(Math.random()*500000))<<1;
int b=a&(~a+1);
System.out.println(a);
System.out.println(Integer.toBinaryString(a));
System.out.println(Integer.toBinaryString(b));
}
10.一个数组中出现奇数次的两个数,其它数都是偶数次。
public static void printOddTimesNum2(int[] arr){
int eor=0;
for (int i = 0; i <arr.length ; i++) {
eor^=arr[i];
}
int newEor=eor&(~eor+1);
int onlyOne=0;
for (int i = 0; i <arr.length ; i++) {
if((newEor&arr[i])!=0){
onlyOne^=arr[i];
}
}
System.out.println("the one="+onlyOne+"\tother one="+(onlyOne^eor));
}
11.统计一个数的二进制里有多少个1
public static int bigCount(int i){
int count=0;
while(i!=0){
int rightOne=i&(~i+1);
count++;
i^=rightOne;
}
return count;
}
12.红黑树的性质:
1.节点要么是红色要么是黑色 2.根节点必须是黑色 3.叶子节点都是NIL是黑色 4.红色节点的两个子节点都是黑色 5.任意一个节点到它的每个叶子节点的路径都包含相同的黑色节点。 根据性质5推导出如果一个子节点为黑色,那这个节点肯定有两个子节点。
13.手写双向链表
package com.zcc.Algorithm;
public class MyLinkedList<E> {
private Node<E> first;
private int length;
private Node<E> last;
private static final class Node<E>{
private Node<E> pre;
private E elemnt;
private Node<E> next;
public Node(Node<E> pre, E elemnt, Node<E> next) {
this.pre = pre;
this.elemnt = elemnt;
this.next = next;
}
public Node() {
}
}
public boolean add(E e){
Node<E> l=last;
Node<E> newNode=new Node<>(last,e,null);
last=newNode;
if(l!=null){
l.next=last;
}else{
first=newNode;
}
length++;
return true;
}
public E get(int index){
if(index<=length/2){
Node<E> f=first;
for (int i = 0; i <index ; i++) {
f=f.next;
}
return f.elemnt;
}else{
Node<E> l=last;
for (int i = length-1; i >index ; i--) {
l=l.pre;
}
return l.elemnt;
}
}
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("{");
Node<E> f=first;
for (int i = 0; i <= length-1; i++) {
sb.append(f.elemnt+",");
f=f.next;
}
sb.deleteCharAt(sb.length()-1);
sb.append("}");
return sb.toString();
}
public static void main(String[] args) {
MyLinkedList<Object> list = new MyLinkedList<>();
list.add("小明");
list.add("小李");
list.add("小王");
list.add("小程");
list.add("小宋");
list.add("小黄");
list.add("小刘");
list.add("小高");
System.out.println(list.get(5));
System.out.println(list.toString());
}
}
14.手写一个ArrayList
package com.zcc.juc;
import java.util.*;
import java.util.function.Consumer;
public class MyArrayList<E> {
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA={};
private static final Object[] EMPTY_ELEMENTDATA={};
private static final int DEFAULT_CAPACITY = 10;
private Object[] elementData;
private int size;
protected transient int modCount = 0;
public MyArrayList(){
this.elementData=DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public MyArrayList(int initialCapacity){
if(initialCapacity>0){
elementData=new Object[initialCapacity];
}else if(initialCapacity==0){
elementData=EMPTY_ELEMENTDATA;
}else{
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
}
}
public boolean add(E e){
ensureCapacityInternal(size+1);
elementData[size++]=e;
return true;
}
public E remove(int index){
if(index>=size)
throw new IndexOutOfBoundsException();
modCount++;
E oldElement = get(index);
int numMoved=size-index-1;
System.arraycopy(elementData,index+1,elementData,index,numMoved);
elementData[--size]=null;
return oldElement;
}
public E get(int index){
if(index>=size)
throw new IndexOutOfBoundsException();
return (E)elementData[index];
}
private void ensureCapacityInternal(int minCapacity) {
if(elementData==DEFAULTCAPACITY_EMPTY_ELEMENTDATA){
minCapacity=DEFAULT_CAPACITY;
}
modCount++;
if(minCapacity-elementData.length>0){
grow(minCapacity);
}
}
private void grow(int minCapacity) {
int oldCapacity=elementData.length;
int newCapacity=oldCapacity+(oldCapacity>>1);
if(newCapacity-minCapacity<0){
newCapacity=minCapacity;
}
elementData= Arrays.copyOf(elementData,newCapacity);
}
private class Itr implements Iterator<E>{
int cursor;
int lastRet=-1;
int expectedModCount=modCount;
public Itr() {
}
@Override
public boolean hasNext() {
return cursor!=size;
}
@Override
public E next() {
checkForModification();
int i=cursor;
if(i>=size){
throw new NoSuchElementException();
}
Object[] elementData = MyArrayList.this.elementData;
if(i>=elementData.length){
throw new ConcurrentModificationException();
}
cursor=i+1;
return (E)elementData[lastRet=i];
}
@Override
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForModification();
try {
MyArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
private void checkForModification() {
if(expectedModCount!=modCount){
throw new ConcurrentModificationException();
}
}
}
@Override
public String toString() {
Iterator<E> it=new Itr();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
}
|