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. Oracle中Inner join和Where的区别

    1 .Where子句中使用的连接语句,在数据库语言中,被称为隐性连接.Inner join--on子句产生的连接称为显性连接.(其他Join参数也是显性连接)Where 和Inner join产生的连 ...

  2. ecmall挂件开发实例二(转)

    下述例子讲述了快速增加挂件的方法,但对系统中的代码未做更改,基本参照image_ad挂件的方法. 第 一步: 了解首页模板结构 首页文 件结构 include hearder.html 包含的头文件, ...

  3. 13.Python接口自动化测试 -- 豆瓣

    1.代码如下所示: import requests,unittest import json import HTMLTestRunner class TestDouBan(unittest.TestC ...

  4. win7环境下,golang thrift demo代码编译不通过

    用官方的教程代码:http://thrift.apache.org/tutorial/go 用网友提供的代码:Golang RPC 之 Thrift 都出现如下情况 状况1: 编辑器中就会提醒 Can ...

  5. NetCore 下集成SignalR并进行分组处理

    Tips: 1.注意跟普通版Net.MVC的前端处理方式不一样,以前可以connection.start()后直接done里面再做逻辑处理,现在不行了 建议做法是在具体的业务Hub里重写OnConne ...

  6. js 日期正则 转载

    天秤水的Blog 博客园 博问 闪存 首页 新随笔 联系 管理 订阅 随笔- 70  文章- 0  评论- 74    最强日期正则表达式   一.简单的日期判断(YYYY/MM/DD):  ^\d{ ...

  7. Eclipse中添加web dynamic project【菜鸟学JAVA】

    很多eclipse版本是不能直接新建web dynamic project的,需要从网上找插件或更新.我的Eclipse的版本是(Version: 3.7.0) 比较方便的是在Help → Insta ...

  8. 关于FPGA电源精度要求

    FPGA对DC-DC精度的要求不断提升 FPGA厂商不断采用更先进的工艺来降低器件功耗,提高性能,同时FPGA对供电电源的精度要求也越加苛刻,电压必须维持在非常严格的容限内,如果供电电压范围超出了规范 ...

  9. Python项目打包成exe文件

    这里我们使用pyinstaller这个软件即可,使用pip即可完美安装,在要打包的程序目录下打开cmd输入 pyinstaller -F 文件名.py 即可成功,运行成功后生成一个dict文件夹,东西 ...

  10. 前端必须会的!!!关于对HTTP协议的理解、HTTP协议原理分析

    http协议学习系列 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web C ...