Leetcode题解(七)
摘要:24、Swap Nodes in Pairs题目看到此题,第一想法是利用两个指针,分别将其所指向的节点的value交换。然后同时向后移动2个节点,代码如下: 1 struct ListNode { 2 int val; 3 ListNode *next; 4 Li...
阅读全文
Leetcode题解(六)
摘要:21、Merge Two Sorted Lists题目直接上代码:class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *helper=new ListNode...
阅读全文
Leetcode题解(五)
摘要:17、Letter Combinations of a Phone Number题目针对输入的数字串,每一个数字都对应对个字符可以选择。因此可以直接采用递归的思想,依次遍历数字串的每一个数字,处理到当前数字时,余下的数字可以看出一个规模更小的子串来处理,这正符合递归的思想,将问题逐步化小。代码如下:...
阅读全文
Leetcode题解(四)
摘要:12/13、Integer to Roman/RomantoInteger题目罗马数字规则:符号IVXLCDM数字1510501005001000代码如下: 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 ...
阅读全文
Leetcode题解(三)
摘要:8、String to Integer (atoi)题目这道题目关键是要考虑清楚各种输入用例。针对每一种情况,函数都能处理,也就是函数鲁棒性很高。代码如下: 1 class Solution { 2 public: 3 int myAtoi(string str) { 4 i...
阅读全文
Leetcode题解(二)
摘要:4、Median of Two Sorted Arrays(*)题目题目要求找到两个排序数组的中位数。中位数的定义:当n为奇数时,median = array[n/2];当n为偶数时,median = (array[n/2]+array[n/2+1])/2.暴力算法,两个数组归并排序,对合并的数组求...
阅读全文
腾讯2016编程笔试题
摘要:1、在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code)。请编写一个函数,使用递归方法生成N位的格雷码,并且保证这个函数的健壮性。 首先的搞清楚格雷码是什么,百度百科 生成格雷码的方法很多,百科中提到几种生成格雷码的方法,其中包括如下几种: ①递归法
阅读全文
剑指offer(一)
摘要:面试题3:二维数组中查找题目描述: 在一个二维数组中,每一行都按照从左往右递增地顺序排序,每一列都按照从上往下递增的顺序排序。请完成一个函数,输入这样的一个数组和一个整数,判断数组中是否存在该整数。算法一:直接查找,即采取遍历数组的方法;算法二:从数组右上角开始比较;算法三:从数组左下角开始比较;算...
阅读全文
Leetcode题解(一)
摘要:1、Two Sum题目此题第一解题思路,就是最常见的方法,两个指针嵌套遍历数组,依次判断当前指针所指向的值是否满足条件。代码如下; 1 class Solution { 2 public: 3 vector twoSum(vector& nums, int target) { 4 ...
阅读全文
回文字符序列
摘要:转载http://blog.csdn.net/u014800748/article/details/451484411、问题描述描述给定字符串,求它的回文子序列个数。回文子序列反转字符顺序后仍然与原序列相同。例如字符串aba中,回文子序列为"a", "a", "aa", "b", "aba",共5个...
阅读全文
动态规划求一个序列的最长回文子序列(Longest Palindromic Substring )
摘要:1、问题描述给定一个字符串(序列),求该序列的最长的回文子序列。2、分析需要理解的几个概念:---回文---子序列---子串http://www.cnblogs.com/LCCRNblog/p/4321398.html这一篇文章描述了利用动态规划求解两个序列的最长公共子序列(Longest Comm...
阅读全文
统计闰年2月29日天数
摘要:1.问题描述描述给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期)。只有闰年有2月29日,满足以下一个条件的年份为闰年:1. 年份能被4整除但不能被100整除2. 年份能被400整除输入第一行为一个整数T,表示数据组数。之后每组数据包含两行。每一行格式为"month day, yea...
阅读全文
Leetcode 172 Factorial Trailing Zeroes
摘要:1、题目要求Given an integern, return the number of trailing zeroes inn!.Note:Your solution should be in logarithmic time complexity.题目意思是求n的阶乘后面末尾0的个数,并且时间...
阅读全文
Leetcode 199 Binary Tree Right Side View
摘要:1.题目要求Given a binary tree, imagine yourself standing on therightside of it, return the values of the nodes you can see ordered from top to bottom.For ...
阅读全文
Leetcode 198 House Robber
摘要:1.题目要求You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopp...
阅读全文
Leetcode 15 3sum
摘要:1.题目要求Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:...
阅读全文
Leetcode 3 Longest Substring Without Repeating Characters
摘要:1.题目要求Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating lett...
阅读全文
Leetcode 4 Median of Two Sorted Arrays
摘要:1.题目要求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 shou...
阅读全文
Leetcode 1 Two Sum
摘要:1.题目要求Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the ...
阅读全文
面试算法题:16个数,最多用20次比较,找出第二大的数?
摘要:这道题最笨的方法就是先从16个数中选出最大的数,然后再从剩下的15个数中选出最大数就可得到答案,因此,需要15+14=29次比较。既然这道题要求在20次比较之内就能找出第二大的数,那我们就想能简单的方法。假设16个数中最大的是A,第二大的是B。首先将16个数两两进行比较,较大者胜出,然后再在胜出者中...
阅读全文