Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
In Pascal’s triangle, each number is the sum of the two numbers directly above it.
Example:
1 | Input: 5 |
Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
In Pascal’s triangle, each number is the sum of the two numbers directly above it.
Example:
1 | Input: 5 |
Given a non-empty array of digits representing a non-negative integer, increment one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [4,3,2,1] |
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Example 1:
1 | Input: [1,3,5,6], 5 |
Example 2:
1 | Input: [1,3,5,6], 2 |
Example 3:
1 | Input: [1,3,5,6], 7 |
Example 4:
1 | Input: [1,3,5,6], 0 |
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example 1:
1 | Given nums = [3,2,2,3], val = 3, |
Example 2:
1 | Given nums = [0,1,2,2,3,0,4,2], val = 2, |
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
1 | Given nums = [1,1,2], |
Example 2:
1 | Given nums = [0,0,1,1,1,2,2,3,3,4], |
注意,要判断nums的长度,如果是0则会报错
如下为类型CMyString的声明, 请为该类型添加赋值运算符函数.
1 | class CMyString |
找出数组中重复的数字。
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
示例 1:
输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3
限制:
2 <= n <= 100000
设计一个类, 我们只能生成该类的一个实例
1 | public class Hungry { |
能防住多线程, 不能防住反射.
1 | class Lazy{ |
不能防住多线程, 不能防住反射.
1 | //加锁之后的懒汉式单例模式, DCL |
1 | class SafeLazy{ |
多线程足够安全, 仍然防不住反射
1 | public class Holder { |
就是把饿汉式的new 单例模式的过程private final static Hungry HUNGRY = new Hungry();
放到个静态内部类中, 通过静态内部类的加载机制实现懒惰加载. 仍然防不住反射
1 | enum Singleton |
这个最安全, 多线程和反射都破坏不了, 反序列化的方法也破解不了. 也最简洁, 核心代码只需要3行. 加载模式和饿汉式一样, 都是提前加载. 怪不得都推荐使用这种方法
这个题等价于leetcode 287.
Given an array of integers nums
containing n + 1
integers where each integer is in the range [1, n]
inclusive.
There is only one duplicate number in nums
, return this duplicate number.
Follow-ups:
nums
? nums
?O(1)
extra space?O(n2)
?Example 1:
1 | Input: nums = [1,3,4,2,2] |
Example 2:
1 | Input: nums = [3,1,3,4,2] |
Example 3:
1 | Input: nums = [1,1] |
Example 4:
1 | Input: nums = [1,1,2] |
Constraints:
2 <= n <= 3 * 104
nums.length == n + 1
1 <= nums[i] <= n
nums
appear only once except for precisely one integer which appears two or more times.输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例如,给出
前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:
1 | 3 |
限制:
0 <= 节点个数 <= 5000