程序员面试金典(第 6 版)
- 面试题 01.01. 判定字符是否唯一
- 面试题 01.02. 判定是否互为字符重排
- 面试题 01.03. URL化
- 面试题 01.04. 回文排列
- 面试题 01.05. 一次编辑🔐
- 面试题 01.06. 字符串压缩🔐
- 面试题 01.07. 旋转矩阵
- 面试题 01.08. 零矩阵
- 面试题 01.09. 字符串轮转![在这里插入图片描述](https://file.cfanz.cn/uploads/png/2022/01/20/3/dA422Ha8O0.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA44CMIDI1JyBoIOOAjQ==,size_20,color_FFFFFF,t_70,g_se,x_16)
- 面试题 02.01. 移除重复节点
面试题 01.01. 判定字符是否唯一
class Solution {
public boolean isUnique(String astr) {
if (astr == null || astr.length() <= 1) return true;
boolean[] map = new boolean[26];
for (char ch : astr.toCharArray()) {
if (map[ch - 'a']) return false;
map[ch - 'a'] = true;
}
return true;
}
}
面试题 01.02. 判定是否互为字符重排
class Solution {
public boolean CheckPermutation(String s1, String s2) {
if (s1 == null && s2 == null) return true;
if (s1 == null || s2 == null || s1.length() != s2.length()) return false;
int[] map = new int[26];
for (char ch : s1.toCharArray()) {
++map[ch - 'a'];
}
for (char ch : s2.toCharArray()) {
if (map[ch - 'a'] == 0) return false;
--map[ch - 'a'];
}
return true;
}
}
面试题 01.03. URL化
class Solution {
public String replaceSpaces(String S, int length) {
if (S == null || S.length() == 0) return S;
int num = 0;
for (int i = 0; i < length; i++) {
if (S.charAt(i) == ' ') {
num++;
}
}
char[] arr = new char[length + num * 2];
int last = arr.length - 1, pre = length - 1;
while (last >= 0) {
if (S.charAt(pre) != ' ') arr[last--] = S.charAt(pre--);
else {
arr[last--] = '0';
arr[last--] = '2';
arr[last--] = '%';
--pre;
}
}
return new String(arr);
}
}
面试题 01.04. 回文排列
class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null || s.length() <= 1) return true;
boolean[] map = new boolean[256];
for (char ch : s.toCharArray()) {
map[ch] ^= true;
}
boolean flag = false;
for (boolean b : map) {
if (b) {
if (flag) return false;
flag = true;
}
}
return true;
}
}
面试题 01.05. 一次编辑🔐
- oneEditAway(“teacher”,“lbeacher”)
class Solution {
public boolean oneEditAway(String first, String second) {
if (first.length() == 0 || second.length() == 0) return true;
int subLen = first.length() - second.length();
if (Math.abs(subLen) > 1) return false;
int i = first.length() - 1, j = second.length() - 1;
boolean flag = false;
while (i >= 0 && j >= 0) {
if (first.charAt(i) != second.charAt(j)) {
if (subLen == -1) i++;// 阻止first-1
else if (subLen == 1) j++;// 阻止second-1
if (flag) return false;
flag = true;
}
i--;
j--;
}
return true;
}
}
面试题 01.06. 字符串压缩🔐
class Solution {
public String compressString(String S) {
if (S == null || S.length() <= 1) return S;
StringBuffer res = new StringBuffer();
char[] arr = S.toCharArray();
int pre = 0, N = arr.length;
for (int i = 1; i < N; i++) {
if (arr[i] != arr[i - 1]) {
res.append(arr[i - 1]);
res.append(i - pre);
pre = i;
}
}
res.append(arr[N - 1]);
res.append(N - pre);
return res.length() >= N ? S : res.toString();
}
}
面试题 01.07. 旋转矩阵
class Solution {
public void rotate(int[][] matrix) {
if (matrix.length <= 1) return;
int firstRow = 0, firstLine = 0, secondRow = matrix.length - 1, secondLine = matrix[0].length - 1;
while (firstLine < secondLine && firstRow < secondRow) {
circleSway(matrix, firstRow++, firstLine++, secondRow--, secondLine--);
}
for (int[] a : matrix) System.out.println(Arrays.toString(a));
}
private void circleSway(int[][] matrix, int locR1, int locL1, int locR2, int locL2) {
for (int i = 0; i < locL2 - locL1; i++) {
int temp = matrix[locR1][locL1 + i];
matrix[locR1][locL1 + i] = matrix[locR2 - i][locL1];
matrix[locR2 - i][locL1] = matrix[locR2][locL2 - i];
matrix[locR2][locL2 - i] = matrix[locR1 + i][locL2];
matrix[locR1 + i][locL2] = temp;
}
}
}
面试题 01.08. 零矩阵
class Solution {
public void setZeroes(int[][] matrix) {
if (matrix == null) return;
int M = matrix.length, N = matrix[0].length;
boolean[] rows = new boolean[M], lines = new boolean[N];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (matrix[i][j] == 0) {
rows[i] = true;
lines[j] = true;
}
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (rows[i] || lines[j]) {
matrix[i][j] = 0;
}
}
}
}
}
面试题 01.09. 字符串轮转
class Solution {
public boolean isFlipedString(String s1, String s2) {
if (s1 == null && s2 == null) return true;
if (s1 == null || s2 == null) return false;
if (s1.length() != s2.length()) return false;
String s = s1 + s1;
return s.contains(s2);
}
}
面试题 02.01. 移除重复节点
class Solution {
public ListNode removeDuplicateNodes(ListNode head) {
if (head == null || head.next == null) return head;
BitSet set = new BitSet();
ListNode tail = head;
set.set(head.val, true);
while (tail.next != null) {
if (!set.get(tail.next.val)) {
set.set(tail.next.val, true);
tail = tail.next;
} else {
tail.next = tail.next.next;
}
}
return head;
}
}