常用知识点

排序算法:

①归并排序算法模板:

 1 public class Solution {
 2     public void sortInteger(int[] a) {
 3         if (a == null || a.length == 0) {
 4             return;
 5         }
 6         int[] temp = new int[a.length];
 7         mergeDivideSort(a, 0, a.length - 1, temp);
 8     }
 9     public void mergeDivideSort(int[] a, int start, int end, int[] temp) {
10         //归并排序,不断分组直到分成单个数字
11         if (start >= end) {
12             return;
13         }
14         int mid = start + (end - start) / 2;
15         mergeSort(a, start, mid, temp);
16         mergeSort(a, mid + 1, end, temp);
17         merge(a, start, end, temp);
18     }
19     public void merge(int[] a, int start, int end, int[] temp) {
20         //将两个排序的组进行合并
21         int mid = start + (end - start) / 2;
22         int leftIndex = start;
23         int rightIndex = mid + 1;
24         int index = start;
25         while (leftIndex <= mid && rightIndex <= end) {
26             if (a[leftIndex] < a[rightIndex]) {
27                 temp[index++] = a[leftIndex++];
28             } else {
29                 temp[index++] = a[rightIndex++];
30             }
31         }
32         while (leftIndex <= mid) {
33             temp[index++] = a[leftIndex++];
34         }
35         while (rightIndex <= end) {
36             temp[index++] = a[rightIndex++];
37         }
38         //注意复制的时候从start到end
39         for (int i = start; i <= end; i++) {
40             a[i] = temp[i];
41         }
42     }
43 }
View Code

②快速排序算法模板:

 1 public class Solution {
 2     /**
 3      * @param A an integer array
 4      * @return void
 5      */
 6     public void sortIntegers2(int[] a) {
 7         if (a == null || a.length == 0) {
 8             return;
 9         }
10         quickSort(a, 0, a.length - 1);
11     }
12     public void quickSort(int[] a, int start, int end) {
13         if (start >= end) {
14             return;
15         }
16         int mid = start + (end - start) / 2;
17         int pivot = a[mid];
18         int left = start;
19         int right = end;
20         while (left <= right) {
21             //从小到大排序,a[left] < pivot,从大到小排序,a[left] > pivot
22             while (left <= right && a[left] < pivot) {
23                 left++;
24             }
25             while (left <= right && a[right] > pivot) {
26                 right--;
27             }
28             if (left <= right) {
29                 int temp = a[left];
30                 a[left] = a[right];
31                 a[right] = temp;
32             }
33         }
34         //经过快速排序之后只可能有两种情况:第一种right-pivot-left,第二种right-left
35         quickSort(a, start, right);
36         quickSort(a, left, end);
37     }
38 }
View Code

 数字与字符串互相转化:

1如何将字串 String 转换成整数 int?

  A. 有两个方法:

  1). int i = Integer.parseInt([String]); 或

  i = Integer.parseInt([String],[int radix]);

  2). int i = Integer.valueOf(my_str).intValue();

  注: 字串转成 Double, Float, Long 的方法大同小异.

  2 如何将整数 int 转换成字串 String ?

  A. 有叁种方法:

  1.) String s = String.valueOf(i);

  2.) String s = Integer.toString(i);

  3.) String s = "" + i;

  注: Double, Float, Long 转成字串的方法大同小异.

posted @ 2017-06-06 15:08  木子月月鸟  阅读(211)  评论(0编辑  收藏  举报