Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2ea2dbe

Browse files
committed
feat: add leetcode question #151
1 parent 4fbb8b9 commit 2ea2dbe

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.hi.dhl.algorithms.leetcode._151.java;
2+
3+
/**
4+
* <pre>
5+
* author: dhl
6+
* date : 2022年10月27日
7+
* desc :
8+
* </pre>
9+
*/
10+
11+
class Solution {
12+
public String reverseWords(String s) {
13+
char[] chars = s.toCharArray();
14+
int j = chars.length - 1;
15+
int i = j;
16+
StringBuilder builder = new StringBuilder();
17+
while (i >= 0) {
18+
while (i >= 0 && chars[i] == ' ') {
19+
i--;
20+
}
21+
j = i;
22+
while (i >= 0 && chars[i] != ' ') {
23+
i--;
24+
}
25+
builder.append(s.substring(i + 1, j + 1));
26+
while (i >= 0 && chars[i] == ' ') {
27+
i--;
28+
}
29+
if (i >= 0) {
30+
builder.append(" ");
31+
}
32+
}
33+
34+
return builder.toString();
35+
}
36+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.hi.dhl.algorithms.leetcode._34.java;
2+
3+
/**
4+
* <pre>
5+
* author: dhl
6+
* date : 2022年10月25日
7+
* desc :
8+
* </pre>
9+
*/
10+
11+
class Solution {
12+
public int[] searchRange(int[] nums, int target) {
13+
if (nums == null) {
14+
return new int[]{-1, -1};
15+
}
16+
int lo = 0;
17+
int hi = nums.length - 1;
18+
while (lo <= hi) {
19+
int mid = (lo + hi) >>> 1;
20+
if (nums[mid] <= target) {
21+
lo = mid + 1;
22+
} else {
23+
hi = mid - 1;
24+
}
25+
}
26+
if (hi >= 0 && nums[hi] != target) {
27+
return new int[]{-1, -1};
28+
}
29+
30+
int right = lo - 1;
31+
lo = 0;
32+
hi = nums.length - 1;
33+
while (lo <= hi) {
34+
int mid = (lo + hi) >>> 1;
35+
if (nums[mid] >= target) {
36+
hi = mid - 1;
37+
} else {
38+
lo = mid + 1;
39+
}
40+
}
41+
int left = hi + 1;
42+
if (left <= right) {
43+
return new int[]{left, right};
44+
} else {
45+
return new int[]{-1, -1};
46+
}
47+
}
48+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /