顯示具有 LeetCode 標籤的文章。 顯示所有文章
顯示具有 LeetCode 標籤的文章。 顯示所有文章

2019/02/01

Leetcode 869. Reordered Power of 2

869. Reordered Power of 2
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return true if and only if we can do this in a way such that the resulting number is a power of 2.
 Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Note:
  1. 1 <= N <= 10^9

題意就是檢查輸入的數字能不能重組成2的次方倍,例如 46 可以重組成 64,是 2 的次方倍,所以要傳回 true。採用的方法就是計算個別數字出現的次數,跟 2 的次方倍相比,次數相同就表示可以重組成功。因為題目有限縮 N 的範圍,所以可以利用這一點來加快執行速度,程式碼如下:



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
bool check(int* d, int T) {
 int t[10] = {0};
 int m;
 
 while (T) {
  m = T % 10;
  ++t[m];
  T /= 10;
 }

 for (m=0; m<10; m++) {
  if (d[m] != t[m]) {
   return false;
  }
 }
 return true;
}

bool reorderedPowerOf2(int N) {
 int i, n, m, s, z, d[10]={0};
 
 if (N < 128) {
  if (64 == N) {
   return true;
  }
  if (46 == N) {
   return true;
  }
  if (32 == N) {
   return true;
  }
  if (23 == N) {
   return true;
  }
  if (16 == N) {
   return true;
  }
  if (61 == N) {
   return true;
  }
  if (8 == N) {
   return true;
  }
  if (4 == N) {
   return true;
  }
  if (2 == N) {
   return true;
  }
  if (1 == N) {
   return true;
  }
 }
 
 n = N;
 i = 0;
 s = 0;
 while (n) {
  m = n % 10;
  s += m;
  ++d[m];
  n /= 10;
 }

    if (N >= 100000000) {
  if ((41 == s) && check(d, 536870912)) {
   return true;
  }
  if ((43 == s) && check(d, 268435456)) {
   return true;
  }
  if ((35 == s) && check(d, 134217728)) {
   return true;
  }
 } else if (N >= 10000000) {
  if ((40 == s) && check(d, 67108864)) {
   return true;
  }
  if ((29 == s) && check(d, 33554432)) {
   return true;
  }
  if ((37 == s) && check(d, 16777216)) {
   return true;
  }
 } else if (N >= 1000000) {
  if ((41 == s) && check(d, 8388608)) {
   return true;
  }
  if ((25 == s) && check(d, 4194304)) {
   return true;
  }
  if ((26 == s) && check(d, 2097152)) {
   return true;
  }
  if ((31 == s) && check(d, 1048576)) {
   return true;
  }
 } else if (N >= 100000) {
  if ((29 == s) && check(d, 524288)) {
   return true;
  }
  if ((19 == s) && check(d, 262144)) {
   return true;
  }
  if ((14 == s) && check(d, 131072)) {
   return true;
  }
 } else if (N >= 10000) {
  if ((25 == s) && check(d, 65536)) {
   return true;
  }
  if ((26 == s) && check(d, 32768)) {
   return true;
  }
  if ((22 == s) && check(d, 16384)) {
   return true;
  }
 } else if (N >= 1000) { 
  if ((20 == s) && check(d, 8192)) {
   return true;
  }
  if ((19 == s) && check(d, 4096)) {
   return true;
  }
  if ((14 == s) && check(d, 2048)) {
   return true;
  }
  if ((7 == s) && check(d, 1024)) {
   return true;
  }
 } else if (N >= 100) {
  if ((8 == s) && check(d, 512)) {
   return true;
  }
  if ((13 == s) && check(d, 256)) {
   return true;
  }
  if ((11 == s) && check(d, 128)) {
   return true;
  }
 }
 
 return false;
}

另一個解法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
bool check(int* d, int T) {
 int t[10] = {0};
 int m;

 while (T) {
  ++t[T % 10];
  T /= 10;
 }
 
 for (m=0; m<10; m++) {
  if (d[m] != t[m]) {
   return false;
  }
 }
 return true;
}

bool reorderedPowerOf2(int N) {
 int c, i, n, T;
 int d[10] = {0};
 int r[32] = {0,1,1,1,1,2,2,2,3,3,3,4,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,10};

 if (1==N || 2==N || 4==N || 8==N) {
  return true;
 }

 n = N;
 c = 0;
 while (n) {
  ++d[n % 10];
  n /= 10;
  ++c;
 }

 T=16;
 for (i=5;i<32;i++) {
  if (r[i] > c) {
   break;
  } else if (r[i] == c && check(d, T)) {
   return true;
  }
  T<<=1;
 }

 return false;
}

2019/01/19

leetcode 885. Spiral Matrix III

On a 2 dimensional grid with R rows and C columns, we start at (r0, c0)facing east.
Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and column.
Now, we walk in a clockwise spiral shape to visit every position in this grid. 
Whenever we would move outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.) 
Eventually, we reach all R * C spaces of the grid.
Return a list of coordinates representing the positions of the grid in the order they were visited.

Example 1:
Input: R = 1, C = 4, r0 = 0, c0 = 0
Output: [[0,0],[0,1],[0,2],[0,3]]



Example 2:
Input: R = 5, C = 6, r0 = 1, c0 = 4
Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]



Note:
  1. 1 <= R <= 100
  2. 1 <= C <= 100
  3. 0 <= r0 < R
  4. 0 <= c0 < C

這題用沒有向量的C不是很容易解,只好用暴力的方式求解,利用的方法很單純,就是讓 (r0, c0) 去繞圈圈,而且根據旋轉的特性,可以發現右下左上這樣的順序,格數變量是 1,1,2,2,3,3,4,4....,所以程式就長成下面這樣。leetcode 報 20ms,目前百分位置是 100%,期待出現更快的解法可以參考。

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *columnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** spiralMatrixIII(int R, int C, int r0, int c0, int** columnSizes, int* returnSize) {
    int **ret;
    int i, j;
    int step;

    *returnSize = R * C;
    columnSizes[0] = (int *) malloc (R * C * sizeof(int));
    ret = (int **) malloc (R * C * sizeof(int *));

    i = 0;
    ret[i] = (int *) malloc (2 * sizeof(int));
    ret[i][0] = r0;
    ret[i][1] = c0;
    columnSizes[0][i] = 2;
    ++i;
    if (i == *returnSize) {
        return ret;
    }

    step = 1;
    while(1) {
        if (r0 >=0 && r0 < R) {
            for (j=1; j<=step; j++) {
                ++c0;
                if (c0 >= 0 && c0 < C) {
                    ret[i] = (int *) malloc (2 * sizeof(int));
                    ret[i][0] = r0;
                    ret[i][1] = c0;
                    columnSizes[0][i] = 2;
                    ++i;
                    if (i == *returnSize) {
                        return ret;
                    }
                }
            }
        } else {
            c0 += step;
        }
        if (c0 >= 0 && c0 < C) {
            for (j=1; j<=step; j++) {
                ++r0;
                if (r0 >=0 && r0 < R) {
                    ret[i] = (int *) malloc (2 * sizeof(int));
                    ret[i][0] = r0;
                    ret[i][1] = c0;
                    columnSizes[0][i] = 2;
                    ++i;
                    if (i == *returnSize) {
                        return ret;
                    }
                }
            }
        } else {
            r0 += step;
        }
        ++step;
        if (r0 >= 0 && r0 < R) {
            for (j=1; j<=step; j++) {
                --c0;
                if (c0 >= 0 && c0 < C) {
                    ret[i] = (int *) malloc (2 * sizeof(int));
                    ret[i][0] = r0;
                    ret[i][1] = c0;
                    columnSizes[0][i] = 2;
                    ++i;
                    if (i == *returnSize) {
                        return ret;
                    }
                }
            }
        } else {
            c0 -= step;
        }
        if (c0 >= 0 && c0 < C) {
            for (j=1; j<=step; j++) {
                --r0;
                if (r0 >=0 && r0 < R) {
                    ret[i] = (int *) malloc (2 * sizeof(int));
                    ret[i][0] = r0;
                    ret[i][1] = c0;
                    columnSizes[0][i] = 2;
                    ++i;
                    if (i == *returnSize) {
                        return ret;
                    }    
                }
            }
        } else {
            r0 -= step;
        }
        ++step;
    }
    return ret;
}

2016/08/10

LeetCode 65. Valid Number

Validate if a given string is numeric.
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

解法真是非常呆,不過直覺也不是壞事
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
bool isNumber(char* s) {
    int len=strlen(s);
    int i;
    bool havedot=false;
    bool havee=false;
    bool havespace=false;
    bool havenum=false;
    bool havesymbol=false;

    for(i=0; i<len; i++)
    {
        if(s[i]<0x30 || s[i]>0x39)
        {
            if(' ' == s[i])
            {
                if(havenum || havee || havedot || havesymbol)
                {
                    havespace = true;
                }
            }
            else if('.' == s[i])
            {
                if(havee || havedot || havespace)
                {
                    return false;
                }
                else
                {
                    havedot = true;
                }
            }
            else if('e' == s[i])
            {
                if(havee || !havenum)
                {
                    return false;
                }
                else
                {
                    havee = true;
                    havenum = false;
                    havesymbol = false;
                }
            }
            else if('+' == s[i] || '-' == s[i])
            {
                if (havee)
                {
                    if (havesymbol || havenum)
                    {
                        return false;
                    }
                }
                else
                {
                    if (havesymbol || havenum || havedot)
                    {
                        return false;
                    }
                }
                havesymbol = true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            if(havespace)
            {
                return false;
            }
            havenum = true;
        }
    }
    return havenum;
}