Skip to content

Commit 1a3165e

Browse files
committed
400_Nth_Digit and 463_Island_Perimeter
1 parent c4e6523 commit 1a3165e

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Remember solutions are only solutions to given problems. If you want full study
118118
| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/387_First_Unique_Character_in_a_String.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/387_First_Unique_Character_in_a_String.java) | Get frequency of each letter, return first letter with frequency 1, O(n) and O(1) |
119119
| 388 | [Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/388_Longest_Absolute_File_Path.py) | Store last length and rindex, O(n) and O(n) |
120120
| 389 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/389_Find_the_Difference.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/389_Find_the_Difference.java) | 1. Imaging letter a as 0, then the sum(t)-sum(s) is the result<br> 2. Based on solution 1, bit manipulate |
121+
| 400 | [Nth Digit](https://leetcode.com/problems/nth-digit/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/400_Nth_Digit.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/400_Nth_Digit.java) | islands * 4 - overlaps * 2 |
121122
| 401 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/401_Binary_Watch.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/401_Binary_Watch.java) | Note that 12 * 60 is much less than 2^n or n^2. |
122123
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/404_Sum_of_Left_Leaves.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/404_Sum_of_Left_Leaves.java) | 1. Recursively DFS with root.left.left and root.left.right check<br>2. The same DFS based on stack |
123124
| 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/405_Convert_a_Number_to_Hexadecimal.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/405_Convert_a_Number_to_Hexadecimal.java) | [Two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) 1. Bit manipulate, each time handle 4 digits<br>2. Python (hex) and Java API (toHexString & Integer.toHexString) |
@@ -126,6 +127,7 @@ Remember solutions are only solutions to given problems. If you want full study
126127
| 416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/416_Partition_Equal_Subset_Sum.py) | DP, Check if sum of some elements can be half of total sum, O(total_sum / 2 * n) and O(total_sum / 2) |
127128
| 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/421_Maximum_XOR_of_Two_Numbers_in_an_Array.py) | Check 0~32 prefix, check if there is x y in prefixes, where x ^ y = answer ^ 1, O(32n) and O(n) |
128129
| 422 | [Valid Word Square](https://leetcode.com/problems/valid-word-square/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/422_Valid_Word_Square.py) | Compare row with column, O(n^2) |
130+
| 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/463_Island_Perimeter.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/463_Island_Perimeter.java) | math, find the area, actual number, then find the digit |
129131

130132
| # | To Understand |
131133
|---| ----- |

java/400_Nth_Digit.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int islandPerimeter(int[][] grid) {
3+
// https://leetcode.com/problems/island-perimeter/discuss/95001/clear-and-easy-java-solution
4+
int islands = 0, neighbours = 0;
5+
for (int i = 0; i < grid.length; i++) {
6+
for (int j = 0; j < grid[i].length; j++) {
7+
if (grid[i][j] == 1) {
8+
islands++; // count islands
9+
if (i < grid.length - 1 && grid[i + 1][j] == 1) neighbours++; // count down neighbours
10+
if (j < grid[i].length - 1 && grid[i][j + 1] == 1) neighbours++; // count right neighbours
11+
}
12+
}
13+
}
14+
15+
return islands * 4 - neighbours * 2;
16+
}
17+
}

java/463_Island_Perimeter.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int findNthDigit(int n) {
3+
int len = 1;
4+
long count = 9;
5+
int start = 1;
6+
// https://leetcode.com/problems/nth-digit/discuss/88363/Java-solution
7+
while (n > len * count) {
8+
n -= len * count;
9+
len += 1;
10+
count *= 10;
11+
start *= 10;
12+
}
13+
14+
start += (n - 1) / len;
15+
String s = Integer.toString(start);
16+
return Character.getNumericValue(s.charAt((n - 1) % len));
17+
}
18+
}

python/400_Nth_Digit.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def islandPerimeter(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: int
6+
"""
7+
# https://leetcode.com/problems/island-perimeter/discuss/95001/clear-and-easy-java-solution
8+
row_num = len(grid)
9+
if row_num == 0 || len(grid[0]) == 0:
10+
return 0
11+
islands, overlaps = 0, 0
12+
col_num = len(grid[0])
13+
for i in range(row_num):
14+
for j in range(col_num):
15+
if (grid[i][j] == 1):
16+
islands += 1
17+
# careful about right and down
18+
if (i < row_num - 1 && grid[i + 1][j] == 1):
19+
overlaps += 1
20+
if (j < col_num - 1 && grid[i][j + 1] == 1):
21+
overlaps += 1
22+
return islands * 4 - overlaps * 2

python/463_Island_Perimeter.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def findNthDigit(self, n):
3+
"""
4+
:type n: int
5+
:rtype: int
6+
"""
7+
# https://leetcode.com/problems/nth-digit/discuss/88363/Java-solution
8+
count = 9
9+
start = 1
10+
curr_len = 1
11+
while n > curr_len * count:
12+
n -= curr_len * count
13+
curr_len += 1
14+
count *= 10
15+
start *= 10
16+
start += (n - 1) / curr_len
17+
s = str(start)
18+
return int(s[(n - 1) % curr_len]
19+
20+

0 commit comments

Comments
 (0)