M面经Prepare: Delete Words Starting With One Character
with One delete words character 面经 starting prepare
2023-09-11 14:14:07 时间
给定一个char array, 这个array是一个句子,然后给定一个字母,把这个array里面带有这个字母开头的单次删掉,操作是要求in place.
检测 array[i]==' ' && i<array.length-1 && array[i+1]==target,这种情况,设置j从i+1开始直到找到下一个‘ ’或者到达末尾,然后中间的全部删掉,i从j开始
特别要注意第一个char是target的情况
1 package DeleteWordsStartByCertainChar; 2 3 import java.util.*; 4 5 public class Solution { 6 public char[] modify(char[] array, char tar) { 7 if (array==null || array.length==0) return new char[0]; 8 int index=0; 9 int i=0; 10 for (; i<array.length; i++) { 11 if (i==0 && array[i]==tar || array[i]==' ' && i<array.length-1 && array[i+1]==tar) { 12 int j = i+1; 13 while (j<array.length && array[j]!=' ') j++; 14 i = j; 15 i--; 16 } 17 else array[index++] = array[i]; 18 } 19 char[] res = Arrays.copyOf(array, index); 20 return res; 21 } 22 23 24 /** 25 * @param args 26 */ 27 public static void main(String[] args) { 28 // TODO Auto-generated method stub 29 Solution sol = new Solution(); 30 char[] array = "ab big ball bis brunning b ".toCharArray(); 31 char[] res = sol.modify(array, 'b'); 32 System.out.println(res); 33 } 34 35 }
相关文章
- How to deal with "Could not find component on update server. Contact VMware Support or your system administrator." in Vmware.
- [React] Improve developer experience for accessing context with a custom React hook
- [Javascript] Using JSHint for Linting with Gulp
- [Node.js] Mock an API for Local Development in React with Mirage JS
- [DevOps] Set up and run a PostgreSQL instance locally with Docker Compose
- [TypeScript] Work with DOM Elements in TypeScript using Type Assertions
- [Bash] Move and Copy Files and Folders with Bash
- [Python] Indexing An Array With Another Array with numpy
- [Ionic2] Device Interaction in an Ionic App with Cordova Plugins
- [D3] 10. Creating Axes with D3
- Android Studio 解决报错More than one file was found with OS independent path ‘lib/arm64-v8a/libuvc.so‘
- 解决docker network create --subnet报错的问题:Error response from daemon: Pool overlaps with other one on th
- error C2338: You've instantiated std::aligned_storage<Len, Align> with an extended alignment (in other words, Align >
- How to bind multiple properties with formatter on one control from Ke
- How to bind multiple properties with formatter on one control
- 成功解决If your current network has https://www.anaconda.com blocked, please filea support request with
- Paper之CV之FR&FA:《One Millisecond Face Alignment with an Ensemble of Regression Trees》的翻译与解读
- Learn to securely share files on the blockchain with IPFS!
- Diffusion-GAN: Training GANs with Diffusion 解读