注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
目前airbnb的面试正在做一些改革,过了电面之后安排onsite, onsite是分两天,第一天面design, coding, experience, pass了再去面cross functional。安排了第二轮onsite还没去,等onsite都面完了再来汇报。先来个电面经验啥的花了不少米QAQ:
HashMap 的
- /*
- * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
- */
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- /**
- * Follow up: 用trie? 什么是trie?
- * [url]http://www.noteanddata.com/leetcode-336-Palindrome-Pairs-airbnb-interview-problem-java-solution-note.html[/url]
- */
- public class PalindromPairs {
- public List<List<Integer>> palindromePairs(String[] words){
- List<List<Integer>> res = new ArrayList<>();
- if(words == null || words.length == 0) return res;
- HashMap<String, Integer> posMap = new HashMap<>();
- for(int i=0;i<words.length; i++) {
- posMap.put(words[i],i);
- }
- for(int i=0;i<words.length;i++) {
- for(int j=0;j<=words[i].length();j++){ //j<=0 so that it handles empty string
- String sub1 = words[i].substring(0, j);
- String sub2 = words[i].substring(j);
- if(isPalin(sub1)) {
- String revSub2 = new StringBuilder(sub2).reverse().toString();
- if(posMap.containsKey(revSub2) && posMap.get(revSub2) !=i) {
- res.add(Arrays.asList(posMap.get(revSub2), i));
- }
- }
- if(isPalin(sub2) ) { //to avoid duplicate
- String revSub1 = new StringBuilder(sub1).reverse().toString();
- if(posMap.containsKey(revSub1) && posMap.get(revSub1) != i) {
- res.add(Arrays.asList(i, posMap.get(revSub1)));
- }
- }
- }
- }
- return res;
- }
- private boolean isPalin(String str){
- int left = 0;
- int right = str.length()-1;
- while(left<right) {
- if(str.charAt(left++)!=str.charAt(right--)) return false;
- }
- return true;
- }
- }
复制代码
Trie的
- /*
- * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
- */
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class PalindromPairsTrie {
- List<List<Integer>> res = new ArrayList<>();
- TrieNode root = new TrieNode();
- private static class TrieNode {
- TrieNode[] children;
- int index;
- List<Integer> list;
- TrieNode() {
- children = new TrieNode[26];
- index = -1;
- list = new ArrayList<>();
- }
- }
- public List<List<Integer>> palindromePairs(String[] words) {
- for (int i = 0; i < words.length; i++) {
- insert(words[i], i);
- }
- for (int i = 0; i < words.length; i++) {
- search(words[i], i);
- }
- return res;
- }
- private void insert(String word, int index) {
- TrieNode trie = root;
- for (int i = word.length() - 1; i >= 0; i--) {
- int j = word.charAt(i) - 'a';
- if (trie.children[j] == null) {
- trie.children[j] = new TrieNode();
- }
- if (isPalindrome(word, 0, i)) {
- trie.list.add(index);
- }
- trie = trie.children[j];
- }
- trie.list.add(index);
- trie.index = index;
- }
- private void search(String word, int i) {
- TrieNode trie = root;
- for (int j = 0; j < word.length(); j++) { //handles when other part is shorter than words[i]
- if (trie.index >= 0 && trie.index != i && isPalindrome(word, j, word.length() - 1)) {
- res.add(Arrays.asList(i, trie.index));
- }
- trie = trie.children[word.charAt(j) - 'a'];
- if (trie == null) return; // important!!!
- }
- for (int j : trie.list) { //handles when other part is longer than words[i]
- if (i == j) continue;
- res.add(Arrays.asList(i, j));
- }
- }
- private boolean isPalindrome(String word, int i, int j) {
- while (i < j) {
- if (word.charAt(i++) != word.charAt(j--)) return false;
- }
- return true;
- }
- }
复制代码
|