随笔分类 - ACM
摘要:题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 代码: cla...
阅读全文
摘要:题目说明: Write a function to find the longest common prefix string amongst an array of strings. 程序代码: #include using namespace std; string longestCommonPrefix1(vector& strs) { int nMinLength...
阅读全文
摘要:题目说明: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 程序代码: #include using namespace std; //{'I','V','X','L','C','D','M','#','#'};...
阅读全文
摘要:题目说明: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 程序代码: 思路是把数字拆成单独的部分,最后拼接起来 1. 拆分数字,求出每一位数字。 2. 把每位数字单独转换singleDigitToRoman成罗马数字,...
阅读全文
摘要:Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes?
阅读全文
摘要:题目说明: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input...
阅读全文
摘要:题目说明: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321 程序代码: #include using namespace std; int reverse2(int x) { int result[20] = {0}; ...
阅读全文
摘要:题目说明: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P ...
阅读全文
摘要:题目说明: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as ...
阅读全文
摘要:题目说明: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2...
阅读全文
摘要:概述: 基数排序是一种高效的线性排序算法。其方法是将数据按位分开,并从数据的最低有效位到最高有效位进行比较,依次排序,从而得到有序数据集合。 例子: 用基数排序对十进制数据{15,12,49,16,36,40}进行排序。 在对个位数排序之后=> {40,12,15,16,36,49}; 在对十位数排序之后=> {12,15,16,36,40,49}; 有一点非常重要,在对每一位数值进行排序时其排序...
阅读全文
摘要:概述: 计数排序是一个非基于比较的排序算法,该算法于1954年由 Harold H. Seward 提出。它的优势在于在对一定范围内的集合排序时,它的复杂度为Ο(n+k)(其中k是元素的范围),快于任何比较排序算法。 计数排序本质上是通过计算无序集合中元素出现的次数来决定集合应该如何排序的。 例如一个数组{1, 4, 1, 2, 7, 5, 2},进行计数排序过程 1、使用Count数组记录元...
阅读全文
摘要:题目说明: 输入一个数列(包含正负数),求和是最大的连续子序列。例如{5,-3,4,2}的最大子序列就是 {5,-3,4,2}。 程序代码: #include using namespace std; int MaxSumOfSequence(int* pData, int nLength, int& nBegin, int& nEnd) { if (!pData || !nLen...
阅读全文
摘要:题目说明: 找两个字符串的最长公共子序列,这个子序列不要求在原字符串中是连续的,但要求顺序是一致的,比如"abcd"和"aebfc"的最长公共子串是"abc"。 程序代码: #include #include using namespace std; enum DirectionType { TypeUp, TypeLeftUp, TypeLeft }; int...
阅读全文
摘要:题目说明: 找两个字符串的最长公共子串,这个子串要求在原字符串中是连续的。比如"bab"和"caba"的最长公共子串是"ba"和"ab"。 程序代码: #include #include #include using namespace std; int GetLCS(const string& strA, const string& strB, vector& result) { ...
阅读全文
摘要:题目说明: 输入一个表示整数的字符串,把该字符串转换成整数并输出。例如输入字符串"345",则输出整数345。 程序代码: #include using namespace std; int StrToInt(const char* szValue, int nBase = 0, bool* bValid = NULL) { long long nValue = 0; ...
阅读全文
摘要:题目说明: 输入一个链表的头结点,反转该链表,并返回反转后链表的头结点。链表结点定义如下:struct ListNode{ int m_nKey; ListNode* m_pNext;}; 程序代码: #include #include using namespace std; struct ListNode { int m_nKey; ListNode*...
阅读全文
摘要:题目说明: 把一个十进制的正整数转换成N进制的数,或者把一个N进制的数转换成十进制的数(2 using namespace std; string DecimalConvertToRadix(int nValue, int nRadix) { int State[256] = {0}; int nIndex = 0; static char szText[] =...
阅读全文
摘要:题目说明: 给定一组数字或符号,按照字典序产生所有可能的集合(包括空集合),例如给定1 2 3,则可能的集合为:{}、{1}、{1,2}、{1,2,3}、{1,3}、{2}、{2,3}、{3}。 题目解析: 如果要产生字典顺序,例如若有4个元素,则: {} => {1} => {1,2} => {1,2,3} => {1,2,3,4} => {1,2,4} => {1,3} => {1,3,...
阅读全文
摘要:题目说明: 给定一组数字或符号,产生所有可能的集合(包括空集合),例如给定1 2 3,则可能的集合为:{}、{1}、{1,2}、{1,2,3}、{1,3}、{2}、{2,3}、{3}。 题目解析: 如果不考虑字典顺序,则有个简单的方法可以产生所有的集合,思考二进位数字加法,并注意1出现的位置,如果每个位置都对应一个数字,则由1所对应的数字所产生的就是一个集合,例如: 000 {} 00...
阅读全文
浙公网安备 33010602011771号