随笔分类 - 排序
摘要:Ok 梳理一下思路,先按照攻击力进行排序,攻击力相同的按照防御力升序排序 if n[i].att == n[j].att { return n[i].def < n[j].def } return n[i].att < n[j].att [{1 27} {3 60} {9 26} {14 6} {1
阅读全文
摘要:同上一篇,使用堆排序的方式做。 第一步构建最小堆,构建好之后数组的第一个元素就是最小的; 第二步排序,开始执行k-1次sift,每次将剩余元素中最小的元素放到未排序元素的末尾 第三步,将数组的后k个数返回即为数组中最小的k个数。 func getLeastNumbers(arr []int, k i
阅读全文
摘要:首先BuildHeap,build heap是指让数组符合堆的性质,最大堆就是根节点的值>=子节点的值。 第一步build完之后,arr[0]就是最大的元素; 然后执行k-1次调整,就会把第k大的元素调整到arr[0]的位置。【要第一大的元素,就不需要再调整了,要第二大就再调整一次,以此类推...】
阅读全文
摘要:不知道为什么,现在某些公司喜欢考单链表快排,有什么意思呢?没意思。首先理解快排的思想,partition,递归。代码里有注释,挺详细的了,自己验证逻辑应该没什么问题。package leetcode.sort;import java.io.FileInputStream;import java.io...
阅读全文
摘要:package leetcode.sort;import java.util.Arrays;/** * QuickSort * Created by blank on 2015-09-22 下午12:06. */public class QuickSort { static int[] arr...
阅读全文
摘要:package leetcode.sort;import java.util.Arrays;import java.util.Random;/** * HeapSort * Created by blank on 15/8/12. */public class HeapSort { publi...
阅读全文
摘要:package leetcode.sort;import java.util.Arrays;import java.util.Random;/** * Created by blank on 2015-09-22 上午11:42. */public class MergeSort { publ...
阅读全文
摘要:Find thekth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For exampl...
阅读全文
摘要:Given an array of integers, find out whether there are two distinct indicesiandjin the array such that the difference betweennums[i]andnums[j]is at mo...
阅读全文
摘要:Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, wh...
阅读全文
摘要:Sort a linked list inO(nlogn) time using constant space complexity.链表排序,要求时间复杂度O(nlgn),我写的归并排序。/** * Definition for singly-linked list. * public class...
阅读全文
摘要:Sort a linked list using insertion sort.题目大意:将一个单链表使用插入排序的方式排序。解题思路:先新建一个头指针,然后重新构建一下这个单链表,每次从头找到第一个比当前元素大的,插在这个元素前面。/** * Definition for singly-linke...
阅读全文
摘要:Given a list of non negative integers, arrange them such that they form the largest number.For example, given[3, 30, 34, 5, 9], the largest formed num...
阅读全文
摘要:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).http://leetcode.com/onlinejudge#question_4复杂度没有达到要求的log(m+n) 而是O(m+n),就这还写了半天呢,妈蛋!class Solution{public: double findMedianSortedArrays...
阅读全文
摘要:题目描述:输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串。输入:测试数据有多组,输入字符串。输出:对于每组输入,输出处理后的结果。样例输入:bacd样例输出:abcd 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 int cmp(char x,char y) 6 { 7 return x<y; 8 } 9 int main()10 {11 char str[210];12 while (scan
阅读全文
摘要:题目描述: 有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相同则按照学生的年龄排序,并输出N个学生排序后的信息。输入: 测试数据有多组,每组输入第一行有一个整数N(N<=1000),接下来的N行包括N个学生的数据。 每个学生的数据包括姓名(长度不超过100的字符串)、年龄(整形数)、成绩(小于等于100的正数)。输出: 将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。 然后输出学生信息,按照如下格式: 姓名 年龄 成绩样例输入:3abc 20 99bcd 19 97bed 20 97样例输出:bcd 19 97bed 2
阅读全文
摘要:快速排序是由東尼·霍爾所發展的一種排序算法。在平均狀況下,排序 n 個項目要Ο(n log n)次比較。在最壞狀況下則需要Ο(n2)次比較,但這種狀況並不常見。事實上,快速排序通常明顯比其他Ο(n log n) 演算法更快,因為它的內部循环(inner loop)可以在大部分的架構上很有效率地被實作出來,且在大部分真實世界的資料,可以決定設計的選擇,減少所需時間的二次方項之可能性。
阅读全文
摘要:在一场笔试中,碰到的题目,给一个数组,找出最小的K个数,当然首先想到的就是排序了,库函数sort一下,直接输出.
可以肯定的是这肯定不是出题者想看到的答案,各种排序算法中堆排序对这种寻找最大最小的K个数效率应该是最高的.
可惜的是笔试现场没能一挥而就...之后在电脑上实现了一下,完全的堆排序,对于寻找K个最大最小的只需用大根堆,小根堆然后判断一下到第K个就可以停止了.
阅读全文

浙公网安备 33010602011771号