Skip to content

Commit ed26ddb

Browse files
committed
Merge branch 'master' of github.com:doocs/leetcode
2 parents eabf72a + d17467a commit ed26ddb

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

solution/001.Two Sum/Solution.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const twoSum = function(nums, target) {
2+
const map = {};
3+
4+
for (let i = 0; i < nums.length; i++) {
5+
if (map[nums[i]] !== undefined) {
6+
return [map[nums[i]], i]
7+
} else {
8+
map[target - nums[i]] = i
9+
}
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
4+
int nums[10000] = { 0 };
5+
int index = 0;
6+
vector<int>::iterator it1 = nums1.begin();
7+
vector<int>::iterator it2 = nums2.begin();
8+
for (; it1 != nums1.end() && it2 != nums2.end();) {
9+
if (*it1 >= *it2) {
10+
nums[index++] = *it2;
11+
it2++;
12+
}
13+
else {
14+
nums[index++] = *it1;
15+
it1++;
16+
}
17+
}
18+
19+
while (it1 != nums1.end()) {
20+
nums[index++] = *it1;
21+
it1++;
22+
}
23+
while (it2 != nums2.end()) {
24+
nums[index++] = *it2;
25+
it2++;
26+
}
27+
28+
if (index % 2 == 0) {
29+
return (double)((nums[index/2] + nums[index/2 - 1])/2.0);
30+
}
31+
else {
32+
return (double)(nums[index/2]);
33+
}
34+
35+
}
36+
};
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const reverse1 = function(x){
2+
let s = String(x);
3+
let isNegative = false;
4+
if(s[0] === '-'){
5+
isNegative = true;
6+
}
7+
s = parseInt(s.split('').reverse().join(''));
8+
return isNegative ? (s > Math.pow(2,31) ? 0 : -s) : (s > Math.pow(2,31) - 1 ? 0 : s);
9+
}
10+
11+
const reverse = function(x){
12+
let result = parseInt(x.toString().split('').reverse().join(''));
13+
if(result > Math.pow(2,31) - 1 || -result < Math.pow(-2,31)) return 0;
14+
return x > 0 ? result: -result;
15+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution(object):
2+
def romanToInt(self, s):
3+
dict = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1} #对应关系存在一个字典里
4+
sum = 0
5+
for i in range(len(s)-1):
6+
if dict[s[i]] < dict[s[i+1]]: #这里小于是因为只会有一个减的存在,换句话说如果两个一样的出现,应该是加法
7+
sum -= dict[s[i]]
8+
else:
9+
sum += dict[s[i]]
10+
return sum+dict[s[-1]] #最后一位一定是加
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const longestCommonPrefix = function(strs){
2+
if(strs.length === 0) return '';
3+
for(let j = 0; j < strs[0].length; j++){
4+
for(let i = 0; i < strs.length; i++){
5+
if(strs[0][j] !== strs[i][j]){
6+
return strs[0].substring(0,j);
7+
}
8+
}
9+
}
10+
return strs[0];
11+
}

0 commit comments

Comments
 (0)