本文共 2775 字,大约阅读时间需要 9 分钟。
问题1:
import java.util.ArrayList;import java.util.HashSet;import java.util.Set;/** * 最长子序列 * 思路分析 :找出所有子序列 判断是否是满足规则的子序列 * 找出所有子序列 采用位运算的形式 */public class MaxChildCollection { private static int result = 0; private static SetresultSet = new HashSet (); //找出该序列的所有子集 //1-8的二进制表示 是将每一位上的01 都组合过的 若用这个01表示数字不采用或采用 则能表示出nums数组的所有子数组 包括null public static int findChild(int[] nums, int target) { int len = nums.length; int temp = len == 0 ? 0 : 1 << len;//一共有多少个子集 for (int i = 0; i < temp; i++) { //举出所有01的组合 int index = i; ArrayList list = new ArrayList (); for (int j = 0; j < len; j++) { if ((index & 1) == 1) { list.add(nums[j]); } index >>= 1; } if (list != null && list.size() > 0) { //所有子序列中含有空值 if (meet(list, target)) { boolean add = resultSet.add(list); if (add) { result++; } } } } return result; } /** * 判断是否为符合要求的子序列 * * @param list * @param target * @return */ public static boolean meet(ArrayList list, int target) { if (list.size() == 1) { //若list里只有一个值 不用找他的最大值和最小值 return list.get(0) <= target ? true : false; } int[] minAndMax = findMinAndMax(list); return minAndMax[0] + minAndMax[1] <= target ? true : false; } /** * 找出数组中的最大值和最小值 */ public static int[] findMinAndMax(ArrayList list) { int[] nums = new int[2]; int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int temp : list) { if (temp < min) { min = temp; } if (temp > max) { max = temp; } } nums[0] = min; nums[1] = max; return nums; } public static void main(String[] args) { int[] nums = { 1, 2, 3}; int result = findChild(nums, 3); System.out.println(result); for (ArrayList list : resultSet) { System.out.println(list); } System.out.println(resultSet.size()); }}
问题二:
n个元素的序列含有2的次方个子序列 含有2的n次方-1个非空子序列问题四:
当三个线程都各自持有自己需要的资源数 -1 时 系统没有其他资源 此时系统一定会因为缺少这一个资源而发生死锁 若此时 系统增加一个资源 那么即可推动系统继续运行 所以系统需要的最少资源数 为 (2-1)+ (3-1) + (4-1)+1=7个问题五: udp传输报文超过最大传输长度MTU 就会在ip层被分片 反之不会 分片之后第一段报文带着udp的头部信息 其余带着ip信息 在传输层重组 如果这其中有一片报文丢失 整段报文失效 udp不可靠传输体现 因此,在普通的局域网环境下,将UDP的数据控制在1472字节以下为好。
而TCP报文段的每个分段中都有TCP首部,到了端点后根据TCP首部的信息在传输层进行重组。IP数据报分片后,只有到达目的地后才进行重组,而不是向其他网络协议,在下一站就要进行重组。转载地址:http://aenhb.baihongyu.com/