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: ...
随机推荐
- c++ template不能有cpp
c++的template只能把生命和定义都放在.h文件里,不然会出错
- CycloneII之EDA及学术开发功能描述
1.概述 同Stratix/Cyclone. 2.逻辑单元(Logic Cell)描述 在以前的架构中(比如Cyclone),单个LE包括一个组合逻辑和寄存器.对于Cyclone II来说,组合逻辑和 ...
- php array_push 与 $arr[]=$value 性能比较
1.array_push方法 array_push 方法,将一个或多个元素压入数组的末尾. int array_push ( array &$array , mixed $var [, mix ...
- Python函数 hash()
hash(object) hash() 用于获取取一个对象(字符串或者数值等)的哈希值.返回对象的哈希值. 实例: >>>hash('test') # 字符串 2314058 ...
- Git 的分支和标签规则
Git 的分支和标签规则 分支使用 x.x 命名,不加 V. 标签使用 v1.x.x-xxx 方式命名.(v 为小写) 分支和标签名不可重复.
- python+anaconda+pycharm工具包安装
更新额外包 $ conda update conda 更新pip python -m pip install --upgrade pip 更新所有 conda update --all 安装ffmpe ...
- 什么是spark(二) RDD
其实你会发现很多概念都是基于RDD提出来的,比如分区,缓存这些操作的对象其实都是RDD:所以不要讲spark的分区,这其实很不专业,分区其实是属于RDD的概念(只有pair RDD才有分区概念) RD ...
- display的flex属性使用详解
flex的兼容性在pc端还算阔以,但是在移动端,那就呵呵了.今天我们只是学习学习,忽略一些不重要的东西. 首先flex的使用需要有一个父容器,父容器中有几个items. 父容器:container 属 ...
- Ajax下载
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- 使用GDI+保存带Alpha通道的图像(续)
之前结合网上的一些代码及ATL::CImage的实现,自己写了一个将HBITMAP以PNG格式保存到文件到函数.见上一篇日记. 不过,后来换了个环境又发现了问题,昨天和今天上午把<Windows ...