退役太久手生了唉..


目前期望手撕的名单:
1.各种排序(归并/快排/堆排/冒泡选择插入/桶排/基数..)
2.各种数据结构(链表/堆/带旋转的平衡树)
3.杂

主要是C++实现,有空我会接着改为Java(虽然没差
实现方法尽量是人尽皆知的那种
并且实现均会通过OJ测试


1.归并排序

void merge(int *arr, int lo, int mid, int hi) {int *tmp = (int *)malloc((hi-lo+1)*sizeof(int));int i = lo, j = mid+1, k = 0;while(i<=mid && j<=hi) {if(arr[i] <= arr[j]) tmp[k++] = arr[i++];else tmp[k++] = arr[j++];}while(i <= mid) tmp[k++] = arr[i++];while(j <= hi)  tmp[k++] = arr[j++];memcpy(arr+lo, tmp, k*sizeof(int));free(tmp);
}
void mergeSort(int *arr, int lo, int hi) {if(lo == hi) return;int mid = lo+hi >> 1;mergeSort(arr, lo, mid);mergeSort(arr, mid+1, hi);merge(arr, lo, mid, hi);
}

Java version

package com.caturra.sorting;import java.util.*;/*** 归并排序,个人习惯使用全闭区间,[lo,hi]* @author Caturra**/
public class MergeSort {public static void sort(Comparable[] comps,int lo,int hi) {if(lo >= hi) return;int mid = lo+hi >> 1;sort(comps,lo,mid);sort(comps,mid+1,hi);merge(comps,lo,mid,hi);}private static void merge(Comparable[] comps,int lo,int mid,int hi) {Comparable[] temp = new Comparable[hi-lo+1];int i = lo, j = mid+1, k = 0;while(i <= mid && j <= hi) {int cmp = comps[i].compareTo(comps[j]);if(cmp < 0) {temp[k++] = comps[i++];} else {temp[k++] = comps[j++];}}while(i <= mid) temp[k++] = comps[i++];while(j <= hi)  temp[k++] = comps[j++];for(k = lo; k <= hi; k++) comps[k] = temp[k-lo]; }/*** 测试样例,HDU1425* @param args*/public static void main(String[] args) {Scanner sc = new Scanner(System.in);while(sc.hasNext()) {int n = sc.nextInt();int m = sc.nextInt();Integer[] arr = new Integer[n];for(int i = 0; i < n; i++) {arr[i] = sc.nextInt();}sort(arr, 0, n-1);for(int i = 0; i < n/2; i++) {arr[i] ^= arr[n-i-1];arr[n-i-1] ^= arr[i];arr[i] ^= arr[n-i-1];}for(int i = 0; i < m-1; i++) System.out.print(arr[i]+" ");System.out.println(arr[m-1]);}}
}

2.快排

快排几乎没敲过,过去太依赖sort了..
需要注意的点:
1.找轴点时i经过的地方必然保证小于未来的基准点,j同理必然大于未来的基准点,因此交错时则停止扫描
\(comps[1...i-1] \lt p\) 交错后\(j=i-1\),存在\(comps[0]>comps[j]\)的可能,因此还需要进一步交换满足轴点的定义
既交错后\(comps[j] \geq comps[0...j-1]\)是最后一步保证的,\(comps[j] \lt comps[j+1...hi]\)是先前的双指针交换保证的
2.j>=0是恒成立的,最后可能停留在0
3.有序表现很惨

Cpp实现参考自邓俊辉老师的写法,相当简洁

// trick:找轴点前先rand一下才能保证复杂度
int partition(int *arr, int lo,int hi) {swap(arr[lo], arr[lo+rand()%(hi-lo+1)]);int val = arr[lo], pivot = lo;for(int i = lo+1; i <= hi; i++) {if(arr[i] < val) swap(arr[++pivot], arr[i]);}swap(arr[lo], arr[pivot]);return pivot;
}
void quickSort(int *arr, int lo,int hi) {if(lo >= hi) return;int pivot = partition(arr, lo, hi);quickSort(arr, lo, pivot-1);quickSort(arr, pivot+1, hi);
} 

Java version

package com.caturra.sorting;import java.util.Scanner;public class QuickSort {public static void sort(Comparable[] comps,int lo,int hi) { if(lo >= hi) return;int j = partition(comps,lo,hi);sort(comps,lo,j-1);sort(comps,j+1,hi);}private static int partition(Comparable[] comps,int lo,int hi) {exch(comps,lo,(int)(lo+Math.random()*(hi-lo))); //通常需要随机化Comparable val = comps[lo];int i = lo, j = hi+1;while(true) {while(i < hi && comps[++i].compareTo(val) < 0);while(j > lo && comps[--j].compareTo(val) > 0);if(i >= j) break;exch(comps,i,j);}exch(comps,lo,j);return j;}private static void exch(Object[] comps,int i,int j) {Object t = comps[i];comps[i] = comps[j];comps[j] = t;}public static void main(String[] args) {// 测试同上}
}

为了处理重复数过多带来的劣化而引入三向切分的方法

    public static void sort(Comparable[] comps,int lo,int hi) {if(lo >= hi) return;int lt = lo, gt = hi, i = lo+1;Comparable val = comps[lo];while(i <= gt) {int cmp = comps[i].compareTo(val);if(cmp < 0) exch(comps,lt++,i++); //comps[i]太小,放入已经排好序的[0,lo)当中,此时和lt连续接壤else if(cmp > 0) exch(comps,i,gt--); //放入大于val的一边,引入的[gt]接着比较else i++;}sort(comps,lo,lt-1);sort(comps,gt+1,hi); //此时[lt,gt]都是重复存在的数}

3.\(O(n^2)\)排序俱乐部

// 相当直观的写法,有一种高逼格写法学不来(
void bubbleSort(int *arr, int lo, int hi) {bool isSorted = false;while(!isSorted) {isSorted = true;for(int i = lo; i < hi; i++) {if(arr[i] > arr[i+1]) {swap(arr[i], arr[i+1]);isSorted = false;}}}
}
/***************************************************/
//trick:倒过来交换是不断挑选最优插入点的过程
void insertSort(int *arr, int lo, int hi) {for(int i = lo+1; i <= hi; i++) {int j = i;while(j>lo && arr[j]<arr[j-1]) { swap(arr[j], arr[j-1]);--j;}}
}
/***************************************************/
// 毫无实现难度
void selectSort(int *arr, int lo, int hi) {for(int i = lo; i <= hi; i++) {int minIdx = i;for(int j = i; j <= hi; j++) {if(arr[j] < arr[minIdx]) {minIdx = j;}}swap(arr[i], arr[minIdx]);}
}

4.heap简略版 / 没有封装堆排 / 没有O(n)构造

struct Heap {int heap[MAXN],tot;void init(){tot=0;}void insert(int val) {heap[++tot]=val;int now = tot;while(heap[now] < heap[now>>1]) {swap(heap[now],heap[now>>1]);now >>= 1; }}int pop() {int res = heap[1];heap[1] = heap[tot--];int now = 1;while(now*2 <= tot) {int nxt = now<<1;if(nxt+1<=tot && heap[nxt+1] < heap[nxt]) nxt++; //  find minif(heap[now] < heap[nxt]) return res;swap(heap[now], heap[nxt]);now = nxt;}return res;}
}h; 

5.KMP

char text[MAXN], pattern[MAXN];
int f[MAXN], nxt[MAXN];
void init() {int len = strlen(pattern+1);int j = 0;nxt[1] = 0;for(int i = 2; i <= len; i++) {while(j>0 && pattern[i]!=pattern[j+1]) j = nxt[j];if(pattern[i] == pattern[j+1]) j++;nxt[i] = j;}
}
int match() {int n = strlen(text+1), m = strlen(pattern+1);int j = 0, ans = 0;for(int i = 1; i <= n; i++) {while(j>0 && (j==m || text[i]!=pattern[j+1])) j = nxt[j];if(text[i] == pattern[j+1]) j++;f[i] = j;if(f[i] == m) ans++;}return ans;
}

6.Huffman编码(丑

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+11;struct Node {int lc, rc;int weight;char val;int id;Node() {}Node(int _id,int _weight,char _val) {id = _id, weight = _weight, val = _val,lc = rc = 0;}bool operator < (const Node &rhs) const {return weight > rhs.weight;}
}huffman[MAXN]; int tot;
priority_queue<Node> pq;
string ans[128];
string tmpBit;void dfs(int now) {if(huffman[now].val) {ans[huffman[now].val] = tmpBit;}tmpBit.push_back('0');if(huffman[now].lc) dfs(huffman[now].lc);tmpBit.pop_back();tmpBit.push_back('1');if(huffman[now].rc) dfs(huffman[now].rc);tmpBit.pop_back();
}int num[128];
string str;int main() {while(cin >> str) {memset(num,0,sizeof num);tot = 0;while(!pq.empty()) pq.pop();int all = 0, allId = 0;for(int i = 0; i < str.length(); i++) {if(num[str[i]] == 0) all++, allId = str[i];num[str[i]]++;}if(all == 1) {cout << (char)allId <<":: " << 0 << endl;cout << "totLength: " << str.length() << endl; cout << "rate: " << str.length() << endl; continue;}for(int i = 1; i < 128; i++) {if(!num[i]) continue;huffman[++tot] = Node(tot,num[i],i);pq.push(huffman[tot]);}while(pq.size() > 1) {Node tmp1 = pq.top(); pq.pop();Node tmp2 = pq.top(); pq.pop();huffman[++tot] = Node(tot,tmp1.weight+tmp2.weight,0);huffman[tot].lc = tmp1.id;huffman[tot].rc = tmp2.id;pq.push(huffman[tot]);}for(int i = 0; i < 128; i++) ans[i].clear();tmpBit.clear();dfs(tot);long long totLen = 0;vector<char> characters;int uniqueLength = 0;for(int i = 0; i < 128; i++) {if(num[i]) {cout<<(char)i<<":: "<<ans[i]<<endl;characters.push_back(i);uniqueLength += ans[i].length(); }totLen += num[i] * ans[i].length();}cout << "totLength: " << totLen << endl; cout << "rate: " << 8.0*str.length() / totLen << endl;int *encoded = (int *) malloc(((uniqueLength+31)/32)*sizeof(int));memset(encoded,0,sizeof encoded);int nowLength = 0;for(int i = 0; i < characters.size(); i++) {for(int j = 0; j < ans[characters[i]].size(); j++) {int x = nowLength / 32;int y = nowLength % 32;int z = 0;if(ans[characters[i]][j] == '1') {z = 1;}encoded[x] |= (z<<y);nowLength++;}}cout<<"ordered table: ";for(int i = 0; i < characters.size(); i++) {cout<<(char)characters[i]<<" ";} cout<<endl;nowLength = 0;for(int i = 0; i < characters.size(); i++) {for(int j = 0; j < ans[characters[i]].size(); j++) {int x = nowLength / 32;int y = nowLength % 32;cout<<(encoded[x]>>y&1);nowLength++;}cout<<"|";}cout<<endl;}return 0;
}

堆(new version)
这次的堆实现是按照算法导论的思路写的

import java.util.Arrays;
import java.util.Scanner;// 默认大根堆
public class MyHeap {public int[] heap;public int size;public boolean heapStatus;public MyHeap(int capacity) {heap = new int[capacity];size = 0;}public int down(int size,int idx) {int mx = 0;int res = heap[idx];while(mx != idx) {mx = idx;int lc = idx << 1;int rc = idx << 1 |1;if(lc <= size && heap[lc] > heap[idx]) {mx = lc;}if(rc <= size && heap[rc] > heap[mx]) {mx = rc;}if(mx != idx) {res = Math.max(res,heap[mx]);swap(heap,mx,idx);idx = mx; // idx换成了mx 但val还是原来下沉的那个mx = 0;}}return res;}public void build(int[] arr) {for(int i = 1; i <= arr.length; i++) {heap[i] = arr[i-1];}size = arr.length;for(int i = size>>1; i >= 1; --i) {down(size,i);}heapStatus = true;}public void swap(int[] arr,int i,int j) {int t = arr[i];arr[i] = arr[j];arr[j] = t;}public void heapSort(int[] arr) {build(arr);for(int i = size; i >= 2; --i) {swap(heap,1,i);down(i-1,1);}}public void up(int i) {while(i > 1 && heap[i] > heap[i>>1]) {swap(heap,i,i>>1);i >>= 1;}}public void insert(int val) {heap[++size] = val;up(size);}public int remove() {int res = down(size,1);swap(heap,1,size--);down(size,1);return res;}public static void main(String[ ] args) {MyHeap heap = new MyHeap(222);int[] arr = new int[] {0,5,2,67,4,1,6,9,3,2,11,7,0};heap.build(arr);System.out.println(Arrays.toString(heap.heap));System.out.println("built");heap.insert(5);heap.insert(666);heap.insert(-1);heap.remove();System.out.println("rm:"+heap.remove());
//        System.out.println("rm:"+heap.remove());
//        heap.heapSort(arr);System.out.println(Arrays.toString(heap.heap));}
}

LCA-RMQ实现

import java.util.HashMap;class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) { val = x; }
}class Solution {public HashMap<TreeNode,Integer> dep = new HashMap<>();public HashMap<TreeNode,TreeNode>[] p = new HashMap[20];public void dfs(TreeNode rt,TreeNode fa) {if(fa == null) {dep.put(rt,0);p[0].put(rt,rt);}else {dep.put(rt,dep.get(fa)+1);p[0].put(rt,fa);}for(int i = 1; i < 20; i++) {p[i].put(rt,p[i-1].get(p[i-1].get(rt)));}if(rt.left != null) dfs(rt.left,rt);if(rt.right != null) dfs(rt.right,rt);}TreeNode lca(TreeNode u,TreeNode v) {if(dep.get(u) < dep.get(v)) {TreeNode t = u;u = v;v = t;}for(int i = 0; i < 20; i++) {if((dep.get(u)-dep.get(v) >>i&1) == 1) {u = p[i].get(u);}}if(u == v) return u;for(int i = 19; i >= 0; --i) {if(p[i].get(u) != p[i].get(v)) {u = p[i].get(u);v = p[i].get(v);}}return p[0].get(u);}public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {if(root == null || p == null || q == null) return null;for(int i = 0; i < 20; i++) this.p[i] = new HashMap<>();dfs(root,null);return lca(p,q);}
}

转载于:https://www.cnblogs.com/caturra/p/10544538.html

手撕代码合集[短期更新]相关推荐

  1. VBA代码合集(更新2023.01.05)

    目录 VBA拆分工作簿 VBA合并工作簿 VBA合并工作表 VBA一键批量修改工作表名称 VBA多个Excel合并为1个文件多个工作表 VBA拆分工作簿 Sub 拆分工作薄()Dim xpath As ...

  2. 2013计算机视觉代码合集

    注:下面有project网站的大部分都有paper和相应的code.Code一般是C/C++或者Matlab代码. 最近一次更新:2013-9-7 一.特征提取Feature Extraction: ...

  3. Interview:算法岗位面试—11.06早上上海某智能驾驶科技公司(创业)笔试+面试之手撕代码、项目考察、比赛考察、图像算法的考察等

    Interview:算法岗位面试-11.06早上上海某智能驾驶科技公司(创业)笔试+面试之手撕代码.项目考察.比赛考察.图像算法的考察等 导读:该公司是在同济某次大型招聘会上投的,当时和HR聊了半个多 ...

  4. YOLOv3最全复现代码合集(含PyTorch/TensorFlow和Keras等)

    点击上方"CVer",选择"置顶公众号" 重磅干货,第一时间送达 前戏 2018年3月26日,CVer第一时间推文:YOLOv3:你一定不能错过 2019年3月 ...

  5. 前端date format_前端面试-手撕代码篇

    前言 在前端面试有一个非常重要的环节,也是面试者最担心的一个环节.对"手撕代码"的考察需要面试者平时总结和积累(临时抱佛脚是不好使的),在这里笔者就自己如何攻破"手撕代码 ...

  6. 王道考研机试指南代码合集

    王道考研机试指南代码合集 github链接 王道考研机试指南的代码合集,附有一些笔记和感悟 文件夹中包括机试指南的pdf,笔记部分,和代码对应题目的列表 如发现任何问题欢迎在下面留言 更新: 最短路题 ...

  7. 秋招总结:遇到的手撕代码题

    2020年秋招总结:遇到的手撕代码题 跟谁学 一面:求连续子数组的最大和(力扣 53) [思路:力扣系列略,题解区都比我讲得好] 二面:翻转字符串中的每个单词(简单题,比较常见,没去找对应的原题) [ ...

  8. 数字IC手撕代码-兆易创新笔试真题

    前言: 本专栏旨在记录高频笔面试手撕代码题,以备数字前端秋招,本专栏所有文章提供原理分析.代码及波形,所有代码均经过本人验证. 目录如下: 1.数字IC手撕代码-分频器(任意偶数分频) 2.数字IC手 ...

  9. 2023华为OD面试手撕代码经验分享

    我们先来看下这个同学的面试经历吧,非常有借鉴的意义. [22届考研渣渣的od求职之旅,推荐一下两个人,德科hr和牛客的老哥] "*********",hr给了机会吧,一开始我都没想 ...

  10. 数字IC手撕代码-流水握手(利用握手解决流水线断流、反压问题)

    前言: 本专栏旨在记录高频笔面试手撕代码题,以备数字前端秋招,本专栏所有文章提供原理分析.代码及波形,所有代码均经过本人验证. 目录如下: 1.数字IC手撕代码-分频器(任意偶数分频) 2.数字IC手 ...

最新文章

  1. 数据结构 之 并查集
  2. 聊聊tomcat jdbc pool的默认参数及poolSweeper
  3. UE4 Run On Server与Run on owning client
  4. 使用Python完美管理和调度你的多个任务
  5. 按键精灵定位坐标循环_[按键精灵教程]带你了解多点找色、多点比色
  6. nginx 源码安装
  7. 出租车计费-Java练习题
  8. ART模式下dex2oat出错导致系统无法正常启动
  9. 14个面试中常见的概率问题
  10. 千兆1光8电导轨式工业级以太网光纤收发器8口千兆光纤收发器工业导轨式发送机
  11. 【数据库】ER模型的简单例子
  12. 数学与泛型编程(6)编程的基本概念
  13. 2019.12.29 BMR计算
  14. Springboot集成Brpc
  15. 壁式框架内力计算_框架剪力墙结构的特点_剪力墙结构内力计算与设计
  16. python从DataFrame中删除列
  17. 如何添加JavaScript代码
  18. 基于Python的图像超分辨率(Image Super Resolution)
  19. 1-7信号调制与PCM基础(物理层)
  20. linux CMA 布局

热门文章

  1. system.data oracleClient 需要Oracle客户端8.1.7或high
  2. IOC(控制反转)的理解
  3. Linux下如何编写和使用自定义的Shell函数和函数库
  4. SpringMVC之二:配置 Spring MVC
  5. c 如何调用c语言程序设计,c语言程序设计_函数调用c语言程序设计_函数调用.ppt...
  6. pcie握手机制_图解PCIE原理(从软件角度)
  7. 全国最优秀的计算机学校,2020全国最好的计算机专业学校排名
  8. android webview 选择图片上传,Android webview打开本地图片上传实现代码
  9. Java之java.lang.CloneNotSupportedException,Java中bean的克隆报错
  10. mysql实际是用命令还是图形化_那些你不常用却非常有用的MySql语句和命令