携程算法笔试求解惑……

第一题那个时刻表发车的题目……完全读不懂那个题什么意思…………
想知道为啥我只能过 12%………………
schedule = input()
res = []

p = list(set(list(schedule)))
count = 0
path = {}
# 建立一个哈希表,记录所有的字母出现的次数
for i in p:
    path[i] = 0
for i in schedule:
    # 如果是起点,就置为 1
    if path[i] == 0:
        path[i] = 1
        count += 1
    # 如果到终点,就置 0,然后判断是不是所有车都到终点了
    else:
        path[i] = 0
        count += 1
        if sum(path.values()) == 0:
            res.append(count)
            count = 0

print(','.join(map(str, res)))


#携程##笔试题目#
全部评论
s = input() res = [] ptr = 0 for i in range(len(s)):     ptr = max(ptr, s.rindex(s[i]))     if i == ptr:         res.append(ptr+1-sum(res))         ptr = 0 for x in res[:-1]:     print(x, end=',') print(res[-1]) ac的代码
点赞 回复
分享
发布于 2019-09-04 21:05
合并区间
点赞 回复
分享
发布于 2019-09-04 21:03
滴滴
校招火热招聘中
官网直投
很简单吧
点赞 回复
分享
发布于 2019-09-04 21:05
#include<iostream> #include<set> #include<string> #include<vector> #include<algorithm> using namespace std;  bool cmpa(vector<int>& a, vector<int>& b) { if (a[0] < b[0]) return true; else if (a[0] > b[0]) return false; else { if (a[1]<b[1]) return true; else return false; } } vector<vector<int>> merge(vector<vector<int>>& intervals) { sort(intervals.begin(), intervals.end(), cmpa); vector<vector<int>> res; for (int i = 0; i < intervals.size(); i++) { int size = res.size(); if (res.empty() || res[size - 1][1]<intervals[i][0]) res.push_back(intervals[i]); else res[size - 1][1] = max(intervals[i][1], res[size - 1][1]); } return res; } int main() { string in; cin >> in; int len = in.size(); set<char> se; vector<vector<int>> arr; for (int i = 0; i < len;i++) { if (se.find(in[i]) == se.end()) { int first = in.find_first_of(in[i]); int end1 = in.find_last_of(in[i]); se.insert(in[i]);  vector<int > temp; temp.push_back(first); temp.push_back(end1); arr.push_back(temp); } } vector<vector<int>>  res = merge(arr); for (int i = 0; i < res.size(); i++) { if (i == (res.size() - 1))cout << res[i][1] - res[i][0] + 1<<endl; else cout << res[i][1] - res[i][0] + 1 << ","; } system("pause"); return 0; }
点赞 回复
分享
发布于 2019-09-04 21:07
s_list = list(input()) from collections import defaultdict count_dict = defaultdict(int) for item in s_list:     count_dict[item]+=1 group = [] res = [] group_dict = defaultdict(int) for item in s_list:     group.append(item)       for g_item in group:         group_dict[g_item]+=1     key_list = list(group_dict.keys())     index  = 0      while index <len(key_list):         if group_dict[key_list[index]] == count_dict[key_list[index]]:             index +=1         else:             break      group_dict = defaultdict(int)     print(index)     if index== len(key_list):         res.append(len(group))         group = []        print(' '.join([str(item) for item in res])) 写的蠢蠢的。。凑活看
点赞 回复
分享
发布于 2019-09-04 21:14
区间合并
点赞 回复
分享
发布于 2019-09-04 21:17
在本地运行没问题,但复制过去提示“运行错误” int main() { char *str = new char(); cin >> str; char *temp = str; unordered_map<char, vector<int>> pos; // 储存每个字符的最初位置和最末位置 int i = 0; while (*(temp+i) != '\0') {         // 没有第二个位置时添加         if(pos[*(temp+i)].size() < 2) pos[*(temp+i)].push_back(i); // 否则覆盖结束位置         else pos[*(temp + i)][1] = i; i++; } // 遍历字符串,将字符初末位置算进来[start, end]闭区间 // 遍历中间,更新末位置 i = 0; int len; int start = 0, end = 0; queue<char> que; while (*(temp + i) != '\0') { que.push(*(temp+i)); start = pos[*(temp + i)][0]; end = start; while (!que.empty()) { char c = que.front(); que.pop(); if (pos[c].size() > 1 && pos[c][1] > end) { // 将当前[end,pos[c][1])加入队列 int j = end + 1; while (j <= pos[c][1]) { que.push(*(temp + i)); j++; } end = pos[c][1]; i = end + 1; }  } cout << end - start + 1 << " "; } //system("pause"); return 0; }
点赞 回复
分享
发布于 2019-09-04 21:23
所以算法岗可以python。。自闭了
点赞 回复
分享
发布于 2019-09-04 21:23

相关推荐

点赞 评论 收藏
转发
点赞 5 评论
分享
牛客网
牛客企业服务