Skip to content

Commit 32207f5

Browse files
committed
Added 1 solution & modified 1 solution
1 parent 20b239d commit 32207f5

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

Easy/Palindrome Permutation.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
class Solution {
2-
public static boolean canPermutePalindrome(String s) {
3-
Map<Character, Integer> map = new HashMap<>();
4-
char[] chars = s.toCharArray();
5-
6-
for (char c : chars) {
7-
map.put(c, map.getOrDefault(c, 0) + 1);
8-
}
9-
10-
int oddVal = 0;
11-
12-
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
13-
if (entry.getValue()%2 != 0) {
14-
oddVal++;
15-
}
16-
17-
if (oddVal == 2) {
18-
return false;
19-
}
2+
public boolean canPermutePalindrome(String s) {
3+
Map<Character, Integer> map = new HashMap<>();
4+
for (char c : s.toCharArray()) {
5+
map.put(c, map.getOrDefault(c, 0) + 1);
6+
}
7+
boolean oddFound = false;
8+
for (Character key : map.keySet()) {
9+
if (map.get(key) % 2 != 0) {
10+
if (oddFound) {
11+
return false;
2012
}
21-
22-
return true;
13+
oddFound = true;
14+
}
2315
}
16+
return true;
17+
}
2418
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* // This is the BinaryMatrix's API interface.
3+
* // You should not implement it, or speculate about its implementation
4+
* interface BinaryMatrix {
5+
* public int get(int x, int y) {}
6+
* public List<Integer> dimensions {}
7+
* };
8+
*/
9+
10+
class Solution {
11+
public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) {
12+
List<Integer> dims = binaryMatrix.dimensions();
13+
int rows = dims.get(0);
14+
int cols = dims.get(1);
15+
int minIdx = Integer.MAX_VALUE;
16+
for (int i = 0; i < rows; i++) {
17+
int idx = binarySearch(binaryMatrix, i, 0, cols - 1);
18+
if (idx != -1) {
19+
minIdx = Math.min(minIdx, idx);
20+
}
21+
}
22+
return minIdx == Integer.MAX_VALUE ? -1 : minIdx;
23+
}
24+
25+
private int binarySearch(BinaryMatrix binaryMatrix, int row, int start, int end) {
26+
int minIdx = Integer.MAX_VALUE;
27+
while (start <= end) {
28+
int mid = (start + end) / 2;
29+
if (binaryMatrix.get(row, mid) == 1) {
30+
minIdx = Math.min(minIdx, mid);
31+
end = mid - 1;
32+
}
33+
else {
34+
start = mid + 1;
35+
}
36+
}
37+
return minIdx == Integer.MAX_VALUE ? -1 : minIdx;
38+
}
39+
}

0 commit comments

Comments
 (0)