全部评论
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; }
这个我是用递归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; }
相关推荐
07-17 09:30
门头沟学院 客户端其它 爱睡觉的冰箱哥:是这样,实习一两段达到过简历关门槛,加上把简历玩具项目替换掉,实习的目的就达成了一大半
点赞 评论 收藏
分享
05-30 00:12
江西服装学院 Java 头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享