富途笔试

3.27 富途 研发笔试,第一题出栈顺序,有人AC吗,我本地测试五组数据都没有问题,但是调试为0,好奇怪啊,求一个答案#富途##笔试题目#
全部评论
public int[] solve (int[] a) {         List<Integer> list = new ArrayList<>();         for(int i=0;i<a.length;i++){             list.add(a[i]);         }         List<Integer> list1 = new ArrayList<>();         Arrays.sort(a);         boolean[] paduan = new boolean[a.length];         int j = a.length-1;         Stack<Integer> stack = new Stack<>();         for(int i=0;i<a.length;i++){             int temp = list.get(i);             if(temp!=a[j]){                 stack.push(temp);                 paduan[i]=true;             }             if(temp==a[j]){                 if(paduan[i]==true){                     list1.add(stack.pop());                 }else{                     paduan[i]=true;                     list1.add(temp);                 }                 j--;             }         }         while(!stack.isEmpty()){             list1.add(stack.pop());         }         for(int i=0;i<list1.size();i++){             a[i] = list1.get(i);         }         return a;     }
点赞 回复 分享
发布于 2021-03-27 16:58
这个我是用递归a的,代码如下: string s1 = "down"; string s2 = "up"; void solve(int pre, int k, int cnt, int &n, vector<string> &ans) {     if (cnt >= n + 1) return;     ans[(k + pre) / 2] = s1;     ans[(k - pre + 1) / 2 + k] = s2;     solve(pre, (k + pre) / 2, cnt + 1, n, ans);     solve(k, (k - pre + 1) / 2 + k, cnt + 1, n, ans);     return; } vector<string> problem(int n) {     // write code here     int num = 1;     for (int i = 0; i < n; ++i)         num *= 2;     num--;     vector<string> ans(num);     ans[num / 2] = s1;     solve(0, num / 2, 2, n, ans);     return ans; }
点赞 回复 分享
发布于 2021-03-27 16:46

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务