面试题之微软 字符串的所有排列

字符串的所有排列

  • 真言

交通发达才能发展,农村修路迫不及待。

  • 题目

给出一个函数来输出一个字符串的所有排列。

  • 思路

思路很简单,时间复杂度O(n*n),思路如下(对于还有相同字符的字符串,答案会有重复的,去重就好了)
  1. 保存一个空的字符串A,
  2. 对给定字符串B的每一个字符
  3. 插入A中,出入的位置有n(n为当前字符串A的长度)+1种情况
  4. 直至A的长度和B相同时,则输出A
举个例子,如下
给定字符串string=“bhe”


  1. 对于三个字符,答案最多就是6个

  • 实验


  • 代码

test.cpp(递归)
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

// function all orders
void All_orders(string A,string B,string::size_type n,ofstream &f);
// function randstring
string RandString();


// function main
int main()
{

	int i = 0;
	while(i<1)
	{
		ofstream f;
		f.open("txt.txt");
		string test = RandString();
		cout<<"test="<<test<<endl;
		All_orders("",test,0,f);
		f.close();
		i++;
	}

	system("pause");
	return 0;
}



// function all orders
void All_orders(string A,string B,string::size_type n,ofstream &f)
{
	if(n < B.length())
	{	
		string next = A;
		next = B[n] + A;
		All_orders(next,B,n+1,f);
		for(string::size_type i = 1;i < A.length();i++)
		{
			next  = A.substr(0,i)+B[n]+A.substr(i,A.length());
			All_orders(next,B,n+1,f);
		}
		if(A.length()>0)
		{
			next = A + B[n];
			All_orders(next,B,n+1,f);
		}
	}
	else if(n == B.length())
	{
		cout<<A<<" "<<endl;
		f<<A<<endl;
	}
}

// function randstring
string RandString()
{
	int size =  4;
	string result = "";
	for(int i = 0; i<size; i++)
	{
		result = result + (char)( 'a' + rand()%10 );
	}
	return result ;
}


test2.cpp(非递归,我喜欢的)
#include <iostream>
#include <string>
#include <queue>
#include <fstream>
using namespace std;

// function all orders
void All_orders(string B);
// function randstring
string RandString();


// function main
int main()
{
	int i = 0;
	while(i<1)
	{
		ofstream f;
		f.open("txt.txt");
		string test = RandString();
		cout<<"test="<<test<<endl;
		All_orders(test);
		f.close();
		i++;
	}

	system("pause");
	return 0;
}



// function all orders
void All_orders(string B)
{
	queue<string> * Q = new queue<string> ;
	string first = "";
	first = first+B[0];
	//cout<<"first="<<first<<endl;
	Q -> push(first);
	string::size_type length ;
	string now ;
	while( Q -> front().length() < B.length() )
	{
		now = Q -> front();
		Q -> pop();
		length = now.length();
		Q->push(B[length]+now);
		for(string::size_type i = 1;i<length;i++)
		{
			Q->push(now.substr(0,i)+B[length]+now.substr(i,length));
		}

		Q->push(now+B[length]);

	}
	ofstream f;
	f.open("text.txt");
	while(!Q->empty())
	{
		cout<<Q->front()<<endl;
		f<<Q->front()<<endl;
		Q->pop();
	}
	f.close();
}

// function randstring
string RandString()
{
	int size =  9;
	string result = "";
	for(int i = 0; i<size; i++)
	{
		result = result + (char)( 'a' + rand()%10 );
	}
	return result ;
}



### 如何将YOLOPyQt结合使用 为了实现YOLOPyQt的集成,可以按照以下方法构建应用程序: #### 1. 安装依赖库 确保安装了必要的Python包。这通常包括`opencv-python`用于图像处理以及`torch`和`tqdm`等其他可能需要的机器学习框架。 ```bash pip install opencv-python torch tqdm pyqt5 ``` #### 2. 加载并配置YOLO模型 加载预训练好的YOLO权重文件,并设置好检测参数。这部分代码可以从官方GitHub仓库获取或者基于已有的YOLO版本调整[^1]。 ```python import cv2 from ultralytics import YOLO model = YOLO('yolov8n.pt') # Load model ``` #### 3. 创建PyQt界面 设计图形用户界面(GUI),允许用户选择视频源或图片路径作为输入给YOLO进行目标识别。这里展示了一个简单的窗口布局例子[^2]。 ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QLineEdit class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("YOLO Object Detection") layout = QVBoxLayout() label = QLabel("Enter image path:") line_edit = QLineEdit() button = QPushButton("Detect Objects!") layout.addWidget(label) layout.addWidget(line_edit) layout.addWidget(button) container = QWidget() container.setLayout(layout) self.setCentralWidget(container) ``` #### 4. 实现对象检测逻辑 当点击按钮时触发事件处理器,在其中调用YOLO来进行预测并将结果显示出来。注意要处理不同类型的媒体数据(如摄像头流、本地文件)[^3]。 ```python def on_button_clicked(self): img_path = self.line_edit.text() # Get input from user results = model(img_path) # Perform inference using loaded model res_plotted = results[0].plot() # Plot bounding boxes over detected objects cv2.imshow("Detected Image", res_plotted) # Show result in a window cv2.waitKey(0) button.clicked.connect(on_button_clicked) ``` 以上就是基本的工作流程;当然实际项目可能会更复杂一些,比如还需要考虑多线程运行以提高性能等问题。对于具体细节上的差异,则取决于所选用的具体YOLO变体及其对应的API接口文档说明[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值