@skysbjdy
2016-12-24T06:38:05.000000Z
字数 5631
阅读 13128
Interview
ArraySum:
Bug: sum = arr[i]
Fixed: sum += arr[i]
Count Occurence: 统计某数在数组中出现的次数
Bug: while loop 里面 i++
Check Grade: 计算GPA
Bug: "||"
Fixed: "&&"
Remove Element: 删除数组特定位置的元素, 超出返回则返回原数组
Bug: arr[i++] 第五行
Fixed: arr[i + 1]
public static int[] removeElement(int arr[], int index) {
int i, j, len = arr.length;
if (index < len) {
for (i = index; i < len - 1; i++) {
arr[i] = arr[i+1];
}
int rarr[] = new int[len - 1];
for (i = 0; i < len - 1; i++) {
rarr[i] = arr[i];
}
} else
return arr;
}
Digit Count: 返回num%len(num) == 0 or != 0
Bug: 最后返回时, num已经是0了. 所以之前要先保存起来.
public int find(int num) {
int count = 0;
int temp = num; //add a temporary variable
while (num ! = 0) {
num = num / 10;
count++;
}
//return num % count; <--Bug
return temp % count; //Fixed
}
Sort Array
Bug: 大于小于符号写反
Fixed: arr[i] < arr[j]降序, arr[i] > arr[j]升序.
public static int[] sortArray(int[] arr) {
int len = arr.length;
int small, pos, i, j, temp; //small, pos can be deleted
for (i = 0; i <= len - 1; i++) {
for (j = i; j < len; j++) {
temp = 0;
if (arr[i] < arr[j]) { // Bug '<' should be '>'
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
Sort Array 2
Bug: 大于小于符号写反
Fixed: arr[i] < arr[j]降序, arr[i] > arr[j]升序.
//这是一个正确的降序排列算法
public static int[] sortArray2(int arr[]) {
int i, max, location, j, temp, len = arr.length;
for (i = 0; i < len; i++) {
max = arr[i];
location = i;
for (j = i; j < len; j++) {
if (max < arr[j]) {
max = arr[j];
location = j;
}
}
temp = arr[i];
arr[i] = arr[location];
arr[location] = temp;
}
return arr;
}
Replace values 数组长度是偶数就把元素都改成1, 奇数就都改成0
Bug: for loop里面 i <= len, j < = len
Fixed: i < len, j < len
public static int[] replaceValues(int arr[]) {
int i, j, len = arr.length;
if (len % 2 == 0) {
for (i = 0; i < len; i++) { //fixed
arr[i] = 0;
}
} else {
for (j = 0; j < len; j++) { //fixed
arr[j] = 1;
}
}
return arr;
}
Reverse array
但是class的名字居然是sort array
Bug: arr[len-1]改成 arr[len-i-1] for循环里 len +=1去掉
public static int[] reverseArray(int[] arr) {
int i, temp, originalLen = arr.length;
int len = originalLen;
for (i = 0; i < originalLen / 2; i++) {
temp = arr[len - i - 1];
arr[len - i - 1] = arr[i]; //fixed
arr[i] = temp;
// len +=1;
}
return arr;
}
Even Odd pattern
Bug: for 循环缺大括号
public static void print4(int num) {
int i, print = 0;
if (num % 2 == 0) {
print = 0;
for (i = 0; i < num; i++) {
System.out.print(print + " ");
print += 2;
}
} else {
print = 1;
for (i = 0; i < num; i++) {
System.out.print(print + " ");
print += 2;
}
}
}
Manchester Code: 假设第一个element前面的数字是0
Bug: 把判断语句的 == 改成 != (考虑i == 0的情况)
Selection sort
Bug: if里面的判断 arr[min] > arr[x], 改成arr[min] > arr[y]
// selection sort in ascending order
for (x = 0; x < n; x++) {
int index_of_min = x;
for (y = x; y < n; y++) {
//if (arr[index_of_min] > arr[x]) { // Bug
if (arr[index_of_min] > arr[y])
y = index_of_min;
}
int temp = arr[x];
arr[x] = arr[index_of_min];
arr[index_of_min] = temp;
}
}
// bubble sort in descending order
for (int x = 0; x < n; x++) {
for (int y = x; y < n; y++) {
if (arr[x] < arr[y]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
Insertion sort
Bug: 大于小于x
for (int i = 1; i < n; i++) {
if (arr[i - 1] > arr[i]) { int temp = arr[i]; //Bug
int j = i;
while (j > 0 && arr[j - 1] > temp) { //Bug arr[j - 1] > temp
arr[j] = arr[j - 1];
j--;
}
arr[j] = temp;
}
}
Print Pattern
a
ab
abc
Bug: char ch = 'a' 定义在了for loop前面, 移进来就好, 同时删除了没用的变量
//This code is correct
public static void print2(int row) {
for (int i = 1; i <= row; i++) {
char ch = 'a';
char print = ch;
for (int j = 1; j <= i; j++) {
System.out.print((print++));
}
System.out.println();
}
}
//可以继续优化:
public static void print2(int row) {
for (int i = 1; i <= row; i++) {
char ch = 'a';
for (int j = 1; j <= i; j++) {
System.out.print((ch++));
}
System.out.println();
}
}
Print Pattern
Row number = 3
11
1111
111111
Row number = 2
11
1111
Bug: 第一层for循环少了大括号,
public static void print3(int row) {
int x = 1;
for (int i = 1; i <= row; i++) { //添加括号
for (int j = i; j > 0; j--) {
System.out.print(x + "" + x);
}
System.out.println();
} //添加括号
}
remove duplicates from unsorted array
Bug: 循环下标要从i + 1开始