【LeetCode】345. Reverse Vowels of a String
发布时间:2020-12-15 07:31:36 所属栏目:Java 来源:网络整理
导读:Difficulty: Easy ?More: 【目录】LeetCode Java实现 Description https://leetcode.com/problems/reverse-vowels-of-a-string/ Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello"Output:
Difficulty: Easy?More:【目录】LeetCode Java实现 Descriptionhttps://leetcode.com/problems/reverse-vowels-of-a-string/ Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello"
Output: "holle"
Example 2: Input: "leetcode"
Output: "leotcede"
? IntuitionUsing two pointers. ? Solutionprivate final static HashSet<Character> vowels = new HashSet<Character> ( Arrays.asList(‘a‘,‘e‘,‘i‘,‘o‘,‘u‘,‘A‘,‘E‘,‘I‘,‘O‘,‘U‘)); public String reverseVowels(String s) { if(s==null || s.length()<=0) return s; int i=0,j=s.length()-1; char[] chars = new char[s.length()]; while(i<=j){ char ci=s.charAt(i); char cj=s.charAt(j); if(!vowels.contains(ci)) chars[i++]=ci; else if(!vowels.contains(cj)) chars[j--]=cj; else{ chars[i++]=cj; chars[j--]=ci; } } return String.valueOf(chars); }
ComplexityTime complexity :?O(n) Space complexity :??O(1) What I‘ve learned1.The use of two pointers. ? ?More:【目录】LeetCode Java实现 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在CXF wsdl2java中设置Java合规性级别
- 10个Java程序员熟悉的面向对象设计原则
- Hibernate cache.provider_class属性:设置二级缓存实现类的
- java – keytool -genkey错误:密钥库文件不存在
- java – 自定义的Jackson HttpMessageConverter不再适用于S
- Jstl标签库
- java – 使用org.apache.http发送带有SOAP操作的HTTP Post请
- 具有货币符号的Java String.format
- java dom4j解析xml用到的几个方法
- Java 理论与实践: JDK 5.0 中更灵活、更具可伸缩性的锁定机