Algorithm
对称二叉树 给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
示例 1:
输入: 1 1 / \ /
2 3 2 3
[1,2,3], [1,2,3]
输出: true 示例 2:
输入: 1 1 /
2 2
[1,2], [1,null,2]
输出: false 示例 3:
输入: 1 1 / \ /
2 1 1 2
[1,2,1], [1,1,2]
输出: false 。
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null){
return true;
}
if(!(p != null && q != null)){
return false;
}
if(p.getVal() == q.getVal()){
TreeNode pLeft = p.getLeft();
TreeNode pRight = p.getRight();
TreeNode qLeft = q.getLeft();
TreeNode qRight = q.getRight();
if(isSameTree(pLeft,qLeft) && isSameTree(pRight,qRight)){
return true;
}
}
return false;
}
}
复制代码
Review
Steering the right course for AI: cloud.google.com/blog/produc…
Tip
多线程-ImmutableObject(不可变对象) 模式 多线程共享变量的情况下,为了保证数据一致性,往往需要对这些变量的访问进行加锁。而锁本身又会带来一些问题和开销。Immutable Object模式使得我们可以在不使用所的情况下,既保证共享变量访问的线程安全,又能避免一入锁可能带来的问题和开销。
/**
* 彩信中心信息
* 模式角色:ImmutableObject.ImmutableObject
*/
public class MMSCInfo {
/**
* 设备编号
*/
private final String deviceID;
/**
* 彩信中心URL
*/
private final String url;
/**
* 该彩信中心允许的最大附件大小
*/
private final int maxAttachmentSizeBytes;
public MMSCInfo(String deviceID , String url , int maxAttachmentSizeBytes){
this.deviceID = deviceID;
this.url = url;
this.maxAttachmentSizeBytes = maxAttachmentSizeBytes;
}
public MMSCInfo(MMSCInfo prototype){
this.deviceID = prototype.deviceID;
this.url = prototype.url;
this.maxAttachmentSizeBytes = prototype.maxAttachmentSizeBytes;
}
public String getDeviceID() {
return deviceID;
}
public String getUrl() {
return url;
}
public int getMaxAttachmentSizeBytes() {
return maxAttachmentSizeBytes;
}
}
复制代码
/**
* 彩信中心路由规则管理器
* 模式角色:ImmutableObject.ImmutableObject
*/
public final class MMSCRouter {
private static volatile MMSCRouter instance = new MMSCRouter();
private final Map<String,MMSCInfo> routeMap;
public MMSCRouter(){
this.routeMap = MMSCRouter.retrieveRouteMapFromDB();
}
private static Map<String,MMSCInfo> retrieveRouteMapFromDB(){
Map<String,MMSCInfo> map = new HashMap<String,MMSCInfo>();
// 省略代码
return map;
}
public static MMSCRouter getInstance(){
return instance;
}
/**
* 根据手机号码前缀获取对应的彩信中心信息
* @param msisdnPrefix 手机号码前缀
* @return 彩信中心信息
*/
public MMSCInfo getMMSC(String msisdnPrefix) {
return routeMap.get(msisdnPrefix);
}
/**
* 将当前MMSCRouter的实例更新为置顶的新实例
* @param newInstance
*/
public static void setInstance(MMSCRouter newInstance){
instance = newInstance;
}
private static Map<String,MMSCInfo> deepCopy(Map<String,MMSCInfo> m){
Map<String,MMSCInfo> result = new HashMap<>();
for(String key : m.keySet()){
result.put(key , new MMSCInfo(m.get(key)));
}
return result;
}
public Map<String,MMSCInfo> getRouteMap(){
// 做防御性复制
return Collections.unmodifiableMap(deepCopy(routeMap));
}
}
复制代码
/**
* 与运维中心(Operation and Maintenance Center) 对接的类
* 模式角色:Immutable
*/
public class OMCAgent extends Thread{
@Override
public void run() {
boolean isTableModificationMsg = false;
String updateTableName = null;
while(true){
// 省略其他代码
/**
*
*/
if (isTableModificationMsg){
if ("MMSCInfo".equals(updateTableName)){
MMSCRouter.setInstance(new MMSCRouter());
}
}
// 省略其他代码
}
}
}
复制代码
Share
如何设计一个百万级用户的抽奖系统? : link.
转载于:https://juejin.im/post/5ceb3c50f265da1b8466c24b