日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

剑指offer之11-15题解

發布時間:2024/2/28 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 剑指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個節點。

    (三)代碼實現

  • 思路一代碼實現
  • /* public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;} }*/ 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;} }
  • 思路二代碼實現
  • /* public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;} }*/ 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 ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;} }*/ 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题解的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。