Skip to content

Commit e701bd7

Browse files
authored
Added tasks 233, 234.
1 parent 6fa27f3 commit e701bd7

File tree

7 files changed

+155
-1
lines changed

7 files changed

+155
-1
lines changed

src/main/java/g0001_0100/s0052_n_queens_ii/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public int totalNQueens(int n) {
99
return totalNQueens(n, 0, row, col, diagonal, antiDiagonal);
1010
}
1111

12-
private static int totalNQueens(
12+
private int totalNQueens(
1313
int n,
1414
int r,
1515
boolean[] row,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0201_0300.s0233_number_of_digit_one;
2+
3+
@SuppressWarnings("java:S127")
4+
public class Solution {
5+
public int countDigitOne(int n) {
6+
int ans = 0;
7+
// count total number of 1s appearing in every digit, starting from the last digit
8+
for (int k = n, cum = 0, curr10 = 1; k > 0; curr10 *= 10) {
9+
int rem = k % 10;
10+
int q = k / 10;
11+
if (rem == 0) {
12+
ans += q * curr10;
13+
} else if (rem == 1) {
14+
ans += q * curr10 + cum + 1;
15+
} else {
16+
ans += (q + 1) * curr10;
17+
}
18+
k = q;
19+
// if loop is at 3rd last digit and n = 54321, cum is now = 321
20+
cum += rem * curr10;
21+
}
22+
return ans;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
233\. Number of Digit One
2+
3+
Hard
4+
5+
Given an integer `n`, count _the total number of digit_ `1` _appearing in all non-negative integers less than or equal to_ `n`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 13
10+
11+
**Output:** 6
12+
13+
**Example 2:**
14+
15+
**Input:** n = 0
16+
17+
**Output:** 0
18+
19+
**Constraints:**
20+
21+
* <code>0 <= n <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0201_0300.s0234_palindrome_linked_list;
2+
3+
import com_github_leetcode.ListNode;
4+
5+
public class Solution {
6+
public boolean isPalindrome(ListNode head) {
7+
int len = 0;
8+
ListNode right = head;
9+
// Culculate the length
10+
while (right != null) {
11+
right = right.next;
12+
len++;
13+
}
14+
// Reverse the right half of the list
15+
len = len / 2;
16+
right = head;
17+
for (int i = 0; i < len; i++) {
18+
right = right.next;
19+
}
20+
ListNode prev = null;
21+
while (right != null) {
22+
ListNode next = right.next;
23+
right.next = prev;
24+
prev = right;
25+
right = next;
26+
}
27+
// Compare left half and right half
28+
for (int i = 0; i < len; i++) {
29+
if (prev != null && head.val == prev.val) {
30+
head = head.next;
31+
prev = prev.next;
32+
} else {
33+
return false;
34+
}
35+
}
36+
return true;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
234\. Palindrome Linked List
2+
3+
Easy
4+
5+
Given the `head` of a singly linked list, return `true` if it is a palindrome.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg)
10+
11+
**Input:** head = \[1,2,2,1\]
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg)
18+
19+
**Input:** head = \[1,2\]
20+
21+
**Output:** false
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.
26+
* `0 <= Node.val <= 9`
27+
28+
**Follow up:** Could you do it in `O(n)` time and `O(1)` space?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0201_0300.s0233_number_of_digit_one;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void countDigitOne() {
11+
assertThat(new Solution().countDigitOne(13), equalTo(6));
12+
}
13+
14+
@Test
15+
public void countDigitOne2() {
16+
assertThat(new Solution().countDigitOne(0), equalTo(0));
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0201_0300.s0234_palindrome_linked_list;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.ListNode;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void isPalindrome() {
12+
ListNode headActual = new ListNode(1);
13+
headActual.next = new ListNode(2);
14+
headActual.next.next = new ListNode(2);
15+
headActual.next.next.next = new ListNode(1);
16+
assertThat(new Solution().isPalindrome(headActual), equalTo(true));
17+
}
18+
19+
@Test
20+
public void isPalindrome2() {
21+
ListNode headActual = new ListNode(1);
22+
headActual.next = new ListNode(2);
23+
assertThat(new Solution().isPalindrome(headActual), equalTo(false));
24+
}
25+
}

0 commit comments

Comments
 (0)