1.排序
冒泡排序
import java.util.Scanner; /** 冒泡排序 */ public class Solution{ public static void main(String[] args) { Scanner cin = new Scanner(System.in); int[] a = {5,456,5,4,8,6,12,1}; Bubble bubble = new Bubble(); bubble.bubbleSort(a); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } } class Bubble { int[] bubbleSort(int[] a) { for (int i = 0; i < a.length - 1; i++) { for (int j = i; j < a.length; j++) { if(a[i] > a[j]) { swap(a,i,j); } } } return a; } void swap(int[] a, int i, int j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } }
快速排序
class Solution { public void quickSort(int[] num, int left, int right) { int i, j, tmp; if(left > right) return; tmp = num[left]; //基准点 i = left; j = right; while(i != j) { while(i<j && num[j] >= tmp) j--; while(i<j && num[i] <= tmp) i++; if(i < j) swap(num, i, j); } swap(num, left, j); quickSort(num, left, right - 1); quickSort(num, left + 1, right); return; } public static void swap(int[] num, int i, int j) { int tmp = num[i]; num[i] = num[j]; num[j] = tmp; } public static void main(String[] args) { Solution s = new Solution(); int[] num = {2,1,5,3,4}; s.quickSort(num, 0, 4); for(int i=0; i<5; i++) { System.out.println(num[i]); } } }
1 2 3 4 5
归并排序
class Solution{ public static void main(String[] args) { int[] a = {15,4654,88,1,556,55,45}; MergeSort mergeSort = new MergeSort(); mergeSort.mergeSort(a, 0, a.length - 1); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } } class MergeSort{ public void merge(int[] a, int left, int mid, int right) { int[] tmp = new int[a.length]; int p1 = left, p2 = mid + 1, k = left; while(p1 <= mid && p2 <= right) { if(a[p1] <= a[p2]) tmp[k++] = a[p1++]; else tmp[k++] = a[p2++]; } while(p1<=mid) tmp[k++] = a[p1++]; while(p2<=right) tmp[k++] = a[p2++]; for(int i = left; i <= right; i++) { a[i] = tmp[i]; } } public void mergeSort(int[] a, int st, int ed) { if(st < ed) { int mid = (st + ed) / 2; mergeSort(a, st, mid); mergeSort(a, mid+1, ed); merge(a, st, mid, ed); } } }
2.十进制转二进制【腾讯】
import java.util.Scanner; import java.util.Stack; /** *十进制转换为二进制 */ public class Solution{ public static void main(String[] args) { Scanner cin = new Scanner(System.in); int num = cin.nextInt(); Stack<Integer> s = new Stack<>(); while(num != 0) { int tmp = num % 2; s.push(tmp); num >>= 1; } while(!s.isEmpty()) { System.out.print(s.peek()); s.pop(); } } }
3.不用循环判断一个数是不是2的n次方【腾讯】
import java.util.Scanner; /** 2的n次方的二进制数只有某一位是1,所以根据这个特点 当x&(x-1) == 0时则说明该数是2的n次方 */ public class Solution{ public static void main(String[] args) { Scanner cin = new Scanner(System.in); int num = cin.nextInt(); boolean flag = (num & (num - 1)) == 0 && num != 0? true : false; System.out.println(flag); } }
4.一个有n个数的数组中的数的范围为[1, n],其中有且仅有一个数有重复,找出那个数?【腾讯】
import java.util.Scanner; public class Solution{ public static void main(String[] args) { Scanner cin = new Scanner(System.in); int[] nums = {1,3,3,4,6,5}; int n = nums.length, ans = 0; int[] hash = new int[n+1]; for (int i = 0; i < n; i++) { if(hash[nums[i]] == 0) { hash[nums[i]] = 1; } else { ans = nums[i]; } } System.out.println(ans); } }
5.一个数组中,只有两个数字仅出现一次,其他数字均出现两次,找出这两个数字
import java.util.Scanner; /** 一个数组中,只有两个数字仅出现一次,其他数字均出现两次,找出这两个数字 * * 要求:不借助任何空间 * a ^ 0 = a, a ^ a = 0,因此这里可以借助异或运算可以实现 */ public class Solution{ public static void main(String[] args) { Scanner cin = new Scanner(System.in); int[] nums = {1,2,1,6,8,6}; int[] num1 = new int[5]; int[] num2 = new int[5]; int n = nums.length, ans = 0; int diff = 0; for (int i = 0; i < n; i++) { diff ^= nums[i]; } diff &= -diff; for (int i : nums) { if((i & diff) == 0) num1[0] ^= i; else num2[0] ^= i; } System.out.println(num1[0]); System.out.println(num2[0]); } }
- 所有数字依次异或,结果为那两个不同的数字异或的结果,并且结果一定非0,记为a
- 从左至右找出a第一个bit位非0的index,以此为依据将原数组分为两个子数组(这样的分类标准可以达到上述的目的:1)相同的数字一定落在同一个子数组里 2)那两个不同的数字分布在不同的子数组里)
- 分成两个子数组后,分别异或即可
6.
import java.util.Scanner; /** 求一个double的n次幂 */ public class Solution{ public double pow(double base, int n) { boolean isNagetive = false; //标记正负数 if(n < 0) { n = -n; isNagetive = true; } if(n == 1) return base; if(n == 0) return 1; double ans = pow(base * base,n / 2); if(n%2==1) ans = ans * base; return isNagetive ? 1 / ans : ans; } public static void main(String[] args) { Scanner cin = new Scanner(System.in); Solution solution = new Solution(); System.out.println(solution.pow(20, 2)); } }
7.丑数
public class Solution{ public int uglyNumber(int n) { if(n<=6) return n; int i2 = 0, i3 = 0, i5 = 0; int[] dp = new int[n]; dp[0] = 1; for(int i=1; i<n; i++) { int next2 = dp[i2]*2,next3 = dp[i3]*3,next5 = dp[i5]*5; dp[i] = Math.min(next2, Math.min(next3, next5)); if(dp[i] == next2) i2++; if(dp[i] == next3) i3++; if(dp[i] == next5) i5++; } return dp[n-1]; } public static void main(String[] args) { Solution solution = new Solution(); System.out.println(solution.uglyNumber(1500)); } }
链表基本操作
class ListNode{ int val; public ListNode(int val) { this.val = val; } ListNode next = null; } //实现链表的基本操作 public class Solution{ ListNode head = null; //链表头的引用 /** * 向链表中插入数据 * d:插入数据的内容 */ public void addNode(int d) { ListNode newNode = new ListNode(d); if(head == null) { head = newNode; return ; } ListNode tmp = head; while (tmp.next != null) { tmp = tmp.next; } //add node to end tmp.next = newNode; } /** * 删除第 index 个结点 * 成功返回true,否则返回false */ public boolean deleteNode(int index) { if(index < 1 || index > length()) { return false; } //删除链表第一个元素 if(index == 1) { head = head.next; return true; } int i = 2; ListNode pre = head; ListNode cur = pre.next; while(cur != null) { if(i == index) { pre.next = cur.next; return true; } pre = cur; cur = cur.next; i++; } return true; } /** * 返回节点的长度 * @return */ public int length() { int len = 0; ListNode tmp = head; while(tmp != null) { len++; tmp = tmp.next; } return len; } /** * 对链表进行排序 * 返回排序后的头节点 */ public ListNode orderList() { ListNode next = null; int tmp = 0; ListNode cur = head; while(cur.next != null) { next = cur.next; while(next != null) { if(cur.val > next.val) { tmp = cur.val; cur.val = next.val; next.val = tmp; } next = next.next; } cur = cur.next; } return head; } //打印链表 public void print() { ListNode node = head; while(node != null) { System.out.print("->" + node.val); node = node.next; } System.out.println(); } public static void main(String[] args) { Solution list = new Solution(); list.addNode(3); list.addNode(1); list.addNode(5); list.addNode(3); list.deleteNode(1); System.out.println("len = " + list.length()); System.out.println("before order: "); list.print(); list.orderList(); System.out.println("after order: "); list.print(); } }