当前位置: 首页 > news >正文

LeetCode面试题 01.03 URL化

题目
解答

package leetcode.editor.cn;//leetcode submit region begin(Prohibit modification and deletion)
class Solution {public String replaceSpaces(String S, int length) {if (S == null || S.isEmpty() || length <= 0) {return "";}StringBuilder sb = new StringBuilder(length);for (int i = 0; i < length; ++i) {if (S.charAt(i) == ' ') {sb.append("%20");} else {sb.append(S.charAt(i));}}return sb.toString();}
}
//leetcode submit region end(Prohibit modification and deletion)

测试用例

package leetcode.editor.cn;import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;public class SolutionTest {private Solution s = null;@Beforepublic void setUp() throws Exception {s = new Solution();}@Testpublic void test1() {String result = s.replaceSpaces("Mr John Smith        ", 13);Assert.assertEquals("Mr%20John%20Smith", result);}@Testpublic void test2() {String result = s.replaceSpaces("               ", 5);Assert.assertEquals("%20%20%20%20%20", result);}
}
http://www.dtcms.com/a/183915.html

相关文章:

  • [Java][Leetcode middle] 80. 删除有序数组中的重复项 II
  • 【Linux网络】HTTP
  • 嵌入式硬件篇---无线通信模块
  • 游戏引擎学习第272天:显式移动转换
  • 【软件工程】基于机器学习的多缺陷定位
  • 【Linux系列】bash_profile 与 zshrc 的编辑与加载
  • 一周内学完计算机网络课程之二:计算机网络物理层的理解
  • Linux电源管理(9)_wakelocks
  • 【计算机视觉】OpenCV实战项目 :Image_Cartooning_Web_App:基于深度学习的图像卡通化
  • 【Linux 系统调试】系统级追踪与性能分析利器----LTTng
  • Java反序列化漏洞
  • python:trimesh 用于 STL 文件解析和 3D 操作
  • 《用MATLAB玩转游戏开发》Flappy Bird:小鸟飞行大战MATLAB趣味实现
  • 【Leetcode 每日一题】1550. 存在连续三个奇数的数组
  • android-ndk开发(12): 获取ndk内置clang的版本详情
  • spark-Schema 定义字段强类型和弱类型
  • TCP/IP 模型每层的封装格式
  • leetcode 15. 三数之和
  • 【25软考网工】第六章(3)数字签名和数字证书
  • 在C++中,符号位是否参与位运算
  • 使用vue3-seamless-scroll实现列表自动滚动播放
  • 【通讯录教程】如何将号码快速导入手机通讯录,支持苹果和安卓手机,一次性导入大量号码进入手机通讯录,基于WPF的解决方案
  • 实战项目5(08)
  • 1688 API 自动化采集实践:商品详情实时数据接口开发与优化
  • 每日c/c++题 备战蓝桥杯(洛谷P1115 最大子段和)
  • 滑动窗口——将x减到0的最小操作数
  • 自然语言处理NLP中的连续词袋(Continuous bag of words,CBOW)方法、优势、作用和程序举例
  • 嵌入式硬件篇---IIC
  • Linux:43线程封装与互斥lesson31
  • upload-labs靶场通关详解:第五关