1. 按要求实现程序功能
从键盘输入5个整型值
1)按从大到小顺序排序方法;
2)计算这些数的平均值的方法;
3)在主方法中调用这些方法,并输出相应的值。
出处:
https://edu.csdn.net/practice/23497440
代码:
import java.util.*; public class Solution { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); Scanner scanner = new Scanner(System.in); for (int a = 1; a < 6; a++) { System.out.print("请输入第 " + a + " 个值:"); list.add(scanner.nextInt()); } System.out.println(descending(list)); System.out.println(getAvg(list)); } public static List<Integer> descending(List<Integer> list) { list.sort(Collections.reverseOrder()); return list; } public static Double getAvg(List<Integer> list) { if (list.isEmpty()) { return 0.0; } return list.stream().mapToInt(Integer::new).average().getAsDouble(); } }
Note: Main.java uses or overrides a deprecated API.
2. 最长连续序列
给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
示例 1:
输入:nums = [100,4,200,1,3,2]
输出:4
解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
示例 2:
输入:nums = [0,3,7,2,5,8,4,6,0,1]
输出:9
提示:
0 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
出处:
https://edu.csdn.net/practice/23497441
代码:
import java.util.HashSet; import java.util.Set; public class longestConsecutive { public static class Solution { public int longestConsecutive(int[] nums) { Set<Integer> num_set = new HashSet<Integer>(); for (int num : nums) { num_set.add(num); } int longestStreak = 0; for (int num : num_set) { if (!num_set.contains(num - 1)) { int currentNum = num; int currentStreak = 1; while (num_set.contains(currentNum + 1)) { currentNum += 1; currentStreak += 1; } longestStreak = Math.max(longestStreak, currentStreak); } } return longestStreak; } } public static void main(String[] args) { Solution s = new Solution(); int[] nums = {100,4,200,1,3,2}; System.out.println(s.longestConsecutive(nums)); int[] nums2 = {0,3,7,2,5,8,4,6,0,1}; System.out.println(s.longestConsecutive(nums2)); } }
输出:
4
9
3. 简化路径
给你一个字符串 path ,表示指向某一文件或目录的 Unix 风格 绝对路径 (以 '/' 开头),请你将其转化为更加简洁的规范路径。
在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,'//')都被视为单个斜杠 '/' 。 对于此问题,任何其他格式的点(例如,'...')均被视为文件/目录名称。
请注意,返回的 规范路径 必须遵循下述格式:
始终以斜杠 '/' 开头。
两个目录名之间必须只有一个斜杠 '/' 。
最后一个目录名(如果存在)不能 以 '/' 结尾。
此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 '.' 或 '..')。
返回简化后得到的 规范路径 。
示例 1:
输入:path = "/home/"
输出:"/home"
解释:注意,最后一个目录名后面没有斜杠。
示例 2:
输入:path = "/../"
输出:"/"
解释:从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。
示例 3:
输入:path = "/home//foo/"
输出:"/home/foo"
解释:在规范路径中,多个连续斜杠需要用一个斜杠替换。
示例 4:
输入:path = "/a/./b/../../c/"
输出:"/c"
提示:
1 <= path.length <= 3000
path 由英文字母,数字,'.','/' 或 '_' 组成。
path 是一个有效的 Unix 风格绝对路径。
出处:
https://edu.csdn.net/practice/23497442
代码: 使用栈模拟路径遍历
import java.util.Deque; import java.util.ArrayDeque; public class simplifyPath { public static class Solution { public String simplifyPath(String path) { Deque<String> stack = new ArrayDeque<>(); for (String str : path.split("/")) { if ("".equals(str) || ".".equals(str)) continue; stack.push(str); } StringBuilder sb = new StringBuilder(); int count = 0; while (!stack.isEmpty()) { String str = stack.pop(); if ("..".equals(str)) count++; else { if (count > 0) { count--; continue; } sb.insert(0, str); sb.insert(0, "/"); } } if (sb.length() == 0) sb.append("/"); return sb.toString(); } } public static void main(String[] args) { Solution s = new Solution(); String path = "/home/"; System.out.println(s.simplifyPath(path)); path = "/../"; System.out.println(s.simplifyPath(path)); path = "/home//foo/"; System.out.println(s.simplifyPath(path)); path = "/a/./b/../../c/"; System.out.println(s.simplifyPath(path)); } }
输出:
/home
/
/home/foo
/c
代码2: 正则 regex
import java.util.List; import java.util.ArrayList; import java.util.Arrays; import java.util.Stack; public class simplifyPath { public static class Solution { public static String simplifyPath(String path) { String regex = "/+"; String[] arr = path.split(regex); List<String> list = new ArrayList<>(Arrays.asList(arr)); list.removeIf(str -> "".equals(str) || ".".equals(str)); Stack<String> stack = new Stack<>(); for (String dir : list) { if ("..".equals(dir)) { if (!stack.isEmpty()) { stack.pop(); } } else { stack.push(dir); } } String result = String.join("/", stack); return "/" + result; } } public static void main(String[] args) { Solution s = new Solution(); String path = "/home/"; System.out.println(s.simplifyPath(path)); String path1 = "/../"; System.out.println(s.simplifyPath(path1)); String path2 = "/home//foo/"; System.out.println(s.simplifyPath(path2)); String path3 = "/a/./b/../../c/"; System.out.println(s.simplifyPath(path3)); } }