目前在阿里巴巴搬砖
1.6 给定一幅由N*N矩阵表示的如下,其中每个像素的大小为4个字节,编写一个方法,将图像旋转90度。不占用额外内存空间能否做到? 类似leetcode:Rotate Image 思路: 我们这里以逆时针旋转为例(写代码时比较容易理解,顺时针旋转的实现思想相似),可以先将原矩阵以主对角线为对称轴,交换主对角线两侧的的元素,得到新的矩阵(如果是顺时针旋转,可以交换副对角线两侧的元素),再将该矩阵的第i行和第n-i-1行相交换,即得到逆时针旋转后的矩阵。
1.5 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能。比如,字符串”aabcccccaaa“会变成”a2b1c5a3“。若”压缩“后的字符串没有变短,则返回原先的字符串。 类似 leetcode中 解法:Count and say.
1.4 编写一个方法,将字符串中的空格全部替换为“%20“。假定该字符串尾部有足够的空间存放新增字符,并且知道字符串的”真实“长度。 C++实现代码: #include #include #include using namespace std; string replacespace(string str) { if(str.
1.3 给定两个字符串,请编写程序,确定其中一个字符串的字符重新排序后,能否变成另一个字符串。 C++实现代码: #include #include #include using namespace std; bool isEqual(string s1,string s2) { map mp; if(s1.
1.2 用C或C++实现void reverse(char *str)函数,即反转一个null结尾的字符串。 C++实现代码: #include #include using namespace std; /* 反转字符串 */ void reverse(char *str)...
1.1 实现一个算法,确定一个字符串的所有字符是否全部不同。假设不允许使用额外的数据结构,又该如何处理? C++实现: #include #include #include using namespace std; /* 判断是否有重复字符 */ bool unqString(string s) { if(s.
这里主要谈及强连通分量(以下简称SCC,strongly connected component)三种常见的求法(以下涉及的图均为有向图),即Kosaraju、Tarjan和Gabow。三种算法背后的基础思想都是DFS,只是它们通过DFS获得了不同的信息。
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous s...
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character.
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatena...
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/.
Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s.
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
动态规划 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 求字符串的最长回文子串 算法1:暴力解法,枚举所有子串,对每个子串判断是否为回文,复杂度为O(n^3) 算法2:删除暴力解法中有很多重复的判断。
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. 这个题跟Permutations非常类似,唯一的区别就是在这个题目中元素集合可以出现重复。
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路:逐位相加,进位保留在和的下一位中。
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 给你一个整数,把它转化为罗马数字,输入保证在1到3999之间。
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions.
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
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 should be O(log (m+n)). 题解: 首先我们先明确什么是median,即中位数。
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).
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter...
Dynamic Programming Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false. 这道题,可以用Recursion和DP两种方法解,我们先来看比较直观的Recursion的解法。
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / \ 2 3 Return 6.
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region.
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 参考:http://xpentium.blog.163.com/blog/static/22737815020147795123240/ 题目是说matrix中的元素可能有0和1,找出一个子矩阵,这个子矩阵要求是全为1中最大的。
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit.
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2.
Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n).
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adja...
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa","b"], ["a","a","b"] ] 与Restore IP Addresses类似。
Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: ...
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 .
Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below.
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequenc...
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 这道题其实是关于卡特兰数的,如果只是要输出结果有多少组,那么直接用卡特兰数的公式就可以。
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [...
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
动态规划: There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the foll...
Divide two integers without using multiplication, division and mod operator. 思路: 这道题属于数值处理的题目,对于整数处理的问题,在Reverse Integer中我有提到过,比较重要的注意点在于符号和处理越界的问题。
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 参考:http://blog.
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure.