您的位置:首页 > Web前端

剑指Offer学习笔记(2)

2016-03-21 16:26 204 查看
一、代码的完整性

1. 数值的整数次方
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public
class
Solution
{

public
double
Power(
double
base,
int
exponent)
{

if
(exponent==
0
){

return
1
;

}
else
if
(exponent>
0
){

double
result=
1
;

for
(
int
i=
1
;i<=exponent;i++){

result*=base;

}

return
result;

}
else
{

double
result=
1.0
;

exponent=-exponent;

for
(
int
i=
1
;i<=exponent;i++){

result*=(
double
)base;

}


return
((
double
)
1.0
)/((
double
)result);

}

//return
exponent;


}

}


2. 调整数组顺序使奇数位于偶数前面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public
class
Solution
{

public
void
reOrderArray(
int
[]
array) {


int
indexEven
= -
1
;


for
(
int
i
=
0
;
i <array.length;i++) {

if
(array[i]
%
2
==
1
)
{

if
(indexEven
<i && indexEven != -
1
)
{

int
temp
= array[i];

for
(
int
j
= i;j >indexEven;j--) {

array[j]
= array[j-
1
];

}

array[indexEven]
= temp;

indexEven
+=
1
;

}


}
else
{

if
(indexEven
==-
1
)
{

indexEven
= i;

}

}


}


}

}


3. 反转链表

输入一个链表,反转链表后,输出链表的所有元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*

public class ListNode {

int
val;

ListNode
next = null;


ListNode(int
val) {

this.val
= val;

}

}*/

public
class
Solution
{

public
ListNode
ReverseList(ListNode head) {

ListNode
cur=head;

ListNode
next=
null
;

ListNode
pre=
null
;

if
(head==
null
||head.next==
null
){

return
head;

}

while
(cur!=
null
){

next=cur.next;

cur.next=pre;


pre=cur;

cur=next;

}

return
pre;

}

}


4. 链表中倒数第k个结点
代码思路如下:两个指针,先让第一个指针和第二个指针都指向头结点,然后再让第一个指正走(k-1)步,到达第k个节点。然后两个指针同时往后移动,当第一个结点到达末尾的时候,第二个结点所在位置就是倒数第k个节点了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*

public class ListNode {

int
val;

ListNode
next = null;


ListNode(int
val) {

this.val
= val;

}

}*/

public
class
Solution
{

public
ListNode
FindKthToTail(ListNode head,
int
k)
{


ListNode
first = head;

ListNode
second = head;


if
(head
==
null
||k<=
0
)
{

return
null
;

}

for
(
int
i
=
1
;
i <k;i++) {

if
(second.next
!=
null
)
{

second
= second.next;

}
else
{

return
null
;

}

}

while
(second.next!=
null
){

second=second.next;

first=first.next;

}




return
first;

}


}


5. 打印1到最大的n位数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public
class
Solution
{

/**

 
*
递归调用的思路

 
*
@param n

 
*/

public
static
void
Print1ToMaxOfNDigits(
int
n)
{

if
(n<=
0
){

return
;

}


char
[]
num =
new
char
;

//num
='\0';


for
(
int
i=
0
;i<
10
;i++){

num[
0
]=(
char
)(i+
'0'
);
//字符转化成数字

Print1ToMaxOfNDigitsRecursive(num,n,
0
);

}



}


private
static
void
Print1ToMaxOfNDigitsRecursive(
char
[]
num,
int
length,
int
index)
{

if
(index==length-
1
){
//如果组合完毕

//System.out.print(num);

printNum(num);

return
;

}


for
(
int
i=
0
;i<
10
;i++){

num[index+
1
]=(
char
)
((
char
)
i+
'0'
);

Print1ToMaxOfNDigitsRecursive(num,
length,index+
1
);

}

}


private
static
void
printNum(
char
[]
num) {

boolean
isBegin0
=
true
;

int
len=num.length;

for
(
int
i=
0
;i<len;i++){

if
(isBegin0&&num[i]!=
'0'
){

isBegin0=
false
;

}


if
(!isBegin0){

System.out.print(num[i]);

}


}

System.out.print(
'
'
);


}


二、代码的完整性

1. 链表中倒数第k个结点
输入一个链表,输出该链表中倒数第k个结点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*

public class ListNode {

int
val;

ListNode
next = null;


ListNode(int
val) {

this.val
= val;

}

}*/

public
class
Solution
{

public
ListNode
FindKthToTail(ListNode head,
int
k)
{


ListNode
first = head;

ListNode
second = head;


if
(head
==
null
||k<=
0
)
{

return
null
;

}

for
(
int
i
=
1
;
i <k;i++) {

if
(second.next
!=
null
)
{

second
= second.next;

}
else
{

return
null
;

}

}

while
(second.next!=
null
){

second=second.next;

first=first.next;

}




return
first;

}


}


相关题目:找中间结点可以两个指针,一个一次走一个 一个一次走两个,快的首先到结尾。
判断是否是环形链表,快的追上慢的。

2.
反转链表
输入一个链表,反转链表后,输出链表的所有元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*

public class ListNode {

int
val;

ListNode
next = null;


ListNode(int
val) {

this.val
= val;

}

}*/

public
class
Solution
{

public
ListNode
ReverseList(ListNode head) {

ListNode
temp=
null
;

ListNode
orgin=
null
;

if
(head==
null
){

return
head;

}

temp=orgin=head;

head=head.next;

orgin.next=
null
;

while
(head!=
null
){

temp=head;

head=head.next;

temp.next=orgin;

orgin=temp;

}


return
temp;

}

}


3.
反转链表

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*

public class ListNode {

int
val;

ListNode
next = null;


ListNode(int
val) {

this.val
= val;

}

}*/

public
class
Solution
{

public
ListNode
Merge(ListNode list1,ListNode list2) {

if
(list1==
null
){

return
list2;

}

if
(list2==
null
){

return
list1;

}

ListNode
mergeList =
null
;

if
(list1.val<list2.val){

mergeList=list1;

mergeList.next=Merge(list1.next,list2);

}
else
{

mergeList=list2;

mergeList.next=Merge(list1,list2.next);

}

return
mergeList;

}

}


4.
树的子结构
输入两颗二叉树A,B,判断B是不是A的子结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**

public class TreeNode {

int
val = 0;

TreeNode
left = null;

TreeNode
right = null;


public
TreeNode(int val) {

this.val
= val;


}


}

*/

public
class
Solution
{

public
boolean
HasSubtree(TreeNode
root1, TreeNode root2) {

boolean
flag=
false
;

if
(root1!=
null
&&root2!=
null
){

if
(root1.val==root2.val){

flag=doesTree1HaveTree2(root1,root2);

}

if
(!flag){

flag=HasSubtree(root1.left,root2);

}

if
(!flag){

flag=HasSubtree(root1.right,root2);

}

}

return
flag;

}


private
boolean
doesTree1HaveTree2(TreeNode
root1, TreeNode root2) {

if
(root2==
null
){

return
true
;

}


if
(root1==
null
){

return
false
;

}


if
(root1.val!=root2.val){

return
false
;

}

if
(root1.val==root2.val){

return
doesTree1HaveTree2(root1.left,
root2.left)&&doesTree1HaveTree2(root1.right, root2.right);

}

return
false
;

}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: