Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
李楚霏
/
leetCode_Solution_Record
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
9789e8f5
authored
Aug 27, 2020
by
李楚霏
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
每日刷题
parent
5c9ccb7e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
88 additions
and
0 deletions
+88
-0
70works/day2_plusOne.cpp
+38
-0
70works/day3_twoSum.js
+17
-0
70works/day4_ Swap Nodes in Pairs.cpp
+33
-0
No files found.
70works/day2_plusOne.cpp
0 → 100644
View file @
9789e8f5
/**
* 一次哈希遍历
* 解题思路:数值作为map的键,下标作为map的值;
* 每次遍历时,如果target-nums[i]在map中能找到值,
* 说明target的两个数已经找到,就返回
*/
#include <iostream>
#include<vector>
#include<map>
using
namespace
std
;
class
Solution
{
public
:
vector
<
int
>
twoSum
(
vector
<
int
>&
nums
,
int
target
)
{
vector
<
int
>
res
;
map
<
int
,
int
>
hashmap
;
for
(
int
i
=
0
;
i
<
nums
.
size
();
i
++
)
{
if
(
hashmap
[
target
-
nums
[
i
]]
&&
hashmap
[
target
-
nums
[
i
]]
!=
i
+
1
)
{
res
.
push_back
(
i
);
res
.
push_back
(
hashmap
[
target
-
nums
[
i
]]
-
1
);
return
res
;
}
//不让hash的下标为负
hashmap
[
nums
[
i
]]
=
i
+
1
;
}
return
res
;
}
};
int
main
()
{
Solution
s
;
vector
<
int
>
test
=
{
-
2
,
11
,
4
,
15
,
7
,
5
};
for
(
int
i
=
0
;
i
<
s
.
twoSum
(
test
,
9
).
size
();
i
++
)
{
cout
<<
s
.
twoSum
(
test
,
9
)[
i
]
<<
endl
;
}
return
0
;
}
\ No newline at end of file
70works/day3_twoSum.js
0 → 100644
View file @
9789e8f5
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var
twoSum
=
function
(
nums
,
target
)
{
let
hashMap
=
{};
for
(
let
i
=
0
;
i
<
nums
.
length
;
i
++
)
{
let
n
=
target
-
nums
[
i
];
let
find
=
hashMap
[
n
];
if
(
find
!=
undefined
)
return
hashMap
[
find
,
i
];
else
hashMap
[
nums
[
i
]]
=
i
;
}
};
let
nums
=
[
2
,
7
,
11
,
15
];
let
target
=
9
;
twoSum
(
nums
,
target
);
70works/day4_ Swap Nodes in Pairs.cpp
0 → 100644
View file @
9789e8f5
#include<iostream>
using
namespace
std
;
struct
ListNode
{
int
val
;
ListNode
*
next
;
ListNode
(
int
x
)
:
val
(
x
),
next
(
NULL
)
{}
};
class
Solution
{
public
:
ListNode
*
swapPairs
(
ListNode
*
head
)
{
if
(
head
==
nullptr
||
head
->
next
==
nullptr
)
return
head
;
ListNode
*
next
=
NULL
;
// 错误原因:递归不需要什么循环,因为递归本身就是循环
// while(head!= NULL && head->next != NULL) {
next
=
head
->
next
;
head
->
next
=
swapPairs
(
next
->
next
);
next
->
next
=
head
;
// }
return
next
;
}
};
int
main
()
{
ListNode
*
head
=
new
ListNode
(
1
);
ListNode
*
head1
=
new
ListNode
(
2
);
ListNode
*
head2
=
new
ListNode
(
3
);
ListNode
*
head3
=
new
ListNode
(
4
);
head
->
next
=
head1
;
head1
->
next
=
head2
;
head2
->
next
=
head3
;
Solution
s
;
s
.
swapPairs
(
head
);
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment