剑指offer--java实现全纪录(更新中)
面试题4 替换空格
public static void replace(String[] str, int len) {
if (str == null || len == 0) return;
int realLen = 0;
int blank = 0;
for (int i = 0; i < len; i ++) {
if (str[i] == "\0") {
break;
}
realLen ++;
if (str[i] == " ") {
blank++;
}
}
int newLen = realLen + blank * 2;
if(newLen > len) return;
int p1 = realLen;
int p2 = newLen;
while(p1 >= 0 && p1 < p2) {
if(str[p1] == " ") {
str[p2--] = "0";
str[p2--] = "2";
str[p2--] = "%";
}
else{
str[p2--] = str[p1];
}
p1--;
}
}
面试题10 数字中1的个数
public static int numberOf1(int n) {
int count = 0;
while(n>0) {
count ++;
n = (n-1)&n;
}
return count;
}
面试题11 数值的整数次幂
public double myPow(double x, int n) {
if(n<0) return 1/x * myPow(1/x,-(n+1));
if(n==0) return 1;
if(n==2) return x * x;
if(n%2==0) return myPow( myPow(x, n/2), 2);
else return x * myPow(myPow(x, n/2), 2);
}
面试题13:在O(1)时间内删除链表结点
public static void delete(ListNode head, ListNode node) { if(head == null || node == null) { return; } else if(node.next != null){ ListNode next = node.next; node.val = next.val; node.next = next.next; } else if(head == node) { head = null; return; } else { ListNode p = head; while(p.next!=node) { p = p.next; } p.next = null; node = null; } }
面试题16:反转链表
链表的中的所有结点需要指向它的前一个结点,但由于是单链表,所以需要一个指针存储当前结点的前一个结点,为了避免链表断裂,需要一个节点存储当前结点的下一个结点;
链表实现类:
public class ListNode { private int value; private ListNode next; public ListNode(int value) { this.value = value; } public ListNode(int value ,ListNode next) { this.value = value; this.next = next; } public ListNode(){ } }
反转链表:
public ListNode reverseList(ListNode head) { //反转后的表头
ListNode reversedHead = null;
//存储当前结点 ListNode cur = head;
//存储前驱结点 ListNode pre = null; while(cur != null) {
//存储下一个结点 ListNode next = cur.next; if(next == null) reversedHead = cur;
//当前结点指向前一个结点(反转) cur.next = pre;
//pre指针指向当前结点 pre = cur;
//cur指针指向下一个结点 cur = next; } return reversedHead; }
面试题20 顺时针打印矩阵
public void printMatrixInCircle(int[][] array){
if(array == null)
return;
int start = 0;
while(array[0].length > start*2 && array.length >start*2){
printOneCircle(array,start);
start++;
}
}
private void printOneCircle(int[][] array,int start){
int columns = array[0].length;
int rows = array.length;
int endX = columns - 1 - start;
int endY = rows - 1 - start;
//从左到右打印一行
for(int i = start;i <= endX ;i++){
int number = array[start][i];
System.out.print(number+",");
}
//从上到下打印一列
if(start <endY){
for(int i = start +1;i<=endY;i++){
int number = array[i][endX];
System.out.print(number+",");
}
}
//从右到左打印一行
if(start < endX && start < endY){
for(int i = endX -1;i>=start;i--){
int number = array[endY][i];
System.out.print(number+",");
}
}
//从下到上打印一列
if(start <endY && start <endY -1){
for(int i =endY -1;i>=start+1;i--){
int number = array[i][start];
System.out.print(number+",");
}
}
}
面试题 28 字符串全排列
public static void permutation(String s) { if(s == null) return ; char[] sArr = s.toCharArray(); permutation(sArr,0,s.length() - 1); } public static void permutation(char[] s,int from,int to) { if(to <= 1) return; if(from == to) { System.out.println(s); } else { for(int i=from; i<=to; i++) { swap(s,i,from); //交换前缀,使其产生下一个前缀 permutation(s, from+1, to); swap(s,from,i); //将前缀换回,继续做上一个前缀的排列 } } } public static void swap(char[] s,int i,int j) { char tmp = s[i]; s[i] = s[j]; s[j] = tmp; }
面试题31 连续数组的最大和
比较经典的面试题,被问到不只一次
public static int maxSum (int[] nums) {
if(nums == null) return Integer.MIN_VALUE;
int dp[] = new int[nums.length];
dp[0] = nums[0];
int max = 0;
for(int i = 1;i < nums.length;i++) {
dp[i] = Math.max(nums[i],nums[i]+dp[i-1]);
max = dp[i]>max?dp[i]:max;
}
return max;
}
扩展:最大子矩阵和
public int maxSumMatrix(int[][] num) {
if(num == null) return 0;
int[][] total = num;
for(int i = 1;i< num.length;i++) {
for(int j = 0; j < num.length;j++) {
total[i][j] += total[i-1][j];
}
}
int max = Integer.MIN_VALUE;
for(int i = 0; i < num.length;i++) {
for(int j = i; j < num.length; j++) {
int[] dp = new int[num[0].length];
for(int k = 0; k < num[0].length;k++){
if(i == 0){
dp[k] = num[j][k];
}
else
dp[k] = num[j][k] - num[i-1][k];
}
int localMax = maxSum(dp);
max = localMax > max?localMax:max;
}
}
return max;
}

浙公网安备 33010602011771号