- 华为机试考过的字符串压缩问题,没有难度。
- 给出了平衡二叉树的概念,让写一个函数判断是否为平衡二叉树。剑指offer书本的原题
方法一:
struct BinaryTreeNode
{
int m_Value;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
int TreeDepth(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return 0;
int nLeftDepth = TreeDepth(pRoot->m_pLeft);
int nRightDepth = TreeDepth(pRoot->m_pRight);
return (nLeftDepth>nRightDepth)?(nLeftDepth+1):(nRightDepth+1);
}
bool IsBalanced(BinaryTreeNode* pRoot)
{
if(pRoot== NULL)
return true;
int nLeftDepth = TreeDepth(pRoot->m_pLeft);
int nRightDepth = TreeDepth(pRoot->m_pRight);
int diff = nRightDepth-nLeftDepth;
if (diff>1 || diff<-1)
return false;
return IsBalanced(pRoot->m_pLeft)&&IsBalanced(pRoot->m_pRight);
}
方法二:
bool IsBalanced(BinaryTreeNode* pRoot, int* depth)
{
if(pRoot== NULL)
{
*depth = 0;
return true;
}
int nLeftDepth,nRightDepth;
bool bLeft= IsBalanced(pRoot->m_pLeft, &nLeftDepth);
bool bRight = IsBalanced(pRoot->m_pRight, &nRightDepth);
if (bLeft && bRight)
{
int diff = nRightDepth-nLeftDepth;
if (diff<=1 || diff>=-1)
{
*depth = 1+(nLeftDepth > nRightDepth ? nLeftDepth : nRightDepth);
return true;
}
}
return false;
}
bool IsBalanced(BinaryTreeNode* pRoot)
{
int depth = 0;
return IsBalanced(pRoot, &depth);
}
- 找到两个单链表的交点,如果不想交返回空,还有判断链表是否有环都是链表最基本的操作,剑指offer都有非常类似的题。
参考:http://blog.chinaunix.net/uid-20754793-id-177773.html - 跟广联达业务比较紧密的一道题,其实就是建立业务描述和真实数据映射关系,题目比较长思路简单。
- 输入一个矩阵,顺时针打印矩阵的数据。也是剑指offer原题
package junit;
public class PrintMatrix {
public static void printCircle(int[][] matrix, int startX, int startY, int endX, int endY) {
// only one column left
if (startY == endY) {
for (int i = startX; i <= endX; i++ ) {
System.out.println(matrix[i][endY]);
}
return;
}
// only one row left
if (startX == endX) {
for (int i = startY; i <= endY; i++ ) {
System.out.println(matrix[startX][i]);
}
return;
}
// 注意交界点不要打印两次
for (int i = startY; i < endY; i++ ) {
System.out.println(matrix[startX][i]);
}
for (int i = startX; i < endX; i++ ) {
System.out.println(matrix[i][endY]);
}
for (int i = endY; i > startY; i-- ) {
System.out.println(matrix[endX][i]);
}
for (int i = endX; i > startX; i-- ) {
System.out.println(matrix[i][startY]);
}
}
public static void printMatrix(int[][] matrix) {
if (matrix == null) {
return;
}
int startX = 0;
int startY = 0;
int endY = matrix[0].length - 1;
int endX = matrix.length - 1;
while ((startX <= endX) && (startY <= endY)) {
printCircle(matrix, startX, startY, endX, endY);
startX++;
startY++;
endX--;
endY--;
}
}
public static void main(String[] args) {
int[][] matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
printMatrix(matrix);
}
}
- 两个版本号比较,就是字符串的一些处理,java 使用split 函数切分比较就轻松完成。这里版本号比较只有数字和. 所以难度比字母存在更低。
package junit;
public class CompareVersion {
public static int compare_string(String str1, String str2) {
// 特殊字符一定要注意转义,是个坑
String[] strs1 = str1.split("\\.");
String[] strs2 = str2.split("\\.");
int i, j;
int num1 ,num2;
for (i = 0, j = 0; i < strs1.length && j < strs2.length; i++, j++) {
num1 = Integer.parseInt(strs1[i]);
num2 = Integer.parseInt(strs2[j]);
if(num1 > num2)
return 1;
if(num1 < num2)
return -1;
}
if (i == strs1.length && j == strs2.length) {
return 0;
}
if (i == strs1.length) {
return -1;
} else {
return 1;
}
}
public static void main(String[] args) {
String version1 = "1.0.1";
String version2 = "1.0";
System.out.println(compare_string(version1, version2));
}
}
其他一些题目也基本都是来自剑指offer。