Skip to content

Commit 45d289e

Browse files
committed
too old to catch up with leetcode
1 parent 0999280 commit 45d289e

File tree

6 files changed

+73
-0
lines changed

6 files changed

+73
-0
lines changed

missing-number/README.md

Whitespace-only changes.

missing-number/Solution.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class Solution {
2+
3+
void swap(int[] nums, int i, int j){
4+
int t = nums[i];
5+
nums[i] = nums[j];
6+
nums[j] = t;
7+
}
8+
9+
void sort(int[] nums){
10+
11+
for(int i = 0; i < nums.length; i++){
12+
while(nums[i] != i){
13+
if(nums[i] < 0) break;
14+
15+
if(nums[i] >= nums.length) {
16+
nums[i] = -1;
17+
break; // drop
18+
}
19+
20+
swap(nums, nums[i], i);
21+
}
22+
}
23+
}
24+
25+
public int missingNumber(int[] nums) {
26+
sort(nums);
27+
28+
for(int i = 0; i < nums.length; i++){
29+
if(nums[i] != i){
30+
return i;
31+
}
32+
}
33+
34+
return nums.length;
35+
}
36+
}

missing-number/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
layout: solution
3+
title: Missing Number
4+
date: 2015-08-24 12:56:25+08:00
5+
leetcode_id: 268
6+
---
7+
{% include_relative README.md %}

shortest-word-distance/README.md

Whitespace-only changes.

shortest-word-distance/Solution.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public int shortestDistance(String[] words, String word1, String word2) {
3+
4+
int len = words.length;
5+
6+
int i1 = -1;
7+
int i2 = -1;
8+
9+
for(int i = 0; i < words.length; i++){
10+
if(word1.equals(words[i])){
11+
i1 = i;
12+
}else if(word2.equals(words[i])){
13+
i2 = i;
14+
}
15+
16+
if(i1 >= 0 && i2 >= 0){
17+
len = Math.min(len, Math.abs(i1 - i2));
18+
}
19+
}
20+
21+
return len;
22+
}
23+
}

shortest-word-distance/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
layout: solution
3+
title: Shortest Word Distance
4+
date: 2015-08-17 01:11:54+08:00
5+
leetcode_id: 243
6+
---
7+
{% include_relative README.md %}

0 commit comments

Comments
 (0)