LeetCode之字符串处理题java
344. Reverse String
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Subscribe to see which companies asked this question
public class Solution {
public String reverseString(String s) {
if(s==null)
return "";
char c[] = s.toCharArray();
int len = s.length();
int i=0;
int j=len-1;
while(i<j){
char tmp = c[i];
c[i] = c[j];
c[j] = tmp;
++i;
--j;
}
return new String(c);
}
}
345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Subscribe to see which companies asked this question
public class Solution {
public String reverseVowels(String s) {
if(s==null){
return "";
}
char[] c = s.toCharArray();
int left = 0;
int right = c.length-1;
while(left<right){
while(left<right&&!isVowel(c[left])){
++left;
}
while(left<right&&!isVowel(c[right])){
--right;
}
char tmp = c[left];
c[left] = c[right];
c[right] = tmp;
++left;
--right;
}
return new String(c);
}
//检查一个字符是否是元音字符
public boolean isVowel(char c){
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
return true;
else
return false;
}
}
168. Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB 解题思路:26进制操作
当n=1时,(n-1)%26+'A'='A';
当n=26时,(n-1)%26+'A'='Z';
当n=27时,进1,进位carry = (n-1)/26=1-->‘A’;依次循环
public class Solution {
public String convertToTitle(int n) {
StringBuilder str = new StringBuilder();
if (n<=0){
return " ";
}
while(n!=0){
str.append((char)((n-1)%26 + 'A'));
n = (n-1)/26;
}
return str.reverse().toString();
}
}
242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
public boolean isAnagram(String s, String t) {
if(s==null||t==null)
return false;
int count_s[] = count(s);
int count_t[] = count(t);
for(int i=0;i<count_t.length;i++){
if(count_t[i]==count_s[i]){
continue;
}else{
return false;
}
}
return true;
} public int[] count(String str){
if(str==null&&str.length()==0){
return null;
}
int[] count = new int[256];
for(int i=0;i<str.length();i++){
count[str.charAt(i)]++;
}
return count;
}
389. Find the Difference
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:
Input:
s = "abcd"
t = "abcde" Output:
e Explanation:
'e' is the letter that was added.
//采用异或的方法:
public class Solution {
public char findTheDifference(String s, String t) {
char c = 0;
for(int i=0;i<s.length();i++){
c ^= s.charAt(i);
}
for(int i=0;i<t.length();i++){
c ^= t.charAt(i);
}
return c;
}
}
//采用+/-的方式
public char findTheDifference(String s, String t) {
char res = t.charAt(t.length() - 1);
for (int i = 0; i < s.length(); i++) {
res += t.charAt(i);
res -= s.charAt(i);
}
return res;
}
383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true 思路:统计字符的个数,ransomNote中的各个字符总数是否<=magazine中对应的字符总数;
public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] count = new int[26];
for(char c:magazine.toCharArray()){
count[c-'a']++;
}
for(char c:ransomNote.toCharArray()){
if(--count[c-'a']<0)
return false;
}
return true;
}
}
387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode"
return 0. s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.
思路1:1)首先都是小写字母,则使用数组统计每个字符出现的次数;
2)再次从头到尾遍历字符串,将字符出现次数为1的字符(首次出现)的下标返回;
public class Solution {
public int firstUniqChar(String s) {
if(s==null||s.length()==0)
return -1;
int[] count = new int[26];//统计次数
for(int i=0;i<s.length();i++){
count[s.charAt(i)-'a']++;
}
for(int i=0;i<s.length();i++){
if(count[s.charAt(i)-'a']==1)
return i;
}
return -1;
}
}
LeetCode之字符串处理题java的更多相关文章
- LeetCode第[21][23]题(Java):Merge Sorted Lists
题目:合并两个已排序链表 难度:Easy 题目内容: Merge two sorted linked lists and return it as a new list. The new list s ...
- LeetCode之二叉树作题java
100. Same Tree Total Accepted: 127501 Total Submissions: 294584 Difficulty: Easy Given two binary tr ...
- LeetCode之数组处理题java
342. Power of Four Total Accepted: 7302 Total Submissions: 21876 Difficulty: Easy Given an integer ( ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- LeetCode:字符串的排列【567】
LeetCode:字符串的排列[567] 题目描述 给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列. 换句话说,第一个字符串的排列之一是第二个字符串的子串. 示例1: ...
随机推荐
- IIS并发瓶颈线程数的限制
.NET线程池最大线程数的限制-记一次IIS并发瓶颈 https://www.cnblogs.com/7rhythm/p/9964543.html .NET ThreadPool 最大线程数的限制 I ...
- JavaScript for 循环累加 json 字符串
var msg = {"status":1,"data":[{"id":"12","words":& ...
- Request对象主要用于获取来自客户端的数据,如用户填入表单的数据、保存在客户端的Cookie等。
1.主要属性 ApplicationPath 获取服务器上asp.net应用程序的虚拟应用程序根路径 Browser 获取有关正在请求的客户端的浏览器功能的信息,该属性值为:HttpBrows ...
- functool.wraps and functools.partial
functools.partial 通过包装手法,允许我们 "重新定义" 函数签名. 通常是将函数的部分参数给固定下来, 从而形成一个输入参数更少的新函数. functool.w ...
- C/C++中一些不太注意到的小知识点--[锦集]
C/C++中一些不太注意到的小知识点--[锦集] C/C++小知识点--[锦集] "="和"<=" 的优先级 1.( (file_got_len = re ...
- 在vue中无论使用router-link 还是 @click事件,发现都没法从列表页点击跳转到内容页去
在vue中如论使用router-link 还是 @click事件,发现都没法从列表页点击跳转到内容页去,以前都是可以的,想着唯一不同的场景就是因为运用了scroll组件(https://ustbhua ...
- 【备忘录】Sublime Text编辑器如何在选中的多行行首增加字符串
如题:上面的代码,想在每一行的开头加上一个字符 * 如下: 操作步骤如下: 1.选中要操作的行(我这里Ctrl+A) 2.Ctrl+Shift+L (待操作状态) 3.方向键← (操作这步骤后,可 ...
- MFC程序如何修改icon图标
场景: Visual Studio写MFC应用程序,默认的程序左上角图标是自带的(如下图),虽说也不丑,但是对于程序员来说,还是缺乏个性了. 你知道,C.C++.java系程序员最常干的事情就是定义. ...
- C++11标准库中cstdio头文件新增的5个格式化I/O函数学习
刚开始学网络编程,稍微扩展书上的简单C/S程序时,发现以前太忽略标准I/O这一块,查官网发现C++11新增了几个格式化I/O函数. snprintf 将格式化输出写入到有大小限制的缓存中 vfs ...
- String与StringBuffer效率的比较
String str = “”; for (int i=0; i<100; i++) str += “a”; 可是你知道在内存中会产生多少的垃圾出来吗?总共会有a.aa.aaa. aaa….,无 ...