设计类
使用Redis实现一个用户一分钟抽奖3次写一个你觉得最好的单例模式,你觉得这么写还有什么问题?
单例模式的几种写法
饿汉式懒汉式、双重检查锁静态内部类
public class Singleton {
private Singleton(){}
private static class SingletonHolder {
private static final Singleton INSTANCE
= new Singleton();
}
public static Singleton
getInstance() {
return SingletonHolder
.INSTANCE
;
}
}
枚举类
public enum SingletonEnum
{
INSTANCE(1, "aa");
SingletonEnum(int value
, String v
) {
}
}
- 单例模式怎么破坏
策略模式demo 我的github上的demo工厂模式demo 设计模式demo写一个观察者模式demo demo模板方法demo
算法题
排序
冒泡排序归并排序选择排序插入排序快速排序
public int[] sortArray(int[] nums
) {
if (nums
== null
) {
return nums
;
}
quickSort(nums
, 0, nums
.length
- 1);
return nums
;
}
public void quickSort(int[] numbers
, int left
, int right
) {
if (right
<= left
) {
return;
}
int pivot
= numbers
[left
];
int start
= left
;
int end
= right
;
while (start
< end
) {
while (start
< end
&& pivot
<= numbers
[end
]) {
end
--;
}
swapArr(numbers
, start
, end
);
while (start
< end
&& numbers
[start
] <= pivot
) {
start
++;
}
swapArr(numbers
, start
, end
);
}
quickSort(numbers
, left
, start
- 1);
quickSort(numbers
, start
+ 1, right
);
}
private void swapArr(int[] numbers
, int left
, int right
) {
int temp
= numbers
[left
];
numbers
[left
] = numbers
[right
];
numbers
[right
] = temp
;
}
树的前中后序遍历
public static void preOrder(TreeNode root
, List
<TreeNode> linkedList
) {
if (root
!= null
) {
linkedList
.add(root
);
preOrder(root
.left
, linkedList
);
preOrder(root
.right
, linkedList
);
}
}
public static void inOrder(TreeNode root
, List
<TreeNode> linkedList
) {
if (root
!= null
) {
inOrder(root
.left
, linkedList
);
linkedList
.add(root
);
inOrder(root
.right
, linkedList
);
}
}
public static void postOrder(TreeNode root
, List
<TreeNode> linkedList
) {
if (root
!= null
) {
postOrder(root
.left
, linkedList
);
postOrder(root
.right
, linkedList
);
linkedList
.add(root
);
}
}
给前序中序遍历的结果还原二叉树斐波拉契数列二分查找
public int search(int[] nums
, int target
) {
if (nums
== null
) {
return -1;
}
int low
= 0;
int high
= nums
.length
- 1;
while (low
<= high
) {
int middle
= (low
+ high
)/2;
if (nums
[middle
] == target
) {
return middle
;
} else if (nums
[middle
] < target
) {
low
= middle
+ 1;
} else {
high
= middle
- 1;
}
}
return -1;
}
单链表翻转
public ListNode
reverseList(ListNode head
) {
ListNode pre
= null
;
ListNode curr
= head
;
while (curr
!= null
) {
ListNode next
= curr
.next
;
curr
.next
= pre
;
pre
= curr
;
curr
= next
;
}
return pre
;
}
数组中数字出现的次数两数之和 2sum
public int[] twoSum(int[] nums
, int target
) {
Map
<Integer, Integer> map
= new HashMap<>();
for (int i
= 0; i
< nums
.length
; i
++) {
int temp
= target
- nums
[i
];
if (map
.containsKey(temp
)) {
return new int[]{map
.get(temp
), i
};
}
map
.put(nums
[i
], i
);
}
return null
;
}
判断链表是否有环
public ListNode
detectCycle(ListNode head
) {
Set
<ListNode> nodeSet
= new HashSet<ListNode>();
ListNode item
= head
;
int index
= 0;
while (item
!= null
) {
if (nodeSet
.contains(item
)) {
return item
;
}
nodeSet
.add(item
);
item
= item
.next
;
}
return null
;
}
合并两个有序数组
public void merge(int[] nums1
, int m
, int[] nums2
, int n
) {
int[] temp
= new int[nums1
.length
];
int mp
= 0, np
= 0;
for (int i
= 0; i
< temp
.length
; i
++) {
if (mp
< m
&& np
< n
) {
if (nums1
[mp
] < nums2
[np
]) {
temp
[i
] = nums1
[mp
++];
} else if (nums2
[np
] < nums1
[mp
]) {
temp
[i
] = nums2
[np
++];
} else {
temp
[i
++] = nums1
[mp
++];
temp
[i
] = nums2
[np
++];
}
continue;
}
if (np
< n
) {
temp
[i
] = nums2
[np
++];
continue;
}
if (mp
< m
) {
temp
[i
] = nums1
[mp
++];
}
}
if (m
>= 0) System
.arraycopy(temp
, 0, nums1
, 0 ,temp
.length
);
}
合并两个数组 可以先排序,后合并回文数 先翻转,再判断翻转整数
public int reverse(int x
) {
long result
= 0;
while (x
!= 0) {
result
= result
*10 + x
%10;
x
= x
/10;
}
return result
> 0x7fffffff || result
< 0x80000000 ? 0 : (int)result
;
}
删除链表倒数第n个元素
public ListNode
removeNthFromEnd(ListNode head
, int n
) {
int index
= - n
;
ListNode item
= head
;
while (item
!= null
) {
item
= item
.next
;
index
++;
}
ListNode pre
= null
;
ListNode curr
= head
;
while (curr
!= null
) {
if (index
-- == 0) {
if (pre
== null
) {
return curr
.next
;
}
pre
.next
= curr
.next
;
break;
}
pre
= curr
;
curr
= curr
.next
;
}
return head
;
}
正方形,从左上角到右下角总共条路两个有序数组,第n大的数一个长度n的数组,前k(k<n)个升序,后面的降序求二叉树的最大深度