生活随笔
收集整理的這篇文章主要介紹了
秋招面/笔试题目集合——06
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一題
思路:遞歸(打標法)
public class 第一題
06 {public static void main(String[] args
) {for (int i
= 1;i
< 500;i
+= 2){String res
= WhoWin(i
);System.out
.println(i
+ "的贏家是: " + res
);}}public static String WhoWin(int balls
){return process(0,balls
,0,0);}private static String process(int turn
, int rest
, int jia
, int yi
) {if (rest
== 0){return jia
% 2 == 0 ? "甲" : "乙";}if (turn
== 0){for (int i
= 1;i
<= Math.min(rest
,3);i
++){String next
= process(1,rest
- i
,jia
+ i
,yi
);if (next
.equals("甲")){return "甲";}}return "乙";}else {for (int i
= 1;i
<= Math.min(rest
,3);i
++){String next
= process(0,rest
- i
,jia
,yi
+ i
);if (next
.equals("乙")){return "乙";}}return "甲";}}
}
第二題
leetcode862. 和至少為 K 的最短子數組
思路:雙端隊列 + 前綴和
public class 第二題
06 {public static void main(String[] args
) {int[] arr
= {3,-2,1,3,-5,2};int res
= shortestSubarray(arr
, 5);System.out
.println(res
);}public static int shortestSubarray(int[] nums
, int k
) {int n
= nums
.length
;long[] preSums
= new long[n
+ 1];for (int i
= 0;i
< n
;i
++){preSums
[i
+ 1] = preSums
[i
] + nums
[i
];}int ans
= Integer.MAX_VALUE;int[] dp
= new int[n
+ 1];int l
= 0;int r
= 0;for (int i
= 0;i
<= n
;i
++){while (l
< r
&& preSums
[dp
[r
- 1]] > preSums
[i
]){r
--;}while (l
< r
&& preSums
[i
] - preSums
[dp
[l
]] >= k
){ans
= Math.min(ans
,i
- dp
[l
++]);}dp
[r
++] = i
;}return ans
== Integer.MAX_VALUE ? -1 : ans
;}
}
總結
以上是生活随笔為你收集整理的秋招面/笔试题目集合——06的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。