Movatterモバイル変換


[0]ホーム

URL:


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

Commitd8a5e91

Browse files
shortest word distance
1 parente089949 commitd8a5e91

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
packageeasy;
2+
3+
publicclassShortestWordDistance {
4+
5+
publicintshortestDistance(String[]words,Stringword1,Stringword2) {
6+
7+
intp = -1,q = -1,min =Integer.MAX_VALUE;
8+
for(inti =0;i <words.length;i++){
9+
if(words[i].equals(word1))p =i;
10+
if(words[i].equals(word2))q =i;
11+
if(p != -1 &&q != -1)min =Math.min(min,Math.abs(p-q));
12+
}
13+
returnmin;
14+
15+
}
16+
17+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)|[Solution](../../blob/master/EASY/src/easy/BinaryTreePaths.java) | O(n*h) | O(h) | DFS/Recursion
6262
|252|[Meeting Rooms](https://leetcode.com/problems/meeting-rooms/)|[Solution](../../blob/master/EASY/src/easy/MeetingRooms.java)| O(nlogn)| O(1)|
6363
|246|[Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/)|[Solution](../../blob/master/EASY/src/easy/StrobogrammaticNumber.java)| O(n)| O(1)|
64+
|243|[Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/)|[Solution](../../blob/master/EASY/src/easy/ShortestWordDistance.java)| O(n)| O(1)|
6465
|223|[Rectangle Area](https://leetcode.com/problems/rectangle-area/)|[Solution](../../blob/master/EASY/src/easy/RectangleArea.java)| O(1)|O(1)| Easy|
6566
|219|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)|[Solution](../../blob/master/EASY/src/easy/ContainsDuplicateII.java)| O(n)|O(n) | Easy| HashMap
6667
|209|[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)|[Solution](../../blob/master/MEDIUM/src/medium/MinimumSizeSubarraySum.java)| O(n)|O(1)| Medium|

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp