sort

leetcode_315_逆序对问题

本小妞迷上赌 提交于 2020-02-13 11:10:55
题目描述 本题来自于Leetcode的算法题库第315题,具体题目描述如下: 给定一个 nums 整数数组 ,按要求返回一个 counts 新数组 。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。 给出实例: 输入: [5,2,6,1] 输出: [2,1,1,0] 解释: 5 的右侧有 2 个更小的元素 (2 和 1). 2 的右侧仅有 1 个更小的元素 (1). 6 的右侧有 1 个更小的元素 (1). 1 的右侧有 0 个更小的元素. 最容易想到的解题思路 这个题目看上去很容易读懂也很容易下手,但是在难度设置上是 hard ,其实最大的问题就是复杂度的问题,最基本的思路就是遍历输入的数组,比较每一个元素和其之后的元素使用计数器记录并添加到输出数组中去,这样的算法复杂度很显然是$O(n^2)$, 代码如下: 12345678910 class : def countSmaller(self, nums): result = [] for i in range(len(nums)): temp = 0 for j in range(i,len(nums)): if nums[i] > nums[j]: temp += 1 result.append(temp) return result

刷题--双指针(2)

痴心易碎 提交于 2020-02-13 07:30:33
相向双指针的第二种题型: Partition array 基本题型:将一个数组根据某个条件分割,比如大于target,奇偶等等 例 lintcode 31. Partition Array https://www.lintcode.com/problem/partition-array/description 首先外层的循环是left <= right 考虑相等的情况可以保证结束循环的时候两种情况 1 right 和 left左右相邻 2 right 和 left 中间空了一个,左右排列。如果条件是left < right 那么当left == right 时跳出循环,nums[left]还要再判断一次。 partition array 的同向双指针做法和quick sort是非常像的,但是在具体的细节上两者的模板有一定的不同。 public int partitionArray(int[] nums, int k) { if(nums == null || nums.length == 0)return 0; int left = 0, right = nums.length - 1; while(left <= right){ while(left <= right && nums[left] < k){ left++; } while(left <= right &&

nginx常用伪静态设置

六眼飞鱼酱① 提交于 2020-02-13 07:05:17
nginx里使用伪静态是直接在nginx.conf 中写规则的,并不需要像apache要开启写模块(mod_rewrite)才能进行伪静态。 nginx只需要打开nginx.conf配置文件,在server里面写需要的规则即可。 复制代码 代码如下: server { listen 80; server_name bbs.jb51.net; index index.html index.htm index.php; root /home/www/bbs; error_page 404 /404.htm; #配置404错误页面 location ~ .*.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } #下面就是伪静态了 location /{ rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last; } access_log access_log off; } 然后重启nginx服务器伪静态就生效了,这种维护起来很是不方便我们可以把它写在外部文件如bbs_nginx

字符串排序算法

久未见 提交于 2020-02-12 22:18:52
字母表数据结构 package string; import edu.princeton.cs.algs4.StdOut; public class Alphabet { public static final Alphabet BINARY = new Alphabet("01"); public static final Alphabet OCTAL = new Alphabet("01234567"); public static final Alphabet DECIMAL = new Alphabet("0123456789"); public static final Alphabet HEXADECIMAL = new Alphabet("0123456789ABCDEF"); public static final Alphabet DNA = new Alphabet("ACGT"); public static final Alphabet LOWERCASE = new Alphabet("abcdefghijklmnopqrstuvwxyz"); public static final Alphabet UPPERCASE = new Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); public static final

C# bubble sort,selection sort,insertion sort

拥有回忆 提交于 2020-02-12 17:52:36
static void Main(string[] args) { InsertionSortDemo(); Console.ReadLine(); } static void InsertionSortDemo() { Random rnd = new Random(); int[] arr = new int[100]; for (int i = 0; i < 100; i++) { arr[i] = rnd.Next(0, 1000); } Console.WriteLine("Raw data:"); foreach (var a in arr) { Console.Write(a + "\t"); } InsertionSort(arr); Console.WriteLine("\n\n\nAfter insertion sort:"); foreach(var a in arr) { Console.Write(a + "\t"); } } static void InsertionSort(int[] arr) { int inner, temp; for (int outer = 0; outer < arr.Length; outer++) { temp = arr[outer]; inner = outer; while (inner > 0 && arr

[PAT-A 1101]Quick Sort

霸气de小男生 提交于 2020-02-12 11:49:15
题目大意: 著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边。 给定划分后的 N 个互不相同的正整数的排列,请问有多少个元素可能是划分前选取的主元? 以快速排序引出,其实与快排没有关系。 给定序列,包含N个正整数,如果一个属左边的所有数都比它小,右边的所有数都比它大,则成该数为主元,求序列中主元的个数。 思路: 1)直接暴力求解会超时。 2)假设序列为A,定义数组leftMax.记录序列A的每一位左边的最大值(不含本位),即leftMax[i]记录了A[0]-A[i]的最大值,显然可以令leftMax[0]=0, 从左至右遍历A,left[i-1]记录A[0]-A[i-2]的最大值,与A[i-1]相比,决定leftMax[i]的取值 3)同理,定义数组rightMin记录A的每一位右边的最小数,令rightMin[i]记录A[i+1]-A[n-1]的最小值,操作同上。 4)遍历A,如果第i个元素大于leftMax[i],小于rightMin[i],则该数为一个主元,输出。 AC代码: //PAT_A 1101 # include <cstdio> # include <algorithm> using namespace std ; const int maxn = 100010 ;

arraynodeSorting

一个人想着一个人 提交于 2020-02-12 04:12:36
发一下牢骚和主题无关: A sorting algorithm is an algorithm that puts elements of a list in a certain order . The most-used orders are numerical order and lexicographical order. There are two mainly kind of sorting algorithms. 2) distribution based sorting algorithms. Counting Sort - A simple and fast sorting algorithm that creates an integer array of size |S| and using the ith bin to count the occurrences of the ith member of S in the input. Each input is then counted by incrementing the value of its corresponding bin. Afterward, the counting array is looped through to arrange all of the inputs in order.

LINUX Shell 下求两个文件交集和差集的办法

点点圈 提交于 2020-02-12 00:05:43
http://blog.csdn.net/autofei/article/details/6579320 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: [xhtml] view plain copy a b c e d a FILE2内容如下: [xhtml] view plain copy c d a c 基本上有两个方法,一个是comm命令,一个是grep命令。分别介绍如下: comm命令 , Compare sorted files FILE1 and FILE2 line by line. With no options, produce three-column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files. 要注意两个文件必须是排序和唯一(sorted and unique)的,默认输出为三列,第一列为是A-B,第二列B-A,第三列为A交B。 直接运行结果如下: [xhtml] view plain copy $ comm a.txt b.txt a b c d a c e d a 仅仅排序:

LINUX Shell 下求两个文件交集和差集的办法

南楼画角 提交于 2020-02-11 22:12:42
假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d a FILE2内容如下: c d a c 基本上有两个方法,一个是comm命令,一个是grep命令。分别介绍如下: comm命令 , Compare sorted files FILE1 and FILE2 line by line. With no options, produce three-column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files. 要注意两个文件必须是排序和唯一(sorted and unique)的,默认输出为三列,第一列为是A-B,第二列B-A,第三列为A交B。 直接运行结果如下: $ comm a.txt b.txt a b c d a c e d a 仅仅排序: $ comm <(sort a.txt ) <(sort b.txt ) a a b c c d e 排序并且唯一: $ comm <(sort a.txt|uniq ) <(sort b.txt|uniq ) a b c d e 如果只想要交集

75. Sort Colors 荷兰国旗问题

感情迁移 提交于 2020-02-11 22:09:41
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. Note: You are not suppose to use the library's sort function for this problem. Example: Input: [2,0,2,1,1,0] Output: [0,0,1,1,2,2] class Solution { public: void sortColors(vector<int>& nums) { int p1 = 0, p2 = 0, p3 = nums.size() - 1; while(p2 <= p3) { if (nums[p2] == 0) { swap(nums[p1], nums[p2]); ++p1; ++p2; } else if