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

Total Accepted: 4116 Total Submissions: 11368 Difficulty: Easy

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

采用快排的partition函数来对字符串进行翻转

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.

 解题思路一:
简易的哈希表,将每个字符串使用哈希表统计每个字符串中的数据,接着,校验两个哈希表中的字母的数量是否一致,如果所有的都一致,则为true,否则为false。这样,时间复杂度为O(n),空间消耗为256*2,空间复杂度为O(n)
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.
 思路1:
  采用异或操作,相同的字符异或结果为0,因为t是s中的字符随机排列后在随机增加一个字符后的字符串,两个字符串通过异或,最后的结果就是唯一不同的字符;
思路2:
  采用+/-操作,在t字符串中各字符使用+,再- s中字符串中各字符,最后剩下的就是唯一不同的字符;
//采用异或的方法:
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的更多相关文章

  1. LeetCode第[21][23]题(Java):Merge Sorted Lists

    题目:合并两个已排序链表 难度:Easy 题目内容: Merge two sorted linked lists and return it as a new list. The new list s ...

  2. LeetCode之二叉树作题java

    100. Same Tree Total Accepted: 127501 Total Submissions: 294584 Difficulty: Easy Given two binary tr ...

  3. LeetCode之数组处理题java

    342. Power of Four Total Accepted: 7302 Total Submissions: 21876 Difficulty: Easy Given an integer ( ...

  4. 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 + ...

  5. 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 ...

  6. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  7. 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 ...

  8. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  9. LeetCode:字符串的排列【567】

    LeetCode:字符串的排列[567] 题目描述 给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列. 换句话说,第一个字符串的排列之一是第二个字符串的子串. 示例1: ...

随机推荐

  1. (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序

    每次使用 Visual Studio 的模板创建一个 UWP 程序,我们会在项目中发现大量的项目文件.配置.应用启动流程代码和界面代码.然而这些文件在 UWP 程序中到底是如何工作起来的? 我从零开始 ...

  2. psoc4的capsense总结

    psoc4的capsense算是个比较实用的东西,触摸按键,显得有点高大上,呵呵.今天试用了一下,对照着数据手册,现在总结一下. 1,先说原理,官方做文档的时候应该把原理讲一下,不要上来就讲怎么用,怎 ...

  3. 【java多线程】用户线程和守护线程的区别

    java中线程分为两种类型:用户线程和守护线程.通过Thread.setDaemon(false)设置为用户线程:通过Thread.setDaemon(true)设置为守护线程.如果不设置次属性,默认 ...

  4. 使用 commander && inquirer 构建专业的node cli

    备注:   比较简单就是使用nodejs 的两个类库帮助我们进行开发而已,具体的使用参考类库文档 1. 项目初始化 a. 安装依赖 yarn init -y yarn add commander in ...

  5. RedHat6.5用ISO配置yum源

    CentOS自带强大的yum功能,默认为从网上自动下载rpm包,对于网速不太给力或者没有网络的情况下需要用的话就不是很方便,很多软件尤其是服务器上的软件我们么有必要追求最新,稳定性最重要,这里我们用C ...

  6. Spring Cloud 服务网关Zuul

    Spring Cloud 服务网关Zuul 服务网关是分布式架构中不可缺少的组成部分,是外部网络和内部服务之间的屏障,例如权限控制之类的逻辑应该在这里实现,而不是放在每个服务单元. Spring Cl ...

  7. Java报错 -- The public type c must be defined in its own file

    出现The public type c must be defined in its own file这个问题,是由于定义的JAVA类同文件名不一致 你的文件里很可能有两个 public 的类,而Ja ...

  8. Java-Runoob:Java 数组

    ylbtech-Java-Runoob:Java 数组 1.返回顶部 1. Java 数组 数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java 语言 ...

  9. git clone 后使用子分支

    git clone 项目git地址 git branch -a 切换到子分支进行开发 git ckeckout 子分支名称,如:git checkout dev_feature_call git pu ...

  10. TimesTen学习(三)安装、连接、远程连接TimesTen数据库

    TimesTen学习(三)远程连接TimesTen数据库 <TimesTen学习(一)安装篇>:http://blog.itpub.net/23135684/viewspace-71774 ...