212. Space Replacement【LintCode by java】

Description

Write a method to replace all spaces in a string with %20. The string is given in a characters array, you can assume it has enough space for replacement and you are given the true length of the string.

You code should also return the new length of the string after replacement.

If you are using Java or Python,please use characters array instead of string.

Example

Given "Mr John Smith", length = 13.

The string after replacement should be "Mr%20John%20Smith", you need to change the string in-place and return the new length 17.

Challenge

Do it in-place.

解题:给一个字符数组,原地将空格换成  %20 。因为是原地转换,不能申请额外的空间,那么只能一步一步往后移动了。因为是把空格转换成“%20”,空格原本占用一个单位,现在需要占用三个单位,只要把原来空格的后面所有的数都向后移动两格即可。在移动的过程中,length也随之变化。代码如下:

 1 public class Solution {
 2     /*
 3      * @param string: An array of Char
 4      * @param length: The true length of the string
 5      * @return: The true length of new string
 6      */
 7     public int replaceBlank(char[] string, int length) {
 8         // write your code here
 9         for(int i = 0; i < length; ){
10             //如果发现空格
11             if(string[i] == ‘ ‘){
12                 for(int j = length-1; j >= i+1; j--){
13                     string[j+2] = string[j];
14                 }
15                 string[i++] = ‘%‘;
16                 string[i++] = ‘2‘;
17                 string[i++] = ‘0‘;
18                 length = length+2;
19             }else{
20                 i++;
21             }
22         }
23         return length;
24     }
25 }

原文地址:https://www.cnblogs.com/phdeblog/p/9220073.html

时间: 2024-08-30 06:14:14

212. Space Replacement【LintCode by java】的相关文章

156. Merge Intervals【LintCode by java】

Description Given a collection of intervals, merge all overlapping intervals. Example Given intervals => merged intervals: [ [ (1, 3), (1, 6), (2, 6), => (8, 10), (8, 10), (15, 18) (15, 18) ] ] Challenge O(n log n) time and O(1) extra space. 题意:给定一个

158. Valid Anagram【LintCode by java】

Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification What is Anagram? Two strings are anagram if they can be the same after change the order of characters. Example Given s = "abcd", t = "dcab&q

165. Merge Two Sorted Lists【LintCode by java】

Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order. Example Given 1->3->8->11->15->null,

172. Remove Element【LintCode by java】

Description Given an array and a value, remove all occurrences of that value in place and return the new length. The order of elements can be changed, and the elements after the new length don't matter. Example Given an array [0,4,4,0,0,2,4,4], value

175. Invert Binary Tree【LintCode by java】

Description Invert a binary tree. Example 1 1 / \ / 2 3 => 3 2 / 4 4   解题:题目要求讲二叉树的左子树和右子树对调一下,用递归来做很简单: 1 /** 2 * Definition of TreeNode: 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left, right; 6 * public TreeNode(int val) {

181. Flip Bits【LintCode, by java】

Description Determine the number of bits required to flip if you want to convert integer n to integer m. Both n and m are 32-bit integers. Example Given n = 31 (11111), m = 14 (01110), return 2. 解题:比较两个整数对应的二进制数,共有多少位不同.注意,负数也包含在内.">>>"

MongDB基础学习(七)—— 【MongoDB for Java】Java操作MongoDB

[MongoDB for Java]Java操作MongoDB 开发的产品为了融资,不停得改版,从第一版到现在最新版本,最后发现公司发展方向都变了,有最初电子商务改成VR内容提供者(没办法,要别人钱,就得按照别人的规划的战略走).本来本章节会放到后面再做讲解,无奈,部门需要做一次培训任务,我就想到拿Java操作MongoDB作为培训内容,开发环境和依赖jar如下: (1)开发环境: System:Windows IDE:eclipse Database:mongoDB2.6 Maven:apac

Java编程思想【Thinking in java】

Java编程思想[Thinking in java]目录:第1章 对象导论1.1抽象过程1.2每个对象都有一个接口1.3每个对象都提供服务1.4被隐藏的具体实现1.5复用具体实现1.6继承1.6.1“是一个”(is-a)与“像是一个”(is-like-a)关系1.7伴随多态的可互换对象1.8单根继承结构1.9容器1.9.1参数化类型(范型)1.10对象的创建和生命周期1.11异常处理:处理错误1.12并发编程1.13Java与Internet1.13.1Web是什么1.13.2客户端编程1.13

【leetcode with java】18 4Sum (On^2)

我看了几个人气比较高的博客,他们这个算法都没做到O(n^2),所以提前将我的解法贴出来分享,供大家参考(前面略过的题目近期都会补上的). [题目]: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target