当前位置: 首页 > 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);}
}

相关文章:

  • [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++中,符号位是否参与位运算
  • 中国-拉共体成员国重点领域合作共同行动计划(2025-2027)
  • 西北大学副校长成陕西首富?旗下巨子生物去年净利超20亿,到底持股多少
  • 融创中国:境外债务重组计划聆讯定于9月15日召开
  • 北洋“修约外交”的台前幕后——民国条约研究会档案探研
  • 最高降价三成,苹果中国iPhone开启大促销,能拉动多少销量?
  • 宁德时代港股募资预计最高至50亿美元:90%将投向匈牙利项目