异或运算符使用

tech2023-02-09  104

private static void linkInverse() { //链表反转 Node node9 = new Node(9, null); Node node7 = new Node(7, node9); Node node5 = new Node(5, node7); Node node3 = new Node(3, node5); Node node1 = new Node(1, node3); Node pre =null; Node cur =node1; Node nex =node1.next; while (nex!=null){ nex=cur.next; cur.next=pre; pre=cur; cur=nex; } Node head =pre; while (head!=null){ System.out.print(head.data); head=head.next; } } static class Node{ int data; Node next; public Node(int data, Node next) { this.data = data; this.next = next; } } private static void findTwoNumber() { int[] arr= {1,7,5,4,3,4,6,8,9,5,4,3,9,8,7,1,4,6,1,4}; int eof=0; for (int i = 0; i < arr.length; i++) { eof^=arr[i]; } System.out.println(eof); int findOne= eof&(~eof+1); int numOne=0; for (int i = 0; i < arr.length; i++) { if((findOne&arr[i])==0){ numOne^=arr[i]; } } System.out.println(numOne); System.out.println(numOne^eof); }

链表反转与找出是奇数个数的两个数字

 

最新回复(0)