小编tom*_*h13的帖子

数据结构面试问题

我被问到以下问题:您将如何存储下面给出的数据(您将选择哪种数据结构):

A-1-2-3-4-5-6

|

B-7-8-9-10-11

|

C-12-14-15-16-17
Run Code Online (Sandbox Code Playgroud)

我的答案:因为这看起来像一堆列表,其头节点链接在一起.使用两种节点类型,一种是常规节点类型,具有以下定义:

Struct node1
{
int val;
struct node*next;
};
// to store the numerical part of the data

struct node2
{
 int val;
struct node *down;
struct node* next;
};
//this is the heads of each list.. for the alphabet part of the question.
Run Code Online (Sandbox Code Playgroud)

采访者的反应:这是你能想到的最好的数据结构吗?每个节点所需的遍历和内存如何?

我对此的回答:如果我们创建某种哈希表,我们可以更好地遍历.

我向你们提问的同志:我们能做得更好吗?是否有更好的方法来存储此类数据?

我们假设数据是所有数字(甚至是每个头节点的数字)和非连续的数据,可能重复.什么是正确的答案?在C/C++中寻找答案

data-structures

3
推荐指数
1
解决办法
5510
查看次数

使用反斜杠的grep问题

我有以下文件:

asdasd
asd
asd
incompatible:svcnotallowed:svc\:/network/bmb/clerver\:default
incompatible:svcnotallowed:svc\:/network1/bmb/clerver\:default
incompatible:svcnotallowed:svc\:/network2/bmb/clerver\:default
asdasd
asd
asd
as
Run Code Online (Sandbox Code Playgroud)

现在假设我有两个变量v1="incompatible:svcnotallowed:"v2="svc\:/network1/bmb/clerver\:default".我想使用v1和v2搜索整个文件.我知道这是一个问题,因为文件中有一个'\'.我只是不知道如何消除它.我尝试使用单引号存储v1和v2(可变内容和grep用法),但是徒劳无功.

这是我尝试过的一系列命令:

grep "$v1$v2" file
grep '$v1$v2' file
Run Code Online (Sandbox Code Playgroud)

我需要这个在KSH中工作, 请让我知道在这种情况下使用grep的正确方法.谢谢.

grep ksh pattern-matching

3
推荐指数
1
解决办法
2352
查看次数

main()内部的struct定义导致Segmentation Fault

是不可能在main()中定义结构.我尝试以下只是为了获得分段错误:

#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#define TRUE 1


void main(int argc,char **argv)
{
struct test_struct
{

        char test_name[50];
        char summary_desc[200];
        char result[50];
};

struct suite_struct
{
        char suite_name[50];
        struct test_struct test[500];
        int test_count;
        int passed;
        int failed;
        int unresolved;
        int notrun;
}suite[500];

        int a,b;

        for (a=0;a<500;a++)
        {
                strcpy(suite[a].suite_name,"");
                for (b=0;b<500;b++)
                {
                        strcpy(suite[a].test[b].test_name,"");
                        strcpy(suite[a].test[b].summary_desc,"");
                        strcpy(suite[a].test[b].result,"");
                }
                suite[a].test_count=0;
                suite[a].passed=0;
                suite[a].failed=0;
                suite[a].unresolved=0;
                suite[a].notrun=0;
        }
}
Run Code Online (Sandbox Code Playgroud)

但是当我在其外部采用结构定义时,它的工作原理是:

#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#define TRUE 1


struct test_struct 
{ 

        char test_name[50]; 
        char …
Run Code Online (Sandbox Code Playgroud)

c struct solaris cc sunstudio

3
推荐指数
1
解决办法
8539
查看次数

一种递归方法,用于查找任何(不一定是完整的)二叉树的深度

我试图以递归方式计算O(log n)时间内任何(不一定是完整的)BST的深度.

这是我提出的算法:

//max() - returns the max of two numbers
int depth(root)
{
    if(root->left==NULL && root->right==NULL) //leaf
        return 0;
    else if(root->left!=NULL && root->right==NULL) //with right leaf
        return( max(depth(root->left),0)+1);
    else if(root->left==NULL && root->right!=NULL) //with left leaf
        return( max(0,depth(root->right)+1);
    else if(root->left->left==NULL && root->left->right==NULL && root->right->left==NULL&&root->right->right==NULL) // this a parent of two leaves
        return 1; 
    else// for the condition that this a parent of two sub roots
        return( max(depth(root->right),depth(root->left))+1);
}
Run Code Online (Sandbox Code Playgroud)

这个算法在O(log n)时间内计算深度是否合适?

有没有更好的办法?

c tree data-structures

2
推荐指数
1
解决办法
5124
查看次数

关于KornShell中的'for'循环

有没有办法在KornShell(ksh)中使用'for'来实现以下内容?这是C等价物:

for(i=1;i<20;i++)
{
    printf("%d",i);
}
Run Code Online (Sandbox Code Playgroud)

我想知道这是否可以使用'for'而不是'while'来实现

我试过以下,似乎没有用.

for i in [1-20]
    do

    print $i
    done
Run Code Online (Sandbox Code Playgroud)

请让我知道您的想法和解决方案.

shell scripting ksh for-loop

2
推荐指数
3
解决办法
1万
查看次数

super.onSaveInstanceState()在重写的onSaveInstanceState()中做了什么

我是Android开发的新手.我使用覆盖版本onSaveInstanceState()来保存我自己的应用数据.我注意到我没有super.onSaveInstanceState(savedInstanceState)在我的函数内部调用,代码运行正常.

@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // TODO:
        // Save state information with a collection of key-value pairs
        // 4 lines of code, one for every count variable
        savedInstanceState.putInt(CREATE_KEY, mCreate);
        savedInstanceState.putInt(RESUME_KEY, mResume);
        savedInstanceState.putInt(RESTART_KEY, mRestart);
        savedInstanceState.putInt(START_KEY, mStart);
    }
Run Code Online (Sandbox Code Playgroud)

我想知道是否super.onSaveInstanceState(savedInstanceState)隐含地调用了?

另外,super.onSaveInstanceState(savedInstanceState)在重写函数内部调用的目的是什么.

java android android-activity

2
推荐指数
1
解决办法
1346
查看次数

基于页眉和页脚模式在python中搜索文件

我想解析一个看起来像这样的文件:

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA


AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
HEADER
body
body
body
FOOTER
BLABLABLABLA
BLABLABLABLA
BLABLABLABLA
Run Code Online (Sandbox Code Playgroud)

我想提取HEADER和FOOTER之间存在的内容.每个HEADER和FOOTER之间的行数可以有所不同,内容本身也可以编写以下代码来提取:

   fd=open(file,"r")
    for line in fd:
        if not start_flag:
            match = re.search(r'.*HEADER.*',line)
            if not match:
                continue
            else:
                body=body+line+"\n"
                start_flag=True
        else:
            match_end = re.search(r'.*FOOTER.*',line)
            if not match_end:
                body=body+line+"\n"
                continue
            else:
                body=body+line+"\n\n"
                break
   print body
Run Code Online (Sandbox Code Playgroud)

这是使用python从文件中提取内容的最佳方法吗?有什么其他方法可以解决这个问题?

python

0
推荐指数
1
解决办法
735
查看次数