sword_offer 面试题5:替换空格

本文介绍了如何将字符串中的空格替换成特定字符以及合并两个已排序数组的方法。首先通过一个函数实现了空格替换功能,接着提供了两种不同的策略来合并两个有序数组并保持排序不变。这些技巧对于日常编程任务非常实用。
/**
* 替换空格
* 题目:请实现一个函数,把字符串中的每个空格替换成“%20”,例如
* 输入“we are happy”,则输出“we%20are%20happy”.
*/
public static String replaceBlank(String str){
if (str == null || str.length()<=0) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ' ') {
sb.append(str.charAt(i));
}else {
sb.append("%20");
}
}
return sb.toString();
}
    /**
     * 相關題目:有两个排序的数组A1和A2,请实现一个函数,把A1和A2合并成一个排序数组。
     * 解题思路:1、新建一个辅助数组,长度为A1+A2,把A1和A2中的所有数字先全部放入到辅助数组,然后再进行排序
     * @param arr
     * @param brr
     * @return
     */
public static int[] mergeStr1(int[] arr,int[] brr){
int arrLength = arr.length;
int brrLength = brr.length;
int[] crr = new int[arrLength+brrLength];
if (arr==null || arr.length<=0) {
return brr;
}else if (brr==null || brr.length<=0) {
return arr;
}else if ((arr==null || arr.length<=0) || (brr==null || brr.length<=0)) {
return crr;
}
for (int i = 0; i < crr.length; i++) {
if (i<arrLength) {
crr[i] = arr[i];
}else {
crr[i] = brr[i-arrLength];
}
}
for (int i = 0; i < crr.length; i++) {
for (int j = i+1; j < crr.length; j++) {
if (crr[i]>crr[j]) {
int tmp = crr[i];
crr[i] = crr[j];
crr[j] = tmp;
}
}
}
return crr;
}
/**
* 解法思路2:直接先比较两个数组的值,然后塞入辅助数组
* @param arr
* @param brr
* @return
*/
public static int[] mergeStr2(int[] arr,int[] brr){
int arrLength = arr.length;
int brrLength = brr.length;
int[] crr = new int[arrLength+brrLength];
if (arr==null || arr.length<=0) {
return brr;
}else if (brr==null || brr.length<=0) {
return arr;
}else if ((arr==null || arr.length<=0) || (brr==null || brr.length<=0)) {
return crr;
}
int i=0,k=0,j=0;
while(i<arrLength && j <brrLength){
if (arr[i] <= brr[j]) {
crr[k++] = arr[i++];
}else {
crr[k++] = brr[j++];
}

}

              //这两步操作是把比较完之后,arr或者brr剩余的数依次添加进辅助数组

while(i<arrLength){
crr[k++] = arr[i++];
}
while(j<brrLength){
crr[k++] = brr[j++];
}
return crr;
}
NetMode: Client : BP_Swordsman_C_1 不是本地ROLE_SimulatedProxy NetMode: Client : BP_Swordsman_C_0 是本地ROLE_AutonomousProxy NetMode: Client : BP_Goblin_Sword_C_2 不是本地ROLE_SimulatedProxy NetMode: Client : BP_Goblin_Sword_C_1 不是本地ROLE_SimulatedProxy NetMode: Client : BP_Goblin_Sword_C_0 不是本地ROLE_SimulatedProxy NetMode: ListenServer : BP_Swordsman_C_1 不是本地ROLE_Authority NetMode: ListenServer : BP_Swordsman_C_0 是本地ROLE_Authority NetMode: ListenServer : BP_Goblin_Sword_C_2 不是本地ROLE_Authority NetMode: ListenServer : BP_Goblin_Sword_C_1 不是本地ROLE_Authority NetMode: ListenServer : BP_Goblin_Sword_C_0 不是本地ROLE_Authority 第一步判断是对的 但是后面的不对 我补充一下控制器代码 // 项目权归Dreams所有 #include "Controllers/AIBaseController.h" #include "Perception/AIPerceptionComponent.h" #include "Perception/AISenseConfig_Sight.h" AAIBaseController::AAIBaseController() { AIPerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>("AIPerceptionComponent"); SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>("SightConfig"); SightConfig->DetectionByAffiliation.bDetectEnemies = true; SightConfig->DetectionByAffiliation.bDetectFriendlies = false; SightConfig->DetectionByAffiliation.bDetectNeutrals = false; SightConfig->SightRadius = 1000.f; SightConfig->LoseSightRadius = 1200.f; SightConfig->SetMaxAge(5.f); SightConfig->PeripheralVisionAngleDegrees = 180.f; AIPerceptionComponent->ConfigureSense(*SightConfig); // AIPerceptionComponent->OnTargetPerceptionUpdated.AddDynamic(this, &ACAIController::TargetPerceptionUpdated); // AIPerceptionComponent->OnTargetPerceptionForgotten.AddDynamic(this, &ACAIController::TargetForgotten); } void AAIBaseController::OnPossess(APawn* NewPawn) { Super::OnPossess(NewPawn); SetGenericTeamId(FGenericTeamId(0)); IGenericTeamAgentInterface* PawnTeamInterface = Cast<IGenericTeamAgentInterface>(NewPawn); if (PawnTeamInterface) { /*SetGenericTeamId(PawnTeamInterface->GetGenericTeamId()); ClearAndDisableAllSenses(); EnableAllSenses();*/ PawnTeamInterface->SetGenericTeamId(GetGenericTeamId()); } } void AAIBaseController::BeginPlay() { Super::BeginPlay(); }
08-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值