HW0b
约 919 字大约 3 分钟
2025-09-27
简单数组训练
任务一
返回数组 array [1, 2, 3, 4, 5, 6]
/** Returns an array [1, 2, 3, 4, 5, 6] */
public static int[] makeDice() {
// TODO: Fill in this function.
return new int[]{1, 2, 3, 4, 5, 6};
}任务二
* Returns the order depending on the customer.
* If the customer is Ergun, return ["beyti", "pizza", "hamburger", "tea"]. * If the customer is Erik, return ["sushi", "pasta", "avocado", "coffee"]. * In any other case, return an empty String[] of size 3.
public static String[] takeOrder(String customer) {
// TODO: Fill in this function.
if ("Ergun".equals(customer)) {
return new String[]{"beyti", "pizza", "hamburger", "tea"};
} else if ("Erik".equals(customer)) {
return new String[]{"sushi", "pasta", "avocado", "coffee"};
} else {
return new String[3];
}
}任务三
返回给定数组中最大元素和最小元素之间的正差值。假定给定的数组是非空的
public static int findMinMax(int[] array) {
// TODO: Fill in this function.
int min = array[0];
int max = array[0];
for (int i : array) {
if (i > max) {
max = i;
}
if (i < min) {
min = i;
}
}
return max - min;
}思路是新建两个变量分别储存最大值与最小值,遍历后更新变量的值
任务四 冰雹序列
目标: 使用递归来计算从输入数字 n 开始的冰雹序列 (hailstone sequence),并将该序列以一个整数列表(List<Integer>)的形式返回。
冰雹序列的规则如下:
从一个正整数
n开始。如果
n是偶数,则将n除以 2。如果
n是奇数,则将n乘以 3 再加 1。重复这个过程,直到
n变为 1。
private static List<Integer> hailstoneHelper(int x, List<Integer> list) {
// TODO: Fill in this function.
list.add(x);
if (x == 1){
return list;
}
if (x % 2 ==0) {
return hailstoneHelper(x / 2, list);
} else {
return hailstoneHelper(x * 3 + 1, list);
}
}采用了递归的思想,根据正课中的 IntList 即可理解链表、递归的思想
ListExpercises
任务一:返回总和
/** Returns the total sum in a list of integers */
public static int sum(List<Integer> L) {
// TODO: Fill in this function.
int sum = 0;
for (int num : L){
sum += num;
} return sum;
}任务二:返回列表内偶数
/** Returns a list containing the even numbers of the given list */
public static List<Integer> evens(List<Integer> L) {
// TODO: Fill in this function.
List<Integer> evens = new ArrayList<>();
for (int num : L){
if (num % 2 == 0){
evens.add(num);
}
} return evens;
}任务三: 返回两列表内相同的数
/** Returns a list containing the common item of the two given lists */ 第一种,嵌套循环
public static List<Integer> common(List<Integer> L1, List<Integer> L2) {
List<Integer> common = new ArrayList<>();
for (int num1 : L1){
for (int num2 : L2){
if (num1 == num2){
common.add(num1);
break;
}
}
} return common;
}第二种,contains
public static List<Integer> common(List<Integer> L1, List<Integer> L2){
List<Integer> common = new Arrary<>();
for (int num1 : L1){
if (L2.contains(num1)){
common.add(num1)
}
} return common;
}任务四 返回字符 c 在列表出现的次数
public static int countOccurrencesOfC(List<String> words, char c) {
// TODO: Fill in this function.
int times = 0;
for (String word : words) {
for (char letter : word.toCharArray()) {
if (letter == c) {
times++;
}
}
} return times;
}还可以使用charAt进行查找
MapExercise
任务一 字母数字映射表
Returns a map from every lower case letter to the number corresponding to that letter, where 'a' is
1, 'b' is 2, 'c' is 3, ..., 'z' is 26.
public static Map<Character, Integer> letterToNum() {
// TODO: Fill in this function.
Map<Character, Integer> letterToNum = new HashMap<>();
int num = 1;
for (char letter = 'a'; letter <= 'z'; letter++) {
letterToNum.put(letter, num);
num++;
}
return letterToNum;
}采用了 for 循环对 char 进行连续赋值
任务二 返回平方
Returns a map from the integers in the list to their squares. For example, if the input list
is [1, 3, 6, 7], the returned map goes from 1 to 1, 3 to 9, 6 to 36, and 7 to 49.
public static Map<Integer, Integer> squares(List<Integer> nums) {
// TODO: Fill in this function.
Map<Integer, Integer> squares = new HashMap<>();
for (Integer num1 : nums) {
squares.put(num1, num1 * num1);
}
return squares;
}任务三 返回字母出现次数
Returns a map of the counts of all words that appear in a list of words.
public static Map<String, Integer> countWords(List<String> words) {
// TODO: Fill in this function.
Map<String, Integer> wordCount = new HashMap<>();
for (String word : words) {
if (wordCount.containsKey(word)) {
wordCount.put(word, wordCount.get(word) + 1);
} else {
wordCount.put(word, 1);
}
}
return wordCount;
}