随笔分类 -  算法题目

两道关于JS的小考题(闭包与中间件)
摘要:题目一:写一个javascript函数 calculate,该函数有如下性质 即可以连续地链式调用,一旦碰到一次调用没有参数的,则返回前面所有参数的和。 其实题目本身并不算复杂,代码也非常简单,就是思路有点绕,可能要在电脑上反复试试调调才能写对,答案如下: 主要思路就是用闭包变量记录当前的结果,所写 阅读全文

posted @ 2016-12-02 23:07 Horstxu 阅读(568) 评论(0) 推荐(0) 编辑

[leetcode]算法题目 - Sudoku Solver
摘要:最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法。回溯法当时初学的时候在思路上比较拧,不容易写对。写了几个回溯法的算法之后心里总算有了点底。回溯法的代码一般都是长成下面这样子:void backtracking(int[]... 阅读全文

posted @ 2015-05-16 00:24 Horstxu 阅读(667) 评论(0) 推荐(0) 编辑

[leetcode]算法题目 - Reverse Nodes in k-Group
摘要:Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.If the number of nodes is not a multiple ofkthen left-o... 阅读全文

posted @ 2015-04-16 00:53 Horstxu 阅读(278) 评论(0) 推荐(0) 编辑

[实战演练]蜻蜓FM2014年校招笔试题目 - 规则二叉树
摘要:题目:某规则二叉树的定义是:对于树中任意两个叶结点A、B,他们与根结点的距离分别是d1和d2,|d1-d2| record;void isRuledTreeCore(Node *n, int depth){ if(n->left == NULL && n->right == NULL){ record.push(depth); return; } if(n->left != NULL) isRuledTreeCore(n->left,depth+1); if(n->right != NULL) isRuledTre... 阅读全文

posted @ 2013-10-23 23:18 Horstxu 阅读(961) 评论(0) 推荐(0) 编辑

[实战演练]Intel面试题目 - 进栈出栈顺序问题
摘要:电话面试中写C++,逻辑比较清楚的一个题目,一紧张就不能好好地写下来,漏洞百出。以前经常在完善的编译环境中写代码,换了一个白板子上写反而写的不通顺了,犯了一些基础错误,比如stack中的首个元素是top方法,判断是否为空为empty方法,方法名字写错了……以后看来还是要勤加练习才好。废话不多说,直接看题目了。题目:两个数组,长度相同,都为n,两个数组分别为inseq和outseq,求出如果以inseq为入栈顺序,那么outseq可不可能是它的一个出栈顺序,可能则返回true样例:inseq = {1,2,3,4,5} outseq={5,4,3,2,1} 返回true;inseq = {1,2 阅读全文

posted @ 2013-10-14 13:53 Horstxu 阅读(2154) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Length of Last Word
摘要:Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word does not exist, return 0.Note:A word is defined as a character sequence consists of non-space characters only.For example,Givens="Hello Worl 阅读全文

posted @ 2013-09-25 22:48 Horstxu 阅读(339) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Sqrt(x)
摘要:Implementint sqrt(int x).Compute and return the square root ofx. 1 class Solution { 2 public: 3 int sqrt(int x) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if(xb)17 return temp;18 else if(temp<a && temp... 阅读全文

posted @ 2013-09-25 21:51 Horstxu 阅读(408) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Sort Colors
摘要:Given an array withnobjects colored red, white or blue, sort them 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 l 阅读全文

posted @ 2013-09-24 21:32 Horstxu 阅读(415) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Restore IP Addresses
摘要:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does not matter)我的答案如下: 1 class Solution { 2 public: 3 4 vector restoreIpAddresses 阅读全文

posted @ 2013-09-24 00:01 Horstxu 阅读(506) 评论(0) 推荐(0) 编辑

[实战演练]史上最长最醒目的队名
摘要:Problem Description Jack所在的班级决定组团报名参加FZU校赛。为了体现班级的团结和睦,班长决定用班级所有人的名字连起来组成一个史上最长最醒目的队名。 因为听说在分数相同的情况下,队名字典序小的会排在更前面,班长还希望连成的史上最长队名拥有最小的字典序。 Input 输入数据第一行包含一个整数T,表示测试数据的组数。对于每组测试数据:第一行为一个整数(10000>=n>0),表示班级成员数。 接下来n行为班级每个人的名字。名字由小写字母组成,每个人名字长度均相同。Output 对于每组测试数据,输出一行,表示连接成的史上最长队名。 Sample Input1 阅读全文

posted @ 2013-09-16 23:01 Horstxu 阅读(1537) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Pow(x, n)
摘要:Implement pow(x,n). 1 class Solution { 2 public: 3 double pow(double x, int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (0==n) return 1.0; 7 if (1==n) return x; 8 9 int k = abs(n);10 int rema... 阅读全文

posted @ 2013-09-13 20:11 Horstxu 阅读(496) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Jump Game
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A =[2,3,1,1,4], returntrue.A =[3,2,1,0,4], returnfalse. 1 阅读全文

posted @ 2013-09-12 21:11 Horstxu 阅读(264) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Pascal's Triangle
摘要:GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]] 1 class Solution { 2 public: 3 vector > generate(int numRows) { 4 // Start typing your C/C++ solution below 5 // DO NOT write in... 阅读全文

posted @ 2013-09-12 20:53 Horstxu 阅读(180) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Maximum Subarray
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6. 1 class Solution { 2 public: 3 int maxSubArray(int A[], int n) { 4 // Start... 阅读全文

posted @ 2013-09-11 22:27 Horstxu 阅读(204) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Plus One
摘要:Given a number represented as an array of digits, plus one to the number. 1 class Solution { 2 public: 3 vector plusOne(vector &digits) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 bool allNine = true; 7 int size = digi... 阅读全文

posted @ 2013-09-11 22:18 Horstxu 阅读(244) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Remove Duplicates from Sorted List
摘要:Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode 阅读全文

posted @ 2013-09-11 21:53 Horstxu 阅读(261) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Symmetric Tree
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3Note:Bonus points if you could solve it both recursively and iterati... 阅读全文

posted @ 2013-09-11 20:37 Horstxu 阅读(191) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Decode Ways
摘要:A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.For example,Given encoded message"12", it cou 阅读全文

posted @ 2013-09-10 00:00 Horstxu 阅读(212) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Gray Code
摘要:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.For example, givenn= 2, return[0,1,3,2]. Its gray code s 阅读全文

posted @ 2013-09-09 22:57 Horstxu 阅读(427) 评论(0) 推荐(0) 编辑

[leetcode.com]算法题目 - Same Tree
摘要:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * Tree... 阅读全文

posted @ 2013-09-09 22:23 Horstxu 阅读(211) 评论(0) 推荐(0) 编辑

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示