传说中的15道常见的基础算法题java解法

来源:互联网 发布:幼儿园 知乎 编辑:程序博客网 时间:2024/06/05 00:09
15道常见的基础算法题:
1、合并排序,将两个已经排序的数组合并成一个数组,其中一个数组能容下两个数组的所有元素;
2、合并两个已经排序的单链表;
3、倒序打印一个单链表;
4、给定一个单链表的头指针和一个指定节点的指针,在O(1)时间删除该节点;
5、找到链表倒数第K个节点;
6、反转单链表;
7、通过两个栈实现一个队列;(没有贴出来)
8、二分查找;
9、快速排序;
10、获得一个int型的数中二进制中1的个数;
11、输入一个数组,实现一个函数,让所有奇数都在偶数前面;
12、判断一个字符串是否是另一个字符串的子串;
13、把一个int型数组中的数字拼成一个串,这个串代表的数字最小;
14、输入一颗二叉树,输出它的镜像(每个节点的左右子节点交换位置);

15、输入两个链表,找到它们第一个公共节点;


经过了测试,不过肯定有考虑不周全的,有些答案估计也不是最优解,比如第13道题。直接贴代码,思路应该都还比较简单。

<span style="font-size:14px;">package org.whut.choumiaoer.write;/** * 合并排序,将两个已经排序的数组合并成一个数组,其中一个数组能容下两个数组的所有元素; */public class mergeSortedArray {    public void merge(int[] A,int m,int[] B,int n){        int i=m-1;        int j=n-1;        int t=m+n-1;        while(t>0){            if(j<0||(j>=0&&A[i]>B[j])){                A[t--]=A[i--];            }else{                A[t--]=B[j--];            }        }        for(int s=0;s<m+n;s++){            System.out.print(A[s]+" ");        }    }}</span>
<span style="font-size:14px;">package org.whut.choumiaoer.write;import org.whut.choumiaoer.linked.basic.ListNode;/** * 合并两个已经排序的单链表 */public class mergeSortedListNode {    public static ListNode merge(ListNode p1,ListNode p2){        ListNode head=new ListNode(0);        ListNode p=head;        while(p1!=null&&p2!=null){            if(p1.val<p2.val){                p.next=p1;                p1=p1.next;            }else{                p.next=p2;                p2=p2.next;            }            p=p.next;        }        if(p1==null){            p.next=p2;        }        if(p2==null){            p.next=p1;        }        return head.next;    }}</span>
<span style="font-size:14px;">package org.whut.choumiaoer.write;import org.whut.choumiaoer.linked.basic.ListNode;/** * 反转单链表 */public class ReOrderListNode {    public static ListNode reOrder(ListNode head){        if(head==null){            return head;        }        ListNode p=head;        ListNode p1=head.next;        p.next=null;        while(p1!=null){            ListNode p2=p1.next;            p1.next=p;            p=p1;            p1=p2;        }        head.next=null;        return p;    }}</span>
<span style="font-size:14px;">package org.whut.choumiaoer.write;import org.whut.choumiaoer.linked.basic.ListNode;/** * 给定一个单链表的头指针和一个指定节点的指针,在O(1)时间删除该节点 */public class DeleteNode {    public static ListNode delete(ListNode head,ListNode t){        if(head==null||head.next==null||t==null){            return null;        }        if(head==t){            return head.next;        }        if(t.next==null){            ListNode p=head;            while(p.next.next!=null){                p=p.next;            }            p.next=null;            return head;        }        ListNode temp=t.next;        t.val=temp.val;        t.next=temp.next;        return head;    }}</span>
<span style="font-size:14px;">package org.whut.choumiaoer.write;import org.whut.choumiaoer.linked.basic.ListNode;/** * 找到链表倒数第K个节点 */public class FindK {    public static ListNode find(ListNode head,int k){        if(head==null||k<0){            return null;        }        ListNode p1=head;        ListNode p2=head;        while(k>0){            p1=p1.next;            k--;            if(p1==null&&k>0){                return null;            }        }        while (p1!=null){            p1=p1.next;            p2=p2.next;        }        return p2;    }}</span>
<span style="font-size:14px;">package org.whut.choumiaoer.write;import org.whut.choumiaoer.linked.basic.ListNode;import java.util.Stack;/** * 倒序打印一个单链表 */public class PrintListNode {    public static void print(ListNode head){        if(head!=null){            ListNode p=head;            Stack<Integer> stack=new Stack<Integer>();            while(p!=null){                stack.push(p.val);                p=p.next;            }            while(!stack.isEmpty()) {                System.out.print(stack.pop()+" ");            }        }    }}</span>
<span style="font-size:14px;">package org.whut.choumiaoer.write;/** * 二分查找 */public class MidSearch {    //判断存在与否    public static boolean search(int[] A,int target){        if(A.length==0){            return false;        }        if(target<A[0]||target>A[A.length-1]){            return false;        }        int low=0;        int high=A.length-1;        while(low<high){            int mid=(high+low)/2;            if(target>A[mid]){                low=mid+1;            }            else if(target<A[mid]){                high=mid-1;            }else{                return true;            }        }        return false;    }    //返回target在数组中的索引    public static int find(int[] A,int target){        if(A.length==0){            return -1;        }        if(target<A[0]||target>A[A.length-1]){            return -1;        }        int low=0;        int high=A.length-1;        int mid=(low+high)/2;        while (low<=high){            mid=(low+high)/2;            if(A[mid]>target){                high=mid-1;            }else if(A[mid]<target){                low=mid+1;            }else {                return mid;            }        }        return mid;    }}</span>
<span style="font-size:14px;">package org.whut.choumiaoer.write;/** * 快速排序 */public class Quick {    public static void sort(int[] A,int low,int high){        if (low<high){            int mid=getMid(A,low,high);            sort(A,low,mid-1);            sort(A,mid+1,high);        }    }    public static int getMid(int[] A,int low,int high){        int temp=A[low];        while(low<high){            while (low<high&&A[high]>=temp){                high--;            }            A[low]=A[high];            while (low<high&&A[low]<temp){                low++;            }            A[high]=A[low];        }        A[low]=temp;        return low;    }}</span>
<span style="font-size:14px;">package org.whut.choumiaoer.write;/** * 获得一个int型的数中二进制中1的个数 */public class CountOfBinary {    public static int counts(int value){        int count=0;        while(value!=0){            value=value&(value-1);            count++;        }        return count;    }}</span>

<span style="font-size:14px;">package org.whut.choumiaoer.write;/** * 输入一个数组,实现一个函数,让所有奇数都在偶数前面 */public class CutArray {    public static void solution(int[] A){        int low=0;        int high=A.length-1;        int temp=A[low];        while (low<high){            if(low<high&&A[high]%2==0){                high--;            }            A[low]=A[high];            if(low<high&&A[low]%2!=0){                low++;            }            A[high]=A[low];        }        A[low]=temp;    }}</span>
<span style="font-size:14px;">package org.whut.choumiaoer.write;/** * 判断一个字符串是否是另一个字符串的子串; */public class IsChildString {    //暴力解决法    public static boolean solution(char[] s,char[] t){         if(s.length<=0||t.length<=0||s.length<t.length){             return false;         }        for(int i=0;i<s.length;i++){            if(s[i]==t[0]){                int m=i+1;                int j=1;                while (j<t.length){                    if(s[m++]!=t[j])break;                    j++;                }                if(j==t.length){                    return true;                }            }        }        return false;    }    //KMP算法    public static int[] next(char[] t){        int[] next=new int[t.length];        next[0]=-1;        int i=0;        int j=-1;        while(i<t.length-1){            if(j==-1||t[i]==t[j]){                i++;                j++;                if(t[i]!=t[j]){                    next[i]=j;                }else{                    next[i]=next[j];                }            }else{                j=next[j];            }        }        return next;    }    public static int KMP_Index(char[] s,char[]t){        int[] next=next(t);        int i=0;        int j=0;        while(i<=s.length-1&&j<=t.length-1){            if(j==-1||s[i]==t[j]){                i++;                j++;            }else{                j=next[j];            }        }        if(j<t.length){            return -1;        }else{            return i-t.length;        }    } }</span>
<span style="font-size:14px;">package org.whut.choumiaoer.write;import java.util.HashMap;/** * 把一个int型数组中的数字拼成一个串,这个串代表的数字最小; */public class MinNumber {    public static StringBuilder getMin(int[] numbers){        StringBuilder sb=new StringBuilder("");        HashMap<Double,Integer> map=new HashMap<>();        double[] doubles=new double[numbers.length];        for(int i=0;i<numbers.length;i++){            double d=numbers[i];            while(d>=10){                d/=10;            }            doubles[i]=d;            map.put(doubles[i],numbers[i]);        }        sort(doubles,0,doubles.length-1);        if(map.containsValue(0)){            sb.append(map.get(doubles[1])).append(0);            for(int i=2;i<doubles.length;i++){                sb.append(String.valueOf(map.get(doubles[i])));            }        }else{            for(int i=0;i<doubles.length;i++){                sb.append(String.valueOf(map.get(doubles[i])));            }        }        return sb;    }   //快速排序   public static void sort(double[] doubles,int low,int high){        if (low<high){            int mid=getMid(doubles,low,high);            sort(doubles, low, mid-1);            sort(doubles, mid+1, high);        }    }    public static int getMid(double[] doubles,int low,int high){        double temp=doubles[low];        while (low<high){            while (low<high&&doubles[high]>=temp){                high--;            }            doubles[low]=doubles[high];            while (low<high&&doubles[low]<temp){                low++;            }            doubles[high]=doubles[low];        }        doubles[low]=temp;        return low;    }}</span>
<span style="font-size:14px;">package org.whut.choumiaoer.write;import org.whut.choumiaoer.tree.TreeNode;/** * 输入一颗二叉树,输出它的镜像(每个节点的左右子节点交换位置); */public class MirrorTree {    public static TreeNode mirror(TreeNode root){        if (root==null){            return null;        }        TreeNode left=null;        TreeNode right=null;        if(root.left!=null){            left=root.left;        }        if(root.right!=null){            right=root.right;        }        root.left=right;        root.right=left;        mirror(root.left);        mirror(root.right);        return root;    }}</span>


<span style="font-size:14px;">package org.whut.choumiaoer.write;import org.whut.choumiaoer.linked.basic.ListNode;/** * 输入两个链表,找到它们第一个公共节点 */public class FirstMeet {    public static ListNode getFirst(ListNode p,ListNode q){        ListNode l1=p;        int m=0;        while (l1!=null){            m++;            l1=l1.next;        }        int n=0;        ListNode l2=q;        while(l2!=null){            n++;            l2=l2.next;        }        if(m>n)return solution(p,m,q,n);        else return solution(q,n,p,m);    }    public static ListNode solution(ListNode h1,int m,ListNode h2,int n){        int k=m-n;        ListNode p1=h1;        ListNode p2=h2;        while (k>0){            p1=p1.next;            k--;        }        while (p1!=null){            if(p1.val==p2.val)return p1;            p1=p1.next;            p2=p2.next;        }        return null;    }}</span>












0 0
原创粉丝点击