赞
踩
分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击人工智能教程
- package live.every.day.Programming;
-
- /**
- * 从字符串"4hj2*7dnsk3772"中提取数字并按正序输出。
- *
- * @author Created by LiveEveryDay
- */
-
- import java.util.Arrays;
-
- public class ExtractNumbersAndSort {
-
- public static void main(String[] args) {
- String str = "4hj2*7dnsk3772";
- char[] charArr = str.toCharArray();
- int numCount = 0;
- for (char c : charArr) {
- if (c >= '0' && c <= '9') {
- numCount++;
- }
- }
- int[] intArr = new int[numCount];
- for (int i = 0, j = 0; i < charArr.length; i++) {
- if (charArr[i] >= '0' && charArr[i] <= '9') {
- intArr[j++] = charArr[i] - '0';
- }
- }
- quickSort(intArr, 0, intArr.length - 1);
- System.out.println(Arrays.toString(intArr));
- }
-
- public static void quickSort(int[] a, int low, int high) {
- if (low >= high) {
- return;
- }
- int pivot = oneTripQuickSort(a, low, high);
- quickSort(a, low, pivot - 1);
- quickSort(a, pivot + 1, high);
- }
-
- private static int oneTripQuickSort(int[] a, int low, int high) {
- int i = low;
- int j = high;
- int pivot = a[i];
- while (i < j) {
- while (i < j && a[j] >= pivot) {
- j--;
- }
- a[i] = a[j];
- while (i < j && a[i] <= pivot) {
- i++;
- }
- a[j] = a[i];
- }
- a[i] = pivot;
- return i;
- }
-
- }
-
- // Output:
- /*
- [2, 2, 3, 4, 7, 7, 7]
- */

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。