生活随笔
收集整理的這篇文章主要介紹了
剑指offer之11-15题解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
劍指offer之11-15題解
目錄
二進制中1的個數數值的整數次方調整數組順序使奇數位于偶數前面鏈表中第k個節點反轉鏈表
11. 二進制中1的個數
(一)題目描述
(二)思路
n & n-1,該位運算去除n的位級表示中最低的那一位。
調用Integer.bitCount()方法:返回二進制補碼中1位的個數
(三)代碼實現
代碼實現
public class Solution {public int NumberOf1(int n
) {int count
= 0;while (n
!= 0) {count
++;n
&= (n
- 1);}return count
;}}
調用 Integer.bitCount()方法實現
public class Solution {public int NumberOf1(int n) {return Integer.bitCount(n);}
}
12. 數值的整數次方
(一)題目描述
(二)思路
直接用Math.pow()方法實現。使用遞歸方法實現
(三)代碼實現
Math.pow()方法實現
public class Solution {public double Power(double base
, int exponent
) {return Math
.pow(base
,exponent
);}
}
遞歸實現
public class Solution {public double Power(double base
, int exponent
) {if (exponent
== 0) {return 1;}if (exponent
== 1) {return base
;}boolean isNegative
= false;if (exponent
< 0) {exponent
= -exponent
;isNegative
= true;}double pow
= Power(base
* base
, exponent
/ 2);if (exponent
% 2 != 0) {pow
= pow
* base
;}return isNegative
? 1 / pow
: pow
;}
}
13. 調整數組順序使奇數位于偶數前面
(一)題目描述
(二)思路
關鍵思路都是找出奇數和偶數,我的思路是創建兩個隊列,一個進奇數,一個進偶數。最后依次加入數組即可。
大佬思路
先找到奇數個數復制數組,遍歷到偶數,奇數修改原數組即可。
(三)代碼實現
隊列實現代碼
import java
.util
.LinkedList
;
import java
.util
.Queue
;
public class Solution {public void reOrderArray1(int[] array
) {if (array
== null
) {return;}Queue queue1
= new LinkedList();Queue queue2
= new LinkedList();for (int i
= 0; i
< array
.length
; i
++) {if (array
[i
] % 2 == 1) {queue1
.offer(array
[i
]);} else {queue2
.offer(array
[i
]);}}int n
= 0;while (!queue1
.isEmpty()) {array
[n
++] = (int) queue1
.poll();}while (!queue2
.isEmpty()) {array
[n
++] = (int) queue2
.poll();}}
}
大佬代碼
import java
.util
.Arrays
;
public class Solution {public void reOrderArray(int[] array
) {int count
= 0;for (int x
: array
){if (x
%2!=0){count
++;}}int[] clone
= array
.clone();int i
= 0;int j
= count
;for (int num
: clone
){if (num
%2!=0){array
[i
++] = num
;}else {array
[j
++] = num
;}}}
}
14. 鏈表中第k個節點
(一)題目描述
(二)思路
- 思路一:設鏈表長度為N。設置兩個指針P1和P2,先讓P1移動K個節點,則還有N-K個節點可以移動。此時讓P1和P2同時移動,可以知道當P1移動到鏈表結尾時,P2移動到N-K個節點,該位置就是倒數第K個節點。
- 思路二:創建一個棧,將節點全部放進去,再彈出K個節點,第K個就是倒數第K個節點。
(三)代碼實現
思路一代碼實現
import java
.util
.Stack
;
public class Solution {public ListNode
FindKthToTail(ListNode head
, int k
) {if (head
== null
)return null
;ListNode p1
= head
;while (p1
!= null
&& k
-- > 0)p1
= p1
.next
;if (k
> 0)return null
;ListNode p2
= head
;while (p1
!= null
) {p1
= p1
.next
;p2
= p2
.next
;}return p2
;}
}
思路二代碼實現
import java
.util
.Stack
;
public class Solution {public ListNode
FindKthToTail(ListNode head
,int k
) {if (head
==null
||k
<0){return null
;}Stack
<ListNode> stack
= new Stack<>();while (head
!=null
){stack
.push(head
);head
= head
.next
;}if (stack
.size()<k
){return null
;}for (int i
= 0; i
< k
; i
++) {head
= stack
.pop();}return head
;}
}
15. 反轉鏈表
(一)題目描述
(二)思路
遞歸
迭代:頭插法
(三)代碼實現
遞歸代碼實現
public class Solution {public ListNode
ReverseList(ListNode head
) {if(head
==null
||head
.next
==null
){return head
;}ListNode next
= head
.next
;head
.next
= null
;ListNode newHead
= ReverseList(next
);next
.next
= head
;return newHead
;}
}
迭代代碼實現
public ListNode
ReverseList(ListNode head
) {ListNode newList
= new ListNode(-1);while (head
!=null
){ListNode next
= head
.next
;head
.next
= newList
.next
;newList
.next
= head
;head
= next
;}return newList
.next
;}
總結
以上是生活随笔為你收集整理的剑指offer之11-15题解的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。