public static int count(String bigStr, String subStr) {
int count = 0;
while(bisStr.indexOf(subStr) != -1) {
int pos = bigStr.indexOf(subStr);
bigStr = bigStr.substring(pos+subStr.length());
count++;
}
return count;
}
1D Array
返回最大值
public int findBiggest(int[] arr)
{
int max = arr[0]; // 或 int max = MIN_VALUE;
for(int i=0; i < arr.length; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}
返回最小值
public int findSmallest(int[] arr)
{
int min = arr[0]; // 或 int min = MAX_VALUE;
for(int i=0; i < arr.length; i++)
{
if (arr[i] < min)
{
min = arr[i];
}
}
return min;
}
返回最大值的index
public int findBiggestIndex(int[] arr)
{
int indexMax = 0;
for(int i=0; i < arr.length; i++)
{
if (arr[i] > arr[indexMax])
{
indexMax = i;
}
}
return indexMax;
}
返回最小值的index
public int findSmallestIndex(int[] arr)
{
int indexMin = 0;
for(int i=0; i < arr.length; i++)
{
if (arr[i] <= arr[indexMin])
{
indexMin = i;
}
}
return indexMin;
}
Reverse数组
public void reverseArray(int[] arr)
{
for (int i = 0; i < arr.length/2; i++)
{
int temp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
}
循环只需要执行一半,否则数组会reverse两次,导致数组不变。
判断数组为递增序列
思路:找反例!
public boolean isIncreasing(int[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
{
if (arr[i+1] < arr[i]) // 注意数组的索引是从0开始,此处判断当前元素与下一个元素,若大则返回false
{
return false;
}
}
return true;
}
public boolean isDecreasing(int[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
{
if (arr[i+1] > arr[i])
{
return false;
}
}
return true;
}
判断是否有peak,即中间值同时大于前一个值和后一个值
public boolean havePeak(int[] arr)
{
for (int i = 1; i < arr.length - 1; i++)
{
if (arr[i] > arr[i-1] && arr[i] > arr[i+1]) // 判断中间的元素是否比它的前一个和后一个都大
{
return true;
}
}
return false;
}
public class WordPair {
public WordPair(String first, String second)
public String getFirst()
public String getSecond()
}
public class WordPairList {
private ArrayList<WordPair> allPairs
public WordPairList(String[] words)
public int numMatches;
}
public class StudentAnswerSheet {
private ArrayList<String> answers;
public double getScore(ArrayList<String> key)
public String getName()
}
public class TestResults {
private ArrayList<StudentAnswerSheet> sheets;
public String highestScoringStudent(ArrayList<String> key)
}
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的对应元素进行比较
public double getScore(ArrayList<String> key)
{
double score = 0.0;
for (int i = 0; i < answers.size(); i++) {
if (answers.get(i).equals(key.get(i))) {
score += 1.0;
} else if (!answers.get(i).equals("?")) {
score -= 0.25;
}
}
return score;
}
b) Consider the following class that represents the test results of a group of students that took a multiple-choice test.
public String highestScoringStudent(ArrayList<String> key)
{
StudentAnswerSheet highest = sheets.get(0);
for (StudentAnswerSheet sheet : sheets) {
if (sheet.getScore(key) > highest.getScore(key)) {
highest = sheet;
}
}
return highest.getName();
}
Ex3.
1D Array/ArrayList elements to 2D Array
方法:遍历2D Array,循环里更新1D Array的index
FRQ-2
public SeatingChart(ArrayList<Student> studentList, int rows, int cols)
{
seats = new Student[rows][cols];
int sIndex = 0;
for(int c = 0; c < seats[0].length; c++)
{
for(int r = 0; r < seats.length; r++)
{
if(sIndex < studentList.size())
{
seats[r][c] = studentList.get(sIndex);
sIndex++;
}
}
}
}
public int removeAbsentStudents(int allowedAbsences)
{
int removed = 0;
for(int r = 0; r < seats.length; r++)
{
for(int c = 0; c < seats[0].length; c++)
{
if(seats[r][c] != null && seats[r][c].getAbsenceCount() > allowedAbsences)
{
seats[r][c] = null;
removed++;
}
}
}
return removed;
}
Ex4. FRQ4
public void addReview(ProductReview prodReview)
{
reviewList.add(prodReview);
String prodName = prodReview.getName();
boolean found = false;
for(int i=0; i<productList.size(); i++) {
if(productList.get(i).equals(prodName)) found = true;
}
if(!found) prodList.add(prodName);
}
public int getNumGoodReviews(String prodName) {
int result = 0;
for (ProductReview pr: reviewList) {
if(prodName.equals(pr.getName())) {
String review = pr.gerReview();
if(review.indexOf("best") >=0) result++;
}
}
return result;
}
2D Array
Ex1. Put all elements of a single row/column to 1D Array
public static int[] rowToArray(int[][] 2d, row){
int[] result = new int[2d[0].length];
result = 2d[row];
return result;
}
public static int[] colToArray(int[][] 2d, col){
int[] result = new int[2d.length];
for(int i=0; i<2d.length; i++) {
result[i] = 2d[i][col];
}
return result;
}
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.
public boolean toBeLabeled(int r, int c) {
// Check if the square is black.
if (blackSquares[r][c]) {
return false;
}
// Check if it's the first row or column.
if (r == 0 || c == 0) {
return true;
}
// Check if there's no white square immediately above or to the left.
if (blackSquares[r-1][c]) || blackSquares[r][c-1]) {
return true;
}
return false;
}
public boolean updateAllTemps(double tolerance) {
double[][] newTemp;
boolean isTolerence = true;
for (int row = 0; row < temp.length; row++) {
for (int col = 0; col < temp[row].length; col++) {
newTemp[row][col] = computeTemp(row, col); //elements unchanged
if (Math.abs(temp[row][col] - newTemp[row][col]) > tolerance) {
isTolerence = false;
}
}
}
temp = newTemp;
return isTolerence;
}
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.
public int[][] lenOfStrings(String[][] 2d) {
int[][] result = new int[2d.length][2d[0].length];
for(int i=0; i<2d.length; i++) {
for(int j=0; j<2d[0].length; j++) {
result[i][j] = 2d[i][j].length();
}
}
return result;
}