注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
很久没消息,应该就是挂了。觉得他家recruiter很rude,虽然挂了你好歹发封邮件回下啊,结果都是默拒,所以不知道feedback。Phone题目:
calculator I,比leetcode LC 224简单些
Onsite题目:
问复杂度,卡了下。这种题建议大家练习的时候要注意,很多人刷过就过了,因为题目不难,但复杂度有时候挺麻烦,可以参考leetcode区的讨论。- import java.util.*;
- // This is the text editor interface.
- // Anything you type or change here will be seen by the other person in real time.
- public class Solution {
- public static void main(String [] args) {
- List<String> input = new ArrayList<String>();
- input.add("Col1,Col2");
- input.add("\"a\",b");
- input.add("\"c,d\", \"e\\\"f");
- print(input);
- }
-
- static void print(List<String> l) {
- if(l != null && l.size() > 0) {
- List<String> cols = parse(l.get(0));
- //System.out.println("[");
-
- List<HashMap<String, String>> res = new ArrayList<HashMap<String, String>>();
-
- for(int i = 1; i < l.size(); i++) {
- //System.out.println(" {");
- List<String> words = parse(l.get(i));
- res.add(new HashMap<String, String>());
- for(int j = 0; j < cols.size(); j++) {
- //System.out.println(" " + cols.get(j) + ":" + words.get(j));
- if(res.get(res.size() - 1).containsKey(cols.get(j))) {
- throw new IllegalArgumentException("Duplicate col names");
- } else {
- res.get(res.size() - 1).put(cols.get(j), words.get(j));
- }
- }
- //System.out.println(" },");
- }
- //System.out.println("]");
- System.out.print(res);
- }
- }
-
- static List<String> parse(String s) {
- List<String> res = new ArrayList<String>();
- if(s != null && s.length() > 0) {
- int i = 0;
- boolean inQut = false;
- StringBuffer sb = new StringBuffer();
- while(i < s.length()) {
- char c = s.charAt(i);
- if (c == '"') {
- if(!inQut) {
- inQut = true;
- } else {
- inQut = false;
- }
- } else if (c == '\\' && inQut) {
- if(i < s.length() - 1) {
- char next = s.charAt(i + 1);
- sb.append(next);
- i++;
- } else {
- throw new IllegalArgumentException("Invalid escape");
- }
- } else if (c == ',' && !inQut) {
- res.add(new String(sb));
- sb = new StringBuffer();
- } else {
- if(!(c == ' ' && !inQut)) {
- sb.append(c);
- }
- }
- i++;
- }
- res.add(new String(sb));
- }
- return res;
- }
- }
复制代码 |