Re: [閒聊] 每日LeetCode

看板Marginalman作者 (みけねこ的鼻屎)時間7月前 (2023/10/31 14:23), 7月前編輯推噓1(103)
留言4則, 2人參與, 7月前最新討論串473/719 (看更多)
https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/description 1356. Sort Integers by The Number of 1 Bits 給你一堆數字,依照二進位制的1的數量升序排序,如果數量一樣就比較數字本身。 1.寫一個快速排序和一個compare函數 Java Code: ---------------------------------------------------- class Solution { public int[] sortByBits(int[] arr) { quickSort(arr,0,arr.length - 1); return arr; } void quickSort(int[] arr, int start, int end){ if (start >= end) return; int index = partition(arr, start, end); quickSort(arr, start, index - 1); quickSort(arr, index + 1, end); } int partition(int[] arr, int start, int end) { int pivot = arr[start]; int index = start; for (int i = start + 1; i <= end; i++) { if (compare(pivot, arr[i])) { swap(arr, ++index, i); } } swap(arr, start, index); return index; } void swap(int[] arr, int n1, int n2){ int tmp = arr[n1]; arr[n1] = arr[n2]; arr[n2] = tmp; } boolean compare(int n1, int n2){ int cmp = Integer.bitCount(n1) - Integer.bitCount(n2); return cmp == 0 ? n1 - n2 > 0 : cmp > 0; } } ---------------------------------------------------- 這是昨天的每日一題 ※ 引述《surimodo (搖滾少女!! 活力棉花糖!!)》之銘言: : ※ 引述 《oin1104 (是oin的說)》 之銘言: : : → oin1104: 目前剛開始碰指標 10/31 : 別玩指標了 : 看看 Python 看看 nodejs : 不用碰指標 多香阿 : 退一步 Java 集成物件 : 操作起來也很方便 : 2023還在用 C 刷題 : 除了學校考試我想不到其他理由= = 你說的對 但 Java的sort沒辦法自定義排序在primitive type 寫個排序要自己造輪子 不然就要用執行效率超爛的boxed 白癡語言 操 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.73.13 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1698733395.A.454.html ※ 編輯: Rushia (122.100.73.13 臺灣), 10/31/2023 14:24:39

10/31 14:25, 7月前 , 1F
剛剛在上課
10/31 14:25, 1F

10/31 14:26, 7月前 , 2F
我們現在在教c 所以想練習c 姆咪
10/31 14:26, 2F

10/31 14:26, 7月前 , 3F
不然其他的 我只會一點點python c#而已捏
10/31 14:26, 3F

10/31 14:27, 7月前 , 4F
你要手寫 compare 對ㄚ
10/31 14:27, 4F
文章代碼(AID): #1bG9rJHK (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1bG9rJHK (Marginalman)