Skip to content

Commit 9f40957

Browse files
committed
Merge branch 'master' of github.com:doocs/leetcode
2 parents 77abb79 + b2d3d9e commit 9f40957

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

.gitignore

Whitespace-only changes.

solution/001.Two Sum/Solution.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# @param {Integer[]} nums
2+
# @param {Integer} target
3+
# @return {Integer[]}
4+
def two_sum(nums, target)
5+
nums.each_with_index do |x, idx|
6+
if nums.include? target - x
7+
return [idx, nums.index(target - x)] if nums.index(target - x) != idx
8+
end
9+
next
10+
end
11+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Definition for singly-linked list.
2+
# class ListNode
3+
# attr_accessor :val, :next
4+
# def initialize(val)
5+
# @val = val
6+
# @next = nil
7+
# end
8+
# end
9+
10+
# @param {ListNode} l1
11+
# @param {ListNode} l2
12+
# @return {ListNode}
13+
def add_two_numbers(l1, l2)
14+
return l2 if l1 == nil
15+
return l1 if l2 == nil
16+
cur_val = l1.val + l2.val
17+
l3 = ListNode.new(cur_val % 10)
18+
add = cur_val >= 10 ? 1 : 0
19+
tmp = l3
20+
21+
l1 = l1.next
22+
l2 = l2.next
23+
while !l1.nil? && !l2.nil?
24+
cur_val = l1.val + l2.val + add
25+
tmp.next = ListNode.new(cur_val % 10)
26+
tmp = tmp.next
27+
add = cur_val >= 10 ? 1 : 0
28+
29+
l1 = l1.next
30+
l2 = l2.next
31+
end
32+
33+
until l1.nil?
34+
cur_val = l1.val + add
35+
tmp.next = ListNode.new(cur_val % 10)
36+
tmp = tmp.next
37+
add = cur_val >= 10 ? 1 : 0
38+
l1 = l1.next
39+
end
40+
41+
until l2.nil?
42+
cur_val = l2.val + add
43+
tmp.next = ListNode.new(cur_val % 10)
44+
tmp = tmp.next
45+
add = l2.val + add >= 10 ? 1 : 0
46+
l2 = l2.next
47+
end
48+
49+
tmp.next = ListNode.new(1) if add == 1
50+
l3
51+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> permuteUnique(vector<int>& nums) {
4+
if (nums.size() <= 0) return{};
5+
6+
sort(nums.begin(), nums.end());
7+
8+
vector<vector<int>> res;
9+
vector<bool> used;
10+
vector<int> curr;
11+
12+
for (auto item : nums)
13+
{
14+
used.push_back(false);
15+
}
16+
17+
dfs(res, nums, curr, used, 0);
18+
return res;
19+
}
20+
21+
void dfs(vector<vector<int>>& res, vector<int>& nums, vector<int> curr, vector<bool> used, int depth)
22+
{
23+
if (depth >= nums.size())
24+
{
25+
res.emplace_back(curr);
26+
return;
27+
}
28+
29+
for (int i = 0; i < nums.size(); i++)
30+
{
31+
if (used[i]) continue;
32+
if (i > 0 && nums[i] == nums[i - 1] && !used[i-1]) continue;
33+
34+
used[i] = true;
35+
curr.emplace_back(nums[i]);
36+
dfs(res, nums, curr, used, depth + 1);
37+
curr.pop_back();
38+
used[i] = false;
39+
}
40+
}
41+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def partition(self, head, x):
9+
"""
10+
:type head: ListNode
11+
:type x: int
12+
:rtype: ListNode
13+
"""
14+
leftDummy=ListNode(-1)
15+
rightDummy=ListNode(-1)
16+
leftCur=leftDummy
17+
rightCur=rightDummy
18+
19+
while head:
20+
if head.val<x:
21+
leftCur.next=head
22+
leftCur=leftCur.next
23+
else:
24+
rightCur.next=head
25+
rightCur=rightCur.next
26+
head=head.next
27+
28+
leftCur.next=rightDummy.next
29+
rightCur.next=None
30+
return leftDummy.next

0 commit comments

Comments
 (0)