File tree 4 files changed +176
-0
lines changed
4 files changed +176
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .HashMap ;
2
+ import java .util .Map ;
3
+
4
+ /*
5
+ * @lc app=leetcode.cn id=171 lang=java
6
+ *
7
+ * [171] Excel表列序号
8
+ */
9
+
10
+ // @lc code=start
11
+ class Solution {
12
+ public int titleToNumber (String s ) {
13
+ // Map<String,Integer> map = new HashMap<>();
14
+ // map.put("A", 1);
15
+ // map.put("B", 2);
16
+ // map.put("C", 3);
17
+ // map.put("D", 4);
18
+ // map.put("E", 5);
19
+ // map.put("F", 6);
20
+ // map.put("G", 7);
21
+ // map.put("H", 8);
22
+ // map.put("I", 9);
23
+ // map.put("J", 10);
24
+ // map.put("K", 11);
25
+ // map.put("L", 12);
26
+ // map.put("M", 13);
27
+ // map.put("N", 14);
28
+ // map.put("O", 15);
29
+ // map.put("P", 16);
30
+ // map.put("Q", 17);
31
+ // map.put("R", 18);
32
+ // map.put("S", 19);
33
+ // map.put("T", 20);
34
+ // map.put("U", 21);
35
+ // map.put("V", 22);
36
+ // map.put("W", 23);
37
+ // map.put("X", 24);
38
+ // map.put("Y", 25);
39
+ // map.put("Z", 26);
40
+ // char[] chars = s.toCharArray();
41
+ // int count = 0;
42
+ // for(int i = 0;i<chars.length;i++){
43
+ // count*=26;
44
+ // count+=map.get(String.valueOf(chars[i]));
45
+ // }
46
+ // return count;
47
+
48
+ int count = 0 ;
49
+ char [] chars = s .toCharArray ();
50
+ for (int i =0 ;i <chars .length ;i ++){
51
+ count = count * 26 + (chars [i ] - 'A' +1 );
52
+ }
53
+ return count ;
54
+ }
55
+ }
56
+ // @lc code=end
57
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=278 lang=java
3
+ *
4
+ * [278] 第一个错误的版本
5
+ */
6
+
7
+ // @lc code=start
8
+ /* The isBadVersion API is defined in the parent class VersionControl.
9
+ boolean isBadVersion(int version); */
10
+
11
+ public class Solution extends VersionControl {
12
+ public int firstBadVersion (int n ) {
13
+ int low = 1 ;
14
+ int big = n ;
15
+ int mid = low + (big -low )/2 ;
16
+ int index = -1 ;
17
+ while (low <= big ){
18
+ if (isBadVersion (mid )){
19
+ big =mid -1 ;
20
+ index = mid ;
21
+ }else {
22
+ low = mid +1 ;
23
+ }
24
+ mid = low + (big - low )/2 ;
25
+ }
26
+ return index ;
27
+
28
+ }
29
+ }
30
+ // @lc code=end
31
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=374 lang=java
3
+ *
4
+ * [374] 猜数字大小
5
+ */
6
+
7
+ // @lc code=start
8
+ /**
9
+ * Forward declaration of guess API.
10
+ * @param num your guess
11
+ * @return -1 if num is lower than the guess number
12
+ * 1 if num is higher than the guess number
13
+ * otherwise return 0
14
+ * int guess(int num);
15
+ */
16
+
17
+ public class Solution extends GuessGame {
18
+ public int guessNumber (int n ) {
19
+ int half = 1 + (n -1 )/2 ;
20
+ int low = 1 ;
21
+ int big = n ;
22
+ int result = 0 ;
23
+ while (low <= big ){
24
+ result = guess (half );
25
+ if (result == -1 ){
26
+ big = half -1 ;
27
+ half = low + (big - low )/2 ;
28
+ }else if (result == 1 ){
29
+ low = half +1 ;
30
+ half = low +(big -low )/2 ;
31
+ }else {
32
+ return half ;
33
+ }
34
+ }
35
+ return 1 ;
36
+ }
37
+ }
38
+ // @lc code=end
39
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=605 lang=java
3
+ *
4
+ * [605] 种花问题
5
+ */
6
+
7
+ // @lc code=start
8
+ class Solution {
9
+ public boolean canPlaceFlowers (int [] flowerbed , int n ) {
10
+ int count = n ;
11
+ int i = 0 ;
12
+ while (i < flowerbed .length && count > 0 ){
13
+ if (i == 0 ){
14
+ if (flowerbed .length == 1 ){
15
+ if (flowerbed [i ] == 0 ){
16
+ count --;
17
+ }
18
+ break ;
19
+ }
20
+ if (flowerbed [i ] == 0 && flowerbed [i +1 ]==0 ){
21
+ count --;
22
+ i +=2 ;
23
+ }else {
24
+ i ++;
25
+ }
26
+ }else if (i == flowerbed .length -1 ){
27
+ if (flowerbed [i ] == 0 && flowerbed [i -1 ] == 0 ){
28
+ count --;
29
+ }
30
+ i ++;
31
+ }else if (flowerbed [i ] == 0 ){
32
+ if (flowerbed [i -1 ] == 0 && flowerbed [i +1 ] == 0 ){
33
+ count --;
34
+ i +=2 ;
35
+ }else {
36
+ i ++;
37
+ }
38
+ }else {
39
+ i +=2 ;
40
+ }
41
+ }
42
+ if (count <=0 ){
43
+ return true ;
44
+ }
45
+ return false ;
46
+ }
47
+ }
48
+ // @lc code=end
49
+
You can’t perform that action at this time.
0 commit comments