Skip to content

Commit f92b6b0

Browse files
committed
new constructure
1 parent 55dce02 commit f92b6b0

File tree

67 files changed

+3184
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3184
-0
lines changed

README.md

+701
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
for (int i = 0; i < nums.length; i++) {
5+
map.put(nums[i], i);
6+
}
7+
for (int i = 0; i < nums.length; i++) {
8+
int complement = target - nums[i];
9+
if (map.get(complement) != null && map.get(complement) != i) {
10+
return new int[] { i, map.get(complement) };
11+
}
12+
}
13+
14+
throw new IllegalArgumentException("No two sum solution");
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
unordered_map<int, int> m;
5+
vector<int> result;
6+
for(int i=0; i< nums.size(); i++){
7+
// not found the second one
8+
if (m.find(nums[i]) == m.end()) {
9+
// store the first one position into the second one's key
10+
m[target - nums[i]] = i;
11+
}else {
12+
// found the second one
13+
result.push_back(m[nums[i]]);
14+
result.push_back(i);
15+
break;
16+
}
17+
}
18+
return result;
19+
}
20+
};
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
this function is too slow. We need to have another solution.
3+
*/
4+
5+
function twoSum(nums, target){
6+
var tmp,
7+
position,
8+
a = [];
9+
for(var i = 0; i < nums.length; i++){
10+
tmp = target - nums[i];
11+
position = nums.indexOf(tmp);
12+
if(position !== i && position !== -1){
13+
a.push(i, position);
14+
break;
15+
}
16+
}
17+
return a;
18+
}
19+
20+
// method 2
21+
//这里还是利用了hash的思想,我利用python做这道题也是这样做的。
22+
var twoSum = function(nums, target){
23+
var hash = {},
24+
result = [];
25+
for(var i = 0; i < nums.length; i++){
26+
hash[nums[i]] = i;
27+
}
28+
for(var j = 0; j< nums.length; j++){
29+
var a = target - nums[j];
30+
if(hash[a]){
31+
result.push(j, hash[a]);
32+
break;
33+
}
34+
}
35+
return result;
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
# Created by Bruce yuan on 18-1-23.
3+
4+
5+
class Solution(object):
6+
def twoSum(self, nums, target):
7+
i = 0
8+
q = 0
9+
for i in range(0, len(nums)):
10+
m = target - nums[i]
11+
if m in nums:
12+
q = nums.index(m)
13+
if q != i:
14+
break
15+
return [i, q]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//this is dum
2+
var addTwoNumbers = function(l1, l2) {
3+
var add = 0,
4+
ans,
5+
head;
6+
7+
while(l1 || l2){
8+
var a = l1 ? l1.val : 0,
9+
b = l2 ? l2.val : 0;
10+
11+
var sum = a + b + add;
12+
add = ~~(sum / 10);//这里是去除小数地作用。
13+
//类似于Math.floor(sum / 10);
14+
var node = new ListNode(sum % 10);
15+
if(!ans){
16+
ans = head = node;
17+
} else {
18+
head.next = node;
19+
head = node;
20+
}
21+
22+
if (l1){
23+
l1 = l1.next;
24+
}
25+
if (l2){
26+
l2 = l2.next;
27+
}
28+
29+
}
30+
31+
if(add){
32+
var node1 = new ListNode(add);
33+
head.next = node1;
34+
head = node1;
35+
}
36+
return ans;
37+
38+
39+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function findMedian(nums1, nums2){
2+
var result = [],
3+
il = 0,
4+
ir = 0;
5+
while(il < nums1.length && ir < nums2.length){
6+
if(nums1[il] < nums2[ir]){
7+
result.push(nums1[il]);
8+
il++;
9+
} else {
10+
result.push(nums2[ir]);
11+
ir++;
12+
}
13+
}
14+
15+
while(il < nums1.length){
16+
result.push(nums1[il]);
17+
il++;
18+
}
19+
while(ir < nums2.length){
20+
result.push(nums2[ir]);
21+
ir++;
22+
}
23+
24+
if(result.length % 2 === 0){
25+
var i = (result.length / 2 - 1),
26+
j = (result.length / 2);
27+
console.log(i, j);
28+
29+
return (result[i] + result[j]) / 2;
30+
31+
} else {
32+
return result[Math.floor(result.length / 2)];
33+
}
34+
35+
}
36+
37+
var a = findMedian([1,2,5], [3,4]);
38+
console.log(a);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number} x
3+
* @return {number}
4+
*/
5+
6+
//这里有一个好弱智的地方啊,其实不能用Number.MAX_VALUE来表示是否超过这了这个数的极限数字。
7+
//还有就是需要知道js的除法是不会自动取整啊。而且最后不要用Math.floor()这个方法,貌似有点慢啊
8+
var reverse = function(x) {
9+
var MAX = (1 << 30) * 2 - 1;
10+
var sum = 0;
11+
while(x !== 0){
12+
sum = sum * 10 + x % 10;
13+
x = ~~(x / 10);
14+
}
15+
16+
return (Math.abs(sum) > MAX) ? 0 : sum;
17+
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
6+
/*设置两个指针i, j, 一头一尾, 相向而行. 假设i指向的挡板较低, j指向的挡板较高(height[i] < height[j]).
7+
下一步移动哪个指针?
8+
-- 若移动j, 无论height[j-1]是何种高度, 形成的面积都小于之前的面积.
9+
-- 若移动i, 若height[i+1] <= height[i], 面积一定缩小; 但若height[i+1] > height[i], 面积则有可能增大.
10+
综上, 应该移动指向较低挡板的那个指针.
11+
*/
12+
var maxArea = function(height) {
13+
var max = 0,
14+
low = 0,
15+
k,
16+
high = height.length - 1;
17+
18+
while(low < high){
19+
max = Math.max(max, (high - low) * Math.min(height[low], height[high]));
20+
if(height[low] < height[high]){
21+
//move low
22+
k = low + 1;
23+
if(k < high && height[k] <= height[low]){
24+
k++;
25+
}
26+
low = k;
27+
28+
} else{
29+
//move high
30+
k = high - 1;
31+
if(k > low && height[k] <= height[high]){
32+
k--;
33+
}
34+
high = k;
35+
}
36+
}
37+
38+
return max;
39+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var romanToInt = function(s) {
6+
let roman = {'M': 1000,'D': 500 ,'C': 100,'L': 50,'X': 10,'V': 5,'I': 1};
7+
let z = 0;
8+
for (let i = 0; i < s.length - 1; i++) {
9+
if (roman[s[i]] < roman[s[i + 1]]) {
10+
z -= roman[s[i]];
11+
} else {
12+
z += roman[s[i]];
13+
}
14+
}
15+
16+
return z + roman[s[s.length - 1]];
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string}
4+
*/
5+
var longestCommonPrefix = function(strs) {
6+
let pre = strs[0];
7+
let i = 1;
8+
9+
while (i < strs.length) {
10+
while (!strs[i].startsWith(pre)) {
11+
pre = strs[0].substring(0, pre.length - 1);
12+
}
13+
14+
i++;
15+
}
16+
17+
return pre === undefined ? '' : pre;
18+
};

leetcode-algorithms/015. 3Sum/3Sum.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
6+
//方法一。
7+
var threeSum = function(nums) {
8+
var hash = {};
9+
10+
nums.sort(function(a, b){
11+
return a - b;
12+
});
13+
14+
nums.forEach(function(item){
15+
if(hash[item]){
16+
hash[item]++;//有重复而且要知道每个出现了多少次,因为可以一个数字出现两次。甚至三次。
17+
}else{
18+
hash[item] = 1;
19+
}
20+
});
21+
22+
var result = [],
23+
hashSet = {};
24+
25+
for(var i = 0; i < nums.length; i++){
26+
for(var j = i + 1; j < nums.length; j++){
27+
var a = nums[i],
28+
b = nums[j],
29+
c = 0 - a -b;
30+
31+
if(c < b){
32+
break;
33+
}
34+
35+
if(hashSet[a + ',' + b + ',' + c]){
36+
continue;
37+
}
38+
hash[a]--;
39+
hash[b]--;//如果出现多次表示还可以继续用这个数字,否则不能用。
40+
41+
if(hash[c]){
42+
hashSet[a + ',' + b + ',' + c] = true;
43+
result.push([a, b, c]);
44+
}
45+
46+
hash[a]++;
47+
hash[b]++;
48+
}
49+
}
50+
51+
return result;
52+
53+
};
54+
55+
console.log(threeSum([1,-1,-1,-1,-1,2,2,2,0,0,0]));

leetcode-algorithms/015. 3Sum/3sum.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#coding: utf-8
2+
3+
"""
4+
想法都是一样的,不想写javascript版本了。
5+
"""
6+
7+
class Solution(object):
8+
'''算法思路:
9+
同上,不过这次却巧妙去重
10+
'''
11+
def threeSum(self, nums):
12+
nums, n, r = sorted(nums), len(nums), []
13+
for i, target in enumerate(nums):
14+
if i and nums[i - 1] == target:
15+
continue
16+
17+
j, k = i + 1, n - 1
18+
while j < k:
19+
sum = nums[j] + nums[k]
20+
if sum > -target:
21+
k -= 1
22+
elif sum < -target:
23+
j += 1
24+
else:
25+
r.append([target, nums[j], nums[k]])
26+
while j < k and nums[j + 1] == nums[j]:
27+
j += 1
28+
while j < k and nums[k - 1] == nums[k]:
29+
k -= 1
30+
j += 1
31+
return r

0 commit comments

Comments
 (0)