FRQ Cheatsheet

FRQ1

String常考思路

  1. 替换某个subStr,其他内容不变

举例:bigStr = “I enjoy APCS”, 将第一次出现的enjoy替换为like,其他位置的内容不变,返回新的字符串。

String bigStr = "I enjog APCS";
String subStr = "enjoy";
String repl = "like";

int pos = bigStr.indexOf(subStr);
String result = bigStr.substring(0, pos)+repl+bigStr.substring(pos+subStr.length());

  1. 判断一个长String是否包含subStr,或包含多少个subStr

可能要用到的方法:indexOf, substring

举例:WordMatch

  • For循环思路:从长String的第一个字符开始,index每次移动一个位置,判断当前位置是否与subStr相同。

  • While循环思路:从长String bigStr开始,判断bigStr是否包含subStr; 如果包含,bigStr从第一个subStr的下一个位置开始截取。

注意:上题中subStr是可以重叠的。如果题目要求subStr不能重叠,那么使用while循环,并且index移动距离为subStr长度。

1D Array

  1. 返回最大值

  1. 返回最小值

  1. 返回最大值的index

  1. 返回最小值的index

  1. Reverse数组

循环只需要执行一半,否则数组会reverse两次,导致数组不变。

  1. 判断数组为递增序列

思路:找反例!

  1. 判断是否有peak,即中间值同时大于前一个值和后一个值

  1. 返回最长连续序列起始位置

Ex. [7, 10, 10, 15, 15, 15, 15, 3, 3, 3, 3, 3, 3],最长连续序列是6个3,返回序列的起始位置,也就是7。

方法:起始位置 = 结束位置 - 连续长度 + 1

  1. 返回target的最长连续长度

ArrayList:

包含两个class,一个为常规classA,另一个classB包含ArrayList类型instance variable,ArrayList包含的元素类型是classA的Object。

Ex1. WordPair是常规class,WordPairList包含WordPair类型的ArrayList<allPairs>.

a. 完成classB的constructor,即完成ArrayList的初始化

方法:

  • private ArrayList<WordPair> allPairs已经声明过,在constructor里不要重复声明

  • 初始化allPairs,即add相应的WordPair object到allPairs,生成wordPair object的方法即为调用WordPair class的构造方法

  • 所以要弄清楚WordPair的object是由哪些变量组成,仔细阅读WordPair的构造方法

本题可以看出构造WordPair需要两个String类型变量,根据题目,理解出这两个String变量对应题目中数组的哪些元素。

FRQ-1

b. Write the WordPairList method numMatches. This method returns the number of WordPair objects in allPairs for which the two strings match.

b. (高频考点)因为ArrayList中的每个元素都是classA的object,所以ArrayList的每个元素都可以调用classA中的method(通常是getter method),获取classA object中的实例变量,并与参数进行比较。

根据比较结果,对ArrayList进行相应的add,remove,返回极值等操作。

模版:

  • Add/Remove某些元素

Remove元素要倒序遍历,否则会跳过元素!

  • 返回极值

Ex2:

a) Write the StudentAnswerSheet method getScore. The parameter passed to method getScore is an ArrayList of strings representing the correct answer key for the test being scored. The method computes and returns a double that represents the score for the student's test answers when compared with the answer key. One point is awarded for each correct answer and . of a point is deducted for each incorrect answer. Omitted answers (indicated by"?") do not change the student's score.

思路:循环answer里所有元素,与key的对应元素进行比较

b) Consider the following class that represents the test results of a group of students that took a multiple-choice test.

Ex3.

1D Array/ArrayList elements to 2D Array

方法:遍历2D Array,循环里更新1D Array的index

FRQ-2

Ex4. FRQ4

2D Array

Ex1. Put all elements of a single row/column to 1D Array

Ex2. A square is labeled with a positive number if and only if:

  • the square is white and

  • the square does not have a white square immediately above it, or it does not have a white square immediately to its left, or both.

The squares identified by these criteria are labeled with consecutive numbers in row-major order, starting at 1.

要注意边界问题!

  • 行号最大值是arr2D.length-1

  • 列号最大值是array[0].length-1

  • row[0]没有upper元素

  • col[0]没有left元素

  • row[arr.length-1]没有down元素

  • col[arr[0].length-1]没有right元素

(a) Write the Crossword method toBeLabeled. The method returns true if the square indexed by row r, column c in a crossword puzzle grid should be labeled with a positive number according to the crossword labeling rule; otherwise it returns false. The parameter blackSquares indicates which squares in the crossword puzzle grid are black.

Ex3.

FRQ-3

注意边界!

Ex4.

Traverse all elements of 2D Arrays

Given a String 2D Arrays , return a new 2D Array, which contains the length of each corresponding element. The size of the new array should be the same as the given array.

Last updated