随笔分类 - Interview Algorithm
摘要:https://leetcode.com/problems/search-insert-position/ Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inse...
阅读全文
摘要:http://www.lintcode.com/zh-cn/problem/word-ladder-ii/# 给出两个单词(start和end)和一个字典,找出所有从start到end的最短转换序列 比如: 每次只能改变一个字母。 变换过程中的中间单词必须在字典中出现。 样例 给出数据如下: start...
阅读全文
摘要:再来看以下具体例子:
阅读全文
摘要:https://leetcode.com/problems/word-ladder/ http://www.lintcode.com/zh-cn/problem/word-ladder/ 给出两个单词(start和end)和一个字典,找到从start到end的最短转换序列 比如: 每次只能改变一个字母。 变换过程中的中间单词必须在...
阅读全文
摘要:题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 水题,直接上代码: class Solution
{
public: void push(int node) { stack1.push(node); } int pop() { if (stack2.empty()) { while (...
阅读全文
摘要:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, find the length of the longest substring without repeating characters. For example, the longest subs...
阅读全文
摘要:题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并输出它的后序遍历序列。(测试用例中,"树"的输出形式类似于树的层次遍历,没有节点的用#来代替) 除了一些编译错误,一遍AC了,真是爽。由...
阅读全文
摘要:题目描述 输入一个链表,从尾到头打印链表每个节点的值。 题目比较水,一遍就 AC 了,来看代码:/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution...
阅读全文
摘要:题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。 最直接的方式,直接使用 STL。假设字符串的长度是n,那么对于每个空格字符,需要移动后面 o(n) 个字符,因此对含有 o(n) 个空格字符的字符串而言时间复杂度为 o(n2),过高了。代码如下: class...
阅读全文
摘要:题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 来分析下,假设在数组中随便找一个数字 inner_number 来与 target 进行比较,如果 target > inner_number,那么我们需要在数组中剔除掉 inner_numb...
阅读全文
摘要:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ http://www.lintcode.com/zh-cn/problem/binary-tree-level-order-traversal-ii/# Given a binary tree, return the bottom-up leve...
阅读全文
摘要:https://leetcode.com/problems/binary-tree-level-order-traversal/ http://www.lintcode.com/zh-cn/problem/binary-tree-level-order-traversal/# Given a binary tree, return the level order traversa...
阅读全文
摘要:https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan...
阅读全文
摘要:https://leetcode.com/problems/two-sum/ 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 two nu...
阅读全文
摘要:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Ret...
阅读全文
摘要:https://oj.leetcode.com/problems/merge-k-sorted-lists/ Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Solution: 1. O(nk2) runtime, O(1) space – Bru...
阅读全文
摘要:https://oj.leetcode.com/problems/swap-nodes-in-pairs/ Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3....
阅读全文
摘要:https://oj.leetcode.com/problems/add-two-numbers/ 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...
阅读全文
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 水题,注意 dummy node 的使用即可,在不确定头节点的情况下,应该使用 dummy node,如此可以简化...
阅读全文
摘要:Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many disti...
阅读全文

浙公网安备 33010602011771号