风-fmgao

导航

一些简单算法

找出数组中只出现一次的俩个元素

package com.sly.uploadfile.algorithm.pack1;

/**
 * Created by fmgao on 2019/9/26.
 * 给定一个整数数组nums,其中恰好有俩个元素只出现一次,其余所有元素均出现俩次,找出只出现一次的俩个元素
 */
public class FindTwoFromNums {

    public static void main(String[] args) {
        int[] s = {1, 2, 1, 3, 2, 5};
        int[] two = getTwo(s);
        for (int b : two) {
            System.out.printf("," + b);
        }
    }

    public static int[] getTwo(int[] nums) {
        int c1 = 0;
        for (int num : nums) {
            c1 ^= num;
        }
        int i = 1;
        while ((c1 & i) == 0) {
            i <<= 1;
        }
        int c2 = 0, c3 = 0;
        for (int num : nums) {
            if ((c2 & i) == 0) {
                c2 ^= num;
            } else {
                c3 ^= num;
            }
        }
        return new int[]{c2, c3};

    }

}

 


通配符匹配

package com.sly.uploadfile.algorithm.pack1;

/**
 * Created by fmgao on 2019/9/12.
 *
 * 示例:
 *      输入:
         s = "aa"
         p = "a"
         输出: false
         解释: "a" 无法匹配 "aa" 整个字符串。
 *
 */
public class TongPeiFuPiPei {

    public static void main(String[] args) {
        boolean res = getTongPei("cb", "?b");
        System.out.println(res);
    }

    public static boolean getTongPei(String s, String p) {
        // 申请dp[][]表格,dp[i][j]表示 若s(0,i-1)与p(0,j-1)匹配,dp[i][j]=true,否则为false
        boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
        // s与p都为空时,匹配
        dp[0][0] = true;
        // s为空,p中含有'*'时
        for (int j = 1; j < (p.length() + 1); j++) {
            if (p.charAt(j - 1) == '*'){
                dp[0][j] = dp[0][j - 1] && true;
            }
        }

        for (int i = 1; i < (s.length() + 1); i++) {
            for (int j = 1; j < (p.length() + 1); j++) {
                if ((p.charAt(j - 1) == s.charAt(i - 1)) || (p.charAt(j - 1) == '?')) {
                    dp[i][j] = dp[i - 1][j - 1];
                }
                if (p.charAt(j - 1) == '*') {
                    dp[i][j] = dp[i][j - 1] || dp[i - 1][j] || dp[i - 1][j - 1];
                }
            }
        }
        return dp[s.length()][p.length()];
    }
}

合并俩个数组,得到一个新的有序数组

package com.sly.uploadfile.algorithm.pack1;

/**
 * Created by fmgao on 2019/9/17.
 * 合并俩个有序数组,得到一个新的有序数组
 */
public class TowNums {
    public static void main(String[] args) {

        int[] a = {1, 5, 9, 14, 15};
        int[] b = {0, 3, 7, 13, 18};
        int[] c = arrayMerge(a, b);
        for (int d : c) {
            System.out.print(d + ",");
        }

    }

    public static int[] arrayMerge(int[] arr1, int[] arr2) {
        int len1 = arr1.length;
        int len2 = arr2.length;
        int[] result = new int[len1 + len2];
        int l1 = 0, l2 = 0, l3 = 0;
        while (l1 < len1 && l2 < len2) {
            if (arr1[l1] < arr2[l2]) {
                result[l3++] = arr1[l1++];
            } else {
                result[l3++] = arr2[l2++];
            }
        }
        while (l1 < len1) {
            result[l3++] = arr1[l1++];
        }
        while (l2 < len2) {
            result[l3++] = arr2[l2++];
        }
        return result;
    }
}

快速排序

package com.sly.uploadfile.algorithm.sortdemo01;

/**
 * Created by fmgao on 2019/9/13.
 */
public class QuickSort {
    public static void main(String[] args) {
        int ars[] = {9, 8, 66, 1, 3, 90, 0, 33, 0};
        quickSort(ars);
        for (int s : ars) {
            System.out.print(s + "\t");
        }
    }


    private static void quickSort(int[] array) {
        if (array != null) {
            quickSort(array, 0, array.length - 1);
        }
    }

    private static void quickSort(int[] array, int beg, int end) {
        if (beg > end || array == null) {
            return;
        }
        int p = partition(array, beg, end);
        quickSort(array, beg, p - 1);
        quickSort(array, p + 1, end);
    }

    private static int partition(int[] array, int beg, int end) {
        int first = array[beg];
        int i = beg, j = end;
        while (i < j) {
            while (array[i] <= first && i < end) {
                i++;
            }
            while (array[j] > first && j >= beg) {
                j--;
            }
            if (i < j) {
                array[i] = array[i] ^ array[j];
                array[j] = array[i] ^ array[j];
                array[i] = array[i] ^ array[j];
            }
        }
        if (j != beg) {
            array[j] = array[beg] ^ array[j];
            array[beg] = array[beg] ^ array[j];
            array[j] = array[beg] ^ array[j];
        }
//        System.out.println("j:" + j);
        return j;
    }

}

单节点链表

package com.sly.uploadfile.algorithm.twoadd;

/**
 * Created by fmgao on 2019/11/13.
 */
public class ListNode {

    int data;

    ListNode next;

    ListNode(int data) {
        this.data = data;
    }

}


package com.sly.uploadfile.algorithm.twoadd;

/**
 * Created by fmgao on 2019/11/13.
 */
public class SingleLinkedList {

    public int size;

    public ListNode head;

    // head|next ->null
    public SingleLinkedList() {
        size = 0;
        head = null;
    }

    // head -> a|next ->b|next ->null
    public void addData(int obj) {
        ListNode node = new ListNode(obj);
        if (size == 0) {
            head = node;
            node.data = obj;
        } else {
            node.next = head;
            head = node;
            node.data = obj;
        }
        size++;
    }

    // head -> a|next ->b|next ->c|next ->null
    public boolean deleteData(int value) {
        if (size == 0) {
            return false;
        }
        ListNode current = head;
        ListNode previous = head;
        while (current.data != value) {
            if (current.next == null) {
                return false;
            } else {
                previous = current;
                current = current.next;
            }
        }
        if (current == head) {
            head = current.next;
            size--;
        } else {
            previous.next = current.next;
            size--;
        }
        return true;
    }


    public void display() {
        ListNode node = head;
        while (node != null) {
            if (node.next == null) {
                System.out.print(node.data);
                node = node.next;
            } else {
                System.out.print(node.data + "->");
                node = node.next;
            }
        }
    }


    public static void main(String[] args) {
        SingleLinkedList list = new SingleLinkedList();
        list.addData(1);
        list.addData(2);
        list.addData(5);
        list.deleteData(5);
        list.addData(5);
        list.display();
    }

}

 

posted on 2020-06-20 16:23  风-fmgao  阅读(210)  评论(0编辑  收藏  举报