java 算法
1、递归的方法实现冒泡
1 public class SortTest { 2 public static void main(String[] args) { 3 int[] arr={12,5,16,3,9,0,158,214,85}; 4 queue(arr); 5 System.out.println(Arrays.toString(arr)); 6 } 7 public static int[] queue(int[] a) { 9 for(int j=a.length-1;j>0;j--){ 10 if(a[j]<a[j-1]){ 11 int t=a[j]; 12 a[j]=a[j-1]; 13 a[j-1]=t; 14 return queue(a); 15 } 16 }18 return a; 19 } 20 } 21 }
2、两个有序数组合并为一个有序数组
package com.itheima.suanfa; public class _01TwoArrayOneArray01 { public static void main(String[] args) { //定义两个数组下标,遍历并记录index使用; int indexM =0; int indexN =0; int k=0; int[] arrayM = new int[]{1,4,6,7,8}; int[] arrayN = new int[]{2,3,5,9,11}; int[] res = new int[arrayM.length+arrayN.length]; while (indexM<arrayM.length && indexN<arrayN.length){ if(arrayM[indexM]<arrayN[indexN]){ res[k]=arrayM[indexM]; indexM++; } else{ res[k]=arrayN[indexN]; indexN++; } k++; } if(indexM!=arrayM.length){ for(int i=indexM;i<arrayM.length;i++){ res[k]=arrayM[i]; k++; } } else{ for(int i=indexN;i<arrayN.length;i++){ res[k]=arrayN[i]; k++; } } for(int a =0;a<k;a++){ System.out.println(res[a]); } } }
3、单向链表反转
package com.itheima.suanfa; public class _02SingleLinkReverse { public static void main(String[] args) { DataNode dataNode = new DataNode(1); DataNode dataNode1 = new DataNode(2); dataNode.setNext(dataNode1); DataNode dataNode2 = new DataNode(3); dataNode1.setNext(dataNode2); printLinkData(dataNode); DataNode nodeNew=reverse1(dataNode); printLinkData(nodeNew); } /** * 遍历实现 通用实现方法 * * @param head * @return */ public static DataNode reverse1(DataNode head) { if (null == head || null == head.getNext()) return head; DataNode pre=null; DataNode cur=head; DataNode next=null; while (cur!=null){ next=cur.getNext(); cur.setNext(pre); pre=cur; cur=next; } return pre; } public static void printLinkData(DataNode node){ System.out.println(node.getData()); if (node.getNext()!=null){ printLinkData(node.getNext()); } } } class DataNode { private int data; private DataNode next; public int getData() { return data; } public void setData(int data) { this.data = data; } public DataNode getNext() { return next; } public void setNext(DataNode next) { this.next = next; } public DataNode(int data) { this.data = data; } }
4、单向链表去重 1->2->3->3->5 1->2->3->5 1->2->5
package com.itheima.suanfa; import javax.security.auth.kerberos.DelegationPermission; public class _03SingleLinkRemoveReport { public static void main(String[] args) { int[] num = {2, 2,2, 3, 3, 9, 11}; Node head = new Node(num[0]); Node pre = head; for (int i = 1; i < num.length; i++) { Node node = new Node(num[i]); pre.next = node; pre = node; } printLinkData(head); System.out.println(""); Node newHead = DeleteDupliacateNode1(head); printLinkData(newHead); } /*将链表中重复的元素全部删除 1->2->2->3 1->2->3*/ public static Node DeleteDupliacateNode1(Node head) { if (head == null) { return head; } Node zeroHead = new Node(0); zeroHead.next = head; System.out.println("zeroHead"); printLinkData(zeroHead); System.out.println(""); Node pre = zeroHead; Node p = head; while (p != null && p.next != null) { if (p.data == p.next.data) { int val = p.data; while (p.next != null && p.next.data == val) { p.next = p.next.next; } pre.next = p; } else { pre = p; p = p.next; } } return zeroHead.next; } /*将链表中重复的元素全部删除 1->2->2->3 1->3 0,2, 2, 3, 3, 9, 11*/ public static Node DeleteDupliacateNode12(Node head) { if (head == null) { return head; } Node zeroHead = new Node(0); zeroHead.next = head; System.out.println("zeroHead"); printLinkData(zeroHead); System.out.println(""); Node pre = zeroHead; Node p = head; while (p != null && p.next != null) { if (p.data == p.next.data) { int val = p.data; while (p != null && p.data == val) { p = p.next; } pre.next = p; } else { pre = p; p = p.next; } } return zeroHead.next; } public static void printLinkData(Node node) { System.out.print(node.data + " "); if (node.next != null) { printLinkData(node.next); } } } class Node { public int data; public Node next; public Node(int data) { this.data = data; } }
5、( { [ 匹配
package com.itheima.suanfa; import java.util.HashMap; import java.util.Map; import java.util.Stack; public class _04MatchExer01 { static boolean isMatch(String s) { //定义左右括号的对应关系 Map<Character, Character> bracket = new HashMap<>(); bracket.put(')', '('); bracket.put(']', '['); bracket.put('}', '{'); Stack stack = new Stack(); for (int i = 0; i < s.length(); i++) { Character temp = s.charAt(i);//先转换成字符 //是否为左括号 if (bracket.containsValue(temp)) { stack.push(temp); //是否为右括号 } else if (bracket.containsKey(temp)) { if (stack.isEmpty()) { return false; } //若左右括号匹配 if (stack.peek() == bracket.get(temp)) { stack.pop(); } else { return false; } } } return stack.isEmpty() ? true : false; } public static void main(String[] args) { System.out.println(isMatch("(***)-[{-------}]")); //true System.out.println(isMatch("(2+4)*a[5]")); //true System.out.println(isMatch("({}[]]])")); //false System.out.println(isMatch("())))")); //false } }