java降低if的圈复杂度_几种减少圈复杂度的实践

1、类型判断、转换

if (visibleFlag) {

visible = 1;

} else {

visible = 0;

}

改为(如:在数据库保存时无法自动转换数据类型):

visible = visibleFlag + 0

2、独立函数调用

postTitle = postTitle ? postTitle.trim() : '';

改为:

postTitle = util.trim(xss.sanitize(postTitle))

3、多条件合并为一个:转为数组的include等操作

if (action === 'create' || action === 'edit') {...}

改为:

if (['create', 'edit'].includes(action)) {...}

4、空值判断简化

if (page !== undefined && page !== ''){...}

改为

if (!page) {...}

5、嵌套if简化

if (x) {

if (y) {...} else {...}

} else {

...

}

改为:

if (x && y) {...} else {...}

6、复杂if改为使用正则(通配符)

如:

if (/^1[34578]\d{9}$/i.test(phone)) {...}

7、重复判断剥离并复用:赋值给变量、常量,减少二次判断

if (postStatus === 'draft' || postStatus === 'auto-draft') {...}

...

if (postStatus === 'draft' || postStatus === 'auto-draft') {...} else {...}

改为:

const postDraft = postStatus === 'draft' || postStatus === 'auto-draft';

if (postDraft) {...}

8、关联表查询、子查询改为视图查询

参考:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 快速排序是一种常用的排序算法,使用 Java 实现快速排序有以下几种方法: 1. 递归方法 ``` public class QuickSort { public static void sort(int[] arr, int low, int high) { if (arr == null || arr.length == 0) return; if (low >= high) return; int middle = low + (high - low) / 2; int pivot = arr[middle]; int i = low, j = high; while (i <= j) { while (arr[i] < pivot) { i++; } while (arr[j] > pivot) { j--; } if (i <= j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } if (low < j) sort(arr, low, j); if (high > i) sort(arr, i, high); } public static void main(String[] args) { int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1}; sort(arr, 0, arr.length-1); for (int i : arr) { System.out.print(i + " "); } } } ``` 2. 非递归方法 ``` import java.util.Stack; public class QuickSort2 { public static void sort(int[] arr) { if (arr == null || arr.length == 0) return; Stack<Integer> stack = new Stack<Integer>(); stack.push(0); stack.push(arr.length - 1); while (!stack.isEmpty()) { int high = stack.pop(); int low = stack.pop(); int pivot = partition(arr, low, high); if (pivot - 1 > low) { stack.push(low); stack.push(pivot - 1); } if (pivot + 1 < high) { stack.push(pivot + 1); stack.push(high); } } } public static int partition(int[] arr, int low, int high) { int pivot = arr[low]; int i = low + 1; int j = high; while (i <= j) { while (i <= j && arr[i] < pivot) { i++; } while (i <= j && arr[j] > pivot) { j--; } if (i <= j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] ### 回答2: 快速排序是一种常用的排序算法,它的基本思想是通过划分将数据分成较小和较大的两部分,递归地对两部分进行排序,最终实现整个序列的有序。 在Java中实现快速排序的几种常用方法如下: 1. 递归实现: ```java public static void quickSort(int[] arr, int low, int high) { if (low < high) { int partitionIndex = partition(arr, low, high); quickSort(arr, low, partitionIndex - 1); quickSort(arr, partitionIndex + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[low]; while (low < high) { while (low < high && arr[high] >= pivot) { high--; } arr[low] = arr[high]; while (low < high && arr[low] <= pivot) { low++; } arr[high] = arr[low]; } arr[low] = pivot; return low; } ``` 2. 非递归实现: ```java public static void quickSort(int[] arr) { if (arr == null || arr.length <= 1) { return; } Stack<Integer> stack = new Stack<>(); stack.push(0); stack.push(arr.length - 1); while (!stack.isEmpty()) { int high = stack.pop(); int low = stack.pop(); int partitionIndex = partition(arr, low, high); if (low < partitionIndex - 1) { stack.push(low); stack.push(partitionIndex - 1); } if (high > partitionIndex + 1) { stack.push(partitionIndex + 1); stack.push(high); } } } public static int partition(int[] arr, int low, int high) { int pivot = arr[low]; while (low < high) { while (low < high && arr[high] >= pivot) { high--; } arr[low] = arr[high]; while (low < high && arr[low] <= pivot) { low++; } arr[high] = arr[low]; } arr[low] = pivot; return low; } ``` 以上就是使用Java实现快速排序的几种常用方法和代码。快速排序的时间复杂度为O(nlogn),是一种高效的排序算法。 ### 回答3: 快速排序是一种经典的排序算法,可以使用多种方式实现。下面介绍两种常见的 Java 实现方法。 一种常见的快速排序实现方法是使用递归。首先选择一个基准元素(通常是数组的第一个元素),将数组分成两个部分,小于基准元素的放在左边,大于基准元素的放在右边。然后再递归地对左右两个部分进行快速排序。 ```java public class QuickSort { public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pivot = partition(arr, low, high); quickSort(arr, low, pivot - 1); quickSort(arr, pivot + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[low]; while (low < high) { while (low < high && arr[high] >= pivot) { high--; } arr[low] = arr[high]; while (low < high && arr[low] <= pivot) { low++; } arr[high] = arr[low]; } arr[low] = pivot; return low; } } ``` 另一种常见的快速排序实现方法是使用栈。首先将初始的 low 和 high 压入栈中,然后循环处理栈顶的元素,直到栈为空。在每次循环中,先弹出栈顶的 low 和 high,并执行划分过程,将得到的两个新的 low 和 high 压入栈中。 ```java public class QuickSort { public static void quickSort(int[] arr, int low, int high) { Stack<Integer> stack = new Stack<>(); stack.push(low); stack.push(high); while (!stack.isEmpty()) { high = stack.pop(); low = stack.pop(); int pivot = partition(arr, low, high); if (low < pivot - 1) { stack.push(low); stack.push(pivot - 1); } if (pivot + 1 < high) { stack.push(pivot + 1); stack.push(high); } } } public static int partition(int[] arr, int low, int high) { int pivot = arr[low]; while (low < high) { while (low < high && arr[high] >= pivot) { high--; } arr[low] = arr[high]; while (low < high && arr[low] <= pivot) { low++; } arr[high] = arr[low]; } arr[low] = pivot; return low; } } ``` 这两种实现方法都基于相同的快速排序思想,只是在具体的实现上略有不同。快速排序的时间复杂度通常为 O(nlogn),是一种效率较高的排序算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值