随笔分类 - 数据结构与算法设计
摘要:分析:求最长重复子序列,即说明要找到一个至少出现两次的最长的子序列。假设某个子序列第二次出现和第一次出现的位置相差i,则i的值为1,2,、、、,str.size()-1,代码如下所示:string longestRepeatSubstring(const string&str){ int n...
阅读全文
摘要:Description:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents...
阅读全文
摘要:Description:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.Code:#define NU...
阅读全文
摘要:Description:Given an integern, return all distinct solutions to then-queens puzzle.Each solution contains a distinct board configuration of then-queen...
阅读全文
摘要:Description:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeatin...
阅读全文
摘要:Description:Given a stringS, find the longest palindromic substring inS. You may assume that the maximum length ofSis 1000, and there exists one uniqu...
阅读全文
摘要:Description:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S=...
阅读全文
摘要:Description:Write a function to find the longest common prefix string amongst an array of strings.(最长公共字串)Code:string merge(string&str1, string&str2) ...
阅读全文
摘要:Description:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeatin...
阅读全文
摘要:Description:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Code1:vector g...
阅读全文
摘要:一. strcpy函数原型声明:char *strcpy(char* dest, const char *src);头文件:#include 和 #include 功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间说明:src和dest所指内存区域不可以重叠且des...
阅读全文
摘要:1. 使用atoi函数原型:int atoi(const char*nptr);头文件:stdlib.h示例:#include #include int main(void){ int n; char *str = "12345.67"; n = atoi(str); pr...
阅读全文
摘要:1. 使用itoa函数char *itoa(int value, char *string,int radix);原型说明:value:欲转换的数据。string:目标字符串的地址。radix:转换后的进制数,可以是2进制、8进制、10进制、16进制等。Exmaple:#include #inclu...
阅读全文
摘要:一.简单选择排序(不稳定) 1.基本思想:通过n-i次关键字间的比较,从n-i+1个记录中选出关键字最小的记录,并和第i个记录交换。 template <typename Comparable> void selectSort (vector<Comparable>&a) {//找出按从小到大排序应
阅读全文
摘要:归并算法具有稳定性 算法思想: 代码: 有稳定性 python版本代码
阅读全文
摘要:一. 直接插入排序(稳定) 算法原理 将一个记录插入到已经排好序的有序表中,从而得到一个新的,长度增加1的有序表。 【每遍操作】 现将元素复制到0号位置(哨兵),再将本元素同已排序的序列,从尾开始比较。在已排序的序列中寻找自己的位置,进行插入;或者寻找不到,则一直进行到哨兵为止,即本元素最小,应放置
阅读全文
摘要:变换一个整数的符号,即正数变负数,负数变正数。1 int reverseSign(int n) {2 return ~n+1;3 }
阅读全文
摘要:给出一个16位的无符号整数。称这个二进制数的前8位为“高位”,后8位为“低位”。现在写一程序将它的高低位交换。例如,数34520用二进制表示为:1000011011011000将它的高低位进行交换,我们得到了一个新的二进制数:1101100010000110它即是十进制的55430。这个问题用位操作...
阅读全文
摘要:一个整数为2的幂说明该整数的二进制中只有一个1. bool isSquareOf2 (int n){ return (n&(n-1))==0?true:false;}
阅读全文
摘要:一.冒泡排序(稳定) 算法原理 1.首先比较第一个和第二个数据,将其中较小的数据放到第一个位置,较大的放到第二个位置。然后比较第二个和第三个位置,仍将较大的放到后一个位置。以此类推,直到比较第n-1个数据和第n个数据。这样就将待排序序列中的最大的一个放到第n个位置上,这个过程称为一趟排序。 2.对前
阅读全文