当前位置:网站首页>剑指offer基础版 ----- 第28天
剑指offer基础版 ----- 第28天
2022-07-31 05:09:00 【米兰的小红黑】

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) {
val = x; }
* }
*/
public class Codec {
public String serialize(TreeNode root) {
return rserialize(root, "");
}
public TreeNode deserialize(String data) {
String[] dataArray = data.split(",");
List<String> dataList = new LinkedList<String>(Arrays.asList(dataArray));
return rdeserialize(dataList);
}
public String rserialize(TreeNode root, String str) {
if (root == null) {
str += "None,";
} else {
str += str.valueOf(root.val) + ",";
str = rserialize(root.left, str);
str = rserialize(root.right, str);
}
return str;
}
public TreeNode rdeserialize(List<String> dataList) {
if (dataList.get(0).equals("None")) {
dataList.remove(0);
return null;
}
TreeNode root = new TreeNode(Integer.valueOf(dataList.get(0)));
dataList.remove(0);
root.left = rdeserialize(dataList);
root.right = rdeserialize(dataList);
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));

class Solution {
List<String> rec;
boolean[] vis;
public String[] permutation(String s) {
int n = s.length();
rec = new ArrayList<String>();
vis = new boolean[n];
char[] arr = s.toCharArray();
Arrays.sort(arr);
StringBuffer perm = new StringBuffer();
backtrack(arr, 0, n, perm);
int size = rec.size();
String[] recArr = new String[size];
for (int i = 0; i < size; i++) {
recArr[i] = rec.get(i);
}
return recArr;
}
public void backtrack(char[] arr, int i, int n, StringBuffer perm) {
if (i == n) {
rec.add(perm.toString());
return;
}
for (int j = 0; j < n; j++) {
if (vis[j] || (j > 0 && !vis[j - 1] && arr[j - 1] == arr[j])) {
continue;
}
vis[j] = true;
perm.append(arr[j]);
backtrack(arr, i + 1, n, perm);
perm.deleteCharAt(perm.length() - 1);
vis[j] = false;
}
}
}
边栏推荐
猜你喜欢

Mysql application cannot find my.ini file after installation

MySQL optimization slow log query

Kubernetes 证书可用年限修改

Interviewer, don't ask me to shake hands three times and wave four times again

110 MySQL interview questions and answers (continuously updated)

面试官,不要再问我三次握手和四次挥手

Centos7 install mysql5.7

CentOS7 —— yum安装mysql

A complete introduction to JSqlParse of Sql parsing and conversion

MYSQL下载及安装完整教程
随机推荐
SQL row-column conversion
MYSQL一站式学习,看完即学完
【ORACLE Explain 详解】
面试官,不要再问我三次握手和四次挥手
CentOS7 install MySQL graphic detailed tutorial
Paginate the list collection and display the data on the page
sql statement - how to query data in another table based on the data in one table
mysql 的简单运用命令
pytorch中的一维、二维、三维卷积操作
Flink sink ES 写入 ES(带密码)
Mysql应用安装后找不到my.ini文件
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
MySQL optimization: from ten seconds to three hundred milliseconds
关于LocalDateTime的全局返回时间带“T“的时间格式处理
Reference code series_1. Hello World in various languages
【一起学Rust】Rust学习前准备——注释和格式化输出
Lock wait timeout exceeded解决方案
MySQL optimization slow log query
Go language study notes - dealing with timeout problems - Context usage | Go language from scratch
为什么要用Flink,怎么入门使用Flink?