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: ...
随机推荐
- hadoop入门手册1:hadoop【2.7.1】【多节点】集群配置【必知配置知识1】
问题导读 1.说说你对集群配置的认识?2.集群配置的配置项你了解多少?3.下面内容让你对集群的配置有了什么新的认识? 目的 目的1:这个文档描述了如何安装配置hadoop集群,从几个节点到上千节点.为 ...
- eclipse安装最新版svn
1.下载最新的Eclipse,我的版本是3.7.2 indigo(Eclipse IDE for Java EE Developers)版 如果没有安装的请到这里下载安装:http://ecli ...
- 在linux修改文件夹及其子文件夹的权限。
加入-R 参数,就可以将读写权限传递给子文件夹例如chmod -R 777 /home/mypackage那么mypackage 文件夹和它下面的所有子文件夹的属性都变成了777.777是读.写.执行 ...
- (转)如何制作nodejs,npm “绿色”安装包
摘自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=8625039&id=3817492 由于公司环境 ...
- 关于angular.extend的用法
ng中的ng-function中会有些方法,便于我们进行js代码的编写 关于angular.extend(dst, src);通过从src对象复制所有属性到dst来扩展目标对象dst.你可以指定多个s ...
- RK3288 手动设置电池电量
参考:[RK3288][Android6.0] 调试笔记 --- 电池电量一直显示100% 系统版本:RK3288 android 5.1 (与参考的变量和宏有点区别) 设备没有电池,在进行Fota升 ...
- jeecg中List页面的高级查询
1.普通的高级查询 <t:datagrid name="orderworthList" title="订单价值统计" actionUrl="or ...
- python的mp3play库试用
没有见过比这个更小型的库了,下面程序实现的功能:播放音乐,按空格键实现暂停和播放的切换. #coding=utf-8 import mp3play import pythoncom, pyHook i ...
- my.conf配置大全
[client]port = 3306socket = /tmp/mysql.sock [mysqld]port = 3306socket = /tmp/mysql.sock basedir = /u ...
- mysql索引之五:多列索引
索引的三星原则 1.索引将相关的记录放到一起,则获得一星 2.如果索引中的数据顺序和查找中的排列顺序一致则获得二星 3.如果索引中的列包含了查询中的需要的全部列则获得三星 多列索引 1.1.多个单列索 ...