105. 从前序与中序遍历序列构造二叉树
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length==0) return null;
TreeNode root = new TreeNode(preorder[0]);
int idx = 0;
while(preorder[0]!=inorder[idx]) idx++;
root.left = buildTree(Arrays.copyOfRange(preorder,1,idx+1),Arrays.copyOfRange(inorder,0,idx));
root.right = buildTree(Arrays.copyOfRange(preorder,idx+1,preorder.length),Arrays.copyOfRange(inorder,idx+1,inorder.length));
return root;
}
}
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || preorder.length == 0) {
return null;
}
TreeNode root = new TreeNode(preorder[0]);
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
int inorderIndex = 0;
for (int i = 1; i < preorder.length; i++) {
int preorderVal = preorder[i];
TreeNode node = stack.peek();
if (node.val != inorder[inorderIndex]) {
node.left = new TreeNode(preorderVal);
stack.push(node.left);
} else {
while (!stack.isEmpty() && stack.peek().val == inorder[inorderIndex]) {
node = stack.pop();
inorderIndex++;
}
node.right = new TreeNode(preorderVal);
stack.push(node.right);
}
}
return root;
}
}
面试题 02.01. 移除重复节点
class Solution {
public ListNode removeDuplicateNodes(ListNode head) {
if(head==null) return head;
Set<Integer> set = new HashSet<>();
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode prev = dummyHead;
while(prev.next!=null){
//如果set集合中不存在当前遍历的值,则添加到set中,并且把这个节点加入链表中
if(!set.contains(prev.next.val)){
set.add(prev.next.val);
prev = prev.next;
}else{
//如果set集合中存在当前遍历的值,则说明之前链表中已有此元素,链表跳过这个节点
prev.next = prev.next.next;
}
}
return dummyHead.next;
}
}
class Solution {
public ListNode removeDuplicateNodes(ListNode head) {
if (head == null) {
return null;
}
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode pre = dummyHead;
while (pre.next != null) {
ListNode cur = pre.next;
while (cur.next != null) {
if (pre.next.val == cur.next.val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
pre = pre.next;
}
return dummyHead.next;
}
}
面试题 10.02. 变位词组
//错误代码,告诫自己
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> list = new ArrayList<>();
for(String str : strs){
List<String> res = new ArrayList<>();
int count = getSum(str);
for(int i = 0;i<strs.length;i++){
if(count==getSum(strs[i])&&strs[i]!="."){
res.add(strs[i]);
strs[i] = ".";
}
}
if(res.size()>0){
list.add(res);
}
}
return list;
}
public int getSum(String s){
int count = 0;
for(int i = 0;i<s.length();i++){
count += s.charAt(i)-'a';
}
return count;
}
}
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
if (strs == null || strs.length == 0) {
return null;
}
Map<String, List<String>> map = new HashMap<>();
for (String str : strs) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
String s = new String(chars);
List<String> list = map.getOrDefault(s, new ArrayList<>());
list.add(str);
map.put(s, list);
}
return new ArrayList<>(map.values());
}
}
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
int len = strs.length;
HashMap<String, List<String>> map = new HashMap<>(len);
for (String str : strs) {
int[] count = new int[26];
int str_len = str.length();
for (int i = 0; i < str_len; ++i)
++count[str.charAt(i) - 'a'];
StringBuilder sb = new StringBuilder(100);
for (int num : count)
sb.append(num + '.');
map.computeIfAbsent(sb.toString(), unused -> new LinkedList<>()).add(str);
}
return new ArrayList<>(map.values());
}
}
你知道的越多,你不知道的越多。