面试程序题

齐岳 2013-08-19 10:52:55
一、写一个函数 传入一个数组和N 要求把前N个元素移到最后 需要占用最少内存
比如传入[a b c d e], N=2, 要求返回数组[c d e a b]

二、in case you are not familiar with regular expression substitution, here's a quick tutorial:

the syntax of string substitution is:
VARIABLE =~ s/SEARCH_PATTERN/NEW_STRING/

For example,
$a = 'abc,123';
$a =~ s/(\w+),(\w+)/\2,\1/; # $a is now '123,abc' because \1='abc' and \2='123'

Here's the question:
write ONE substitution statement(ie. s/SEARCH_PATTERN/NEW_STRING/) so that
"<date>1999-02-25</date>" will be updated to "<date>02-25-1999</date>" AND
"<date>2005-11-03</date>" will be updated to "<date>11-03-2005</date>"
...全文
249 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhy705633103 2013-10-09
  • 打赏
  • 举报
回复
我用循环链表实现的,对时间复杂度和空间复杂度,不是很了解
#include<iostream>
using namespace std;

class list
{
public:
	static list *head;
	static list *p;
	char date;
	list *next;
		static void * operator new(unsigned int size);

	 void zhuangdong(list *plist,int n)
	 {
		 int i =0;
		while(i!=n)
		{
			p = p->next;
			i++;
		}
	 }
	 void show()
	 {
		 for(int i = 0;i!=5;i++)
		 {
			 cout<<p->date<<endl;
			 p = p->next;
		 }
	 }
};
void * list::operator new(unsigned int size)
{
	

		list *p1,*p2;
		head = static_cast<list *>(::operator new(sizeof(list)));
		p1 = head;
		head->date = 'a';
		p = head;
		for(int i = 0;i<4;i++)
		{
			p2 = static_cast<list *>(::operator new(sizeof(list)));
			p1->next = p2;
			switch(i)
			{
			case 0:p2->date = 'b';break;
			case 1:p2->date = 'c';break;
			case 2:p2->date = 'd';break;
			case 3:p2->date = 'e';break;
			default:break;
			}
			p1 = p2;

			
		}
		p1->next = head;
		return head;

		
		
	 
}

list *list::head=NULL;
list *list::p=NULL;
int main()
{
	list *p = new list;
	p->show();
	p->zhuangdong(p,3);
	p->show();
}
马达马达达 2013-08-20
  • 打赏
  • 举报
回复
$a = '1999-02-25'; $a =~ s/(\w+)-(\w+)-(\w+)/\2-\3-\1/; 乱猜的
pehaps 2013-08-20
  • 打赏
  • 举报
回复
一. 题目相当于循环左移n位。移位问题可以转换为两次反转交换问题。 如: 比如传入[a b c d e], N=2, 要求返回数组[c d e a b] 第一步: 把ab反转交换为ba 把cde反转交换为edc 字符串变为baedc 第二部: 把整个字符反转交换 变为:cdeab。 空间复杂度为O(1) 时间复杂度为O(n) 代码如下:


void swap(int A[],int n,int k)
{
  for(int i=0,j=k-1;i<j;i++,j--)
  {
      int temp=A[i];
      A[i]=A[j];
      A[j]=A[i];
   }
   for(int i=k,j=n-1;i<j;i++,j--)
  {
      int temp=A[i];
      A[i]=A[j];
      A[j]=A[i];
   }
    for(int i=0,j=n-1;i<j;i++,j--)
  {
      int temp=A[i];
      A[i]=A[j];
      A[j]=A[i];
   }

)

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧