category
date
Apr 23, 2023
icon
Origin
password
slug
alibishi
status
Published
summary
tags
Tags
type
Post
URL
题目一:
有字符串yujwedjhccdskdsewirewrwsadm,fnsdklwweqpq取出所有所有所有两个w之间的字符串并用归并排序进行排序 (字符串处理)。java语言实现。
import java.util.*; public class MergeSortBetweenWW { public static void main(String[] args) { String str = "yujwedjhccdskdsewirewrwsadm,fnsdklwweqpq"; String[] words = extractWords(str); System.out.println("Before sorting: " + Arrays.toString(words)); mergeSort(words, 0, words.length-1); System.out.println("After sorting: " + Arrays.toString(words)); } // Extracts all substrings between two 'w's and returns them in an array public static String[] extractWords(String str) { List<String> wordsList = new ArrayList<>(); int startIndex = str.indexOf('w'); while (startIndex != -1) { int endIndex = str.indexOf('w', startIndex+1); if (endIndex == -1) { break; } String word = str.substring(startIndex+1, endIndex); wordsList.add(word); startIndex = endIndex; } String[] wordsArr = new String[wordsList.size()]; wordsArr = wordsList.toArray(wordsArr); return wordsArr; } // Sorts a given array using merge sort algorithm public static void mergeSort(String[] arr, int leftIndex, int rightIndex) { if (leftIndex < rightIndex) { int midIndex = (leftIndex + rightIndex) / 2; mergeSort(arr, leftIndex, midIndex); mergeSort(arr, midIndex+1, rightIndex); merge(arr, leftIndex, midIndex, rightIndex); } } // Merges two sorted sub-arrays into a single sorted array public static void merge(String[] arr, int leftIndex, int midIndex, int rightIndex) { String[] tempArr = new String[arr.length]; for (int i = leftIndex; i <= rightIndex; i++) { tempArr[i] = arr[i]; } int i = leftIndex; int j = midIndex + 1; int k = leftIndex; while (i <= midIndex && j <= rightIndex) { if (tempArr[i].compareTo(tempArr[j]) <= 0) { arr[k] = tempArr[i]; i++; } else { arr[k] = tempArr[j]; j++; } k++; } while (i <= midIndex) { arr[k] = tempArr[i]; k++; i++; } } } }
在这个实现中,首先使用
extractWords 函数提取原始字符串中的所有子字符串,并存储在一个字符串数组中。然后使用 mergeSort 函数对该字符串数组进行归并排序,其中 merge 函数用于将两个子数组合并为一个排序的子数组。最后,打印排序后的数组。请注意,如果原始字符串中没有至少两个“w”,则提取的子字符串数组将为空,因此排序不会执行。
题目二:
有100个任务需要分成10批执行,每批执行有顺序(注意注意注意:即第一批执行完执行第二批)。10批任务有序执行,每批任务的10个任务要做到并发执行如果可以,写出多种不同原理或不同工具的实现方式 java语言实现
方案一:使用线程池
该方案使用
java.util.concurrent 中的线程池实现,并发执行每批任务的10个任务。import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class TaskBatchesExecutor { public static void main(String[] args) throws InterruptedException { int numTasks = 100; int batchSize = 10; int numBatches = numTasks / batchSize; ExecutorService executor = Executors.newFixedThreadPool(batchSize); for (int i = 0; i < numBatches; i++) { List<Runnable> tasks = new ArrayList<>(); for (int j = 0; j < batchSize; j++) { int taskId = i * batchSize + j; tasks.add(() -> executeTask(taskId)); } executor.invokeAll(tasks); } executor.shutdown(); executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } private static void executeTask(int taskId) { System.out.println("Executing task #" + taskId + " on thread " + Thread.currentThread().getName()); // Execute the task here } }
在该实现中,我们首先定义了任务的总数,每批执行的任务数以及总批数。然后,我们使用
Executors.newFixedThreadPool创建一个固定大小的线程池,其中线程数等于每批任务的数量。接下来,我们循环每批任务,为每批任务创建一个 List,其中包含10个 Runnable
,每个 Runnable对象代表一个任务。然后,我们使用 executor.invokeAll并发执行每批任务的所有10个任务。最后,我们使用 executor.shutdown()关闭线程池,并使用 executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)等待所有任务完成执行。方案二:使用 CountDownLatch
该方案使用
java.util.concurrent 中的 CountDownLatch 实现,每批任务的10个任务将在 CountDownLatch 计数器为0时并发执行。import java.util.concurrent.CountDownLatch; public class TaskBatchesExecutor { public static void main(String[] args) throws InterruptedException { int numTasks = 100; int batchSize = 10; int numBatches = numTasks / batchSize; CountDownLatch latch = new CountDownLatch(batchSize); for (int i = 0; i < numBatches; i++) { for (int j = 0; j < batchSize; j++) { int taskId = i * batchSize + j; new Thread(() -> { executeTask(taskId); latch.countDown(); }).start(); } latch.await(); latch = new CountDownLatch(batchSize); } } private static void executeTask(int taskId) { System.out.println("Executing task #" + taskId + " on thread " + Thread.currentThread().getName()); // Execute the task here } }
题目三:
有如下两张表,请统计各公司每天的营业额和各公司每天的累积营业额,说明:累积营业额是指:比如第一天是100,第二天是200,那么第二天累积营业额就是300 表: order表
订单ID 交易日期 金额 门店ID
order_id trade_date amount shop_id
表:shop 门店表
门店ID 公司ID
shop_id company_id
如何建索引能提高查询速度
SELECT o.trade_date, s.company_id, SUM(o.amount) AS daily_revenue, SUM(SUM(o.amount)) OVER (PARTITION BY s.company_id ORDER BY o.trade_date) AS cumulative_revenue FROM order o INNER JOIN shop s ON o.shop_id = s.shop_id GROUP BY o.trade_date, s.company_id;
该查询语句使用了
INNER JOIN 操作将 order 表和 shop 表连接起来,以获取每个订单对应的公司ID。接着,我们使用 GROUP BY 将数据按照交易日期和公司ID分组,统计每天每个公司的营业额。最后,我们使用窗口函数 SUM() OVER() 计算每个公司每天的累积营业额。为了提高查询速度,我们可以对以下列进行索引:
order表中的trade_date、amount和shop_id列。
shop表中的shop_id和company_id列。
在
order 表中,对 trade_date、amount 和 shop_id 列建立组合索引,可以加速按照交易日期和门店ID分组的查询操作。在 shop 表中,对 shop_id 和 company_id 列建立联合索引,可以加速按照门店ID连接订单表的查询操作。如果查询条件还涉及其他列,可以考虑对相应列建立单列索引或组合索引,以提高查询速度。题目四:
请说说这段代码错误在哪里,如何修改(写出修改后的代码,java语言实现)说明:输入的list 中的str串都不为null,但可能是"".
public void sort(List<String> list) { Collections.sort(list, (o1, o2) -> { if ("".equals(o1) || "".equals(o2)){ return 0; } return o1.compareTo(o2); }); }
这段代码的错误在于当
o1 或 o2 为空字符串时,返回了 0,这会导致空字符串没有被正确排序。应该将空字符串排在列表的最前面或最后面。为了解决这个问题,可以修改代码如下:
public void sort(List<String> list) { Collections.sort(list, (o1, o2) -> { if (o1.isEmpty() && o2.isEmpty()) { return 0; } else if (o1.isEmpty()) { return -1; } else if (o2.isEmpty()) { return 1; } else { return o1.compareTo(o2); } }); }
在修改后的代码中,我们首先判断
o1和 o2是否为空字符串,如果是,则它们的排序优先级相等,返回 0;如果只有其中一个为空字符串,那么我们把它放在另一个非空字符串的前面或后面,以满足空字符串也能够被正确排序;如果两个字符串都不为空,则使用 compareTo()方法进行排序。- 作者:LiuJixue
- 链接:https://liujixue.cn/article/alibishi
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。





