File tree Expand file tree Collapse file tree 2 files changed +61
-5
lines changed
Expand file tree Collapse file tree 2 files changed +61
-5
lines changed Original file line number Diff line number Diff line change 77// @lc code=start
88class Solution {
99 public int kthGrammar (int n , int k ) {
10- // 0
11- // 01
12- // 0110
13- // 01 10 10 01
14- // 01 10 10 01 10 01 01 10
10+ int s = 1 ;
11+ while (s < k ) {
12+ s *= 2 ;
13+ }
14+ int i = 0 ;
15+ while (k > 1 ) {
16+ s /= 2 ;
17+ if (k > s ) {
18+ k -= s ;
19+ i ++;
20+ }
21+ }
22+ if (k == 0 ) {
23+ if (i % 2 == 0 ) {
24+ return 1 ;
25+ }
26+ return 0 ;
27+ }
28+ if (i % 2 == 0 ) {
29+ return 0 ;
30+ }
31+ return 1 ;
1532 }
1633}
1734// @lc code=end
Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .List ;
3+
4+ /*
5+ * @lc app=leetcode.cn id=901 lang=java
6+ *
7+ * [901] 股票价格跨度
8+ */
9+
10+ // @lc code=start
11+ class StockSpanner {
12+
13+ static List <Integer > spans = new ArrayList <>();
14+ static List <Integer > prices = new ArrayList <>();
15+ public StockSpanner () {
16+ spans .clear ();
17+ prices .clear ();
18+ }
19+
20+ public int next (int price ) {
21+ int i = prices .size ();
22+ prices .add (price );
23+ int s = 1 ;
24+ while (i > 0 && price >= prices .get (i - 1 )) {
25+ s += spans .get (i - 1 );
26+ i -= spans .get (i - 1 );
27+ }
28+ spans .add (s );
29+ return s ;
30+ }
31+ }
32+
33+ /**
34+ * Your StockSpanner object will be instantiated and called as such:
35+ * StockSpanner obj = new StockSpanner();
36+ * int param_1 = obj.next(price);
37+ */
38+ // @lc code=end
39+
You can’t perform that action at this time.
0 commit comments