Leetcode上的剑指offer二刷心得(Part 3)
一刷目录
16. 数值的整数次方
一刷中的是递归,这里记录一个非递归的方法
class Solution { public double myPow(double x, int n) { double ans = 1; double temp = x; int exp = n; while (exp != 0) { if ((exp % 2) != 0) { ans = ans * temp; } temp = temp * temp; exp /= 2; } return n > 0 ? ans : 1 / ans; } }
17. 打印从1到最大的n位数
牛客上没有此题。看题解,有用到大数解法的,不太懂,这里先搁置这个疑问。
class Solution { public int[] printNumbers(int n) { int res = 0; while(n != 0){ res = res * 10 + 9; n--; } int[] result = new int[res]; for(int i = 0;i < res;i++){ result[i] = i + 1; } return result; } }
18.1 删除链表的节点
leetcode上的题意与书中不符合,书中是O(1)操作要求。