刷题02
有效的字母异位词
class Solution {
public boolean isAnagram(String s, String t) {
int[] record=new int[26];
for(char c:s.toCharArray())
{
record[c-'a']++;
}
for(char c:t.toCharArray())
{
record[c-'a']--;
}
for(int i=0;i<record.length;i++)
{
if(record[i]!=0){
return false;
}
}
return true;
}
}
两个数组的交集
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if(nums1==null||nums2==null) return null;
List<Integer> list=new LinkedList<>();
Set<Integer> x=new HashSet<>();
Set<Integer> sum=new HashSet<>();
for(int temp:nums2)
{
x.add(temp);
}
for(int temp:nums1)
{
if(x.contains(temp))
{
sum.add(temp);
}
}
for(int temp:sum)
{
list.add(temp);
}
return list.stream().mapToInt(Integer::valueOf).toArray();
}
}
快乐数
class Solution {
public boolean isHappy(int n) {
Set<Integer> x=new HashSet<>();
while(n!=1&&!x.contains(n))
{
x.add(n);
n=getNextNumber(n);
}
return n==1;
}
public int getNextNumber(int n)
{
int result=0;
while(n!=0)
{
int temp=n%10;
n=n/10;
result=result+temp*temp;
}
return result;
}
}
两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res=new int[2];
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<nums.length;i++)
{
int temp=target-nums[i];
if(map.containsKey(temp))
{
res[0]=i;
res[1]=map.get(temp);
}
map.put(nums[i],i);
}
return res;
}
}
赎金信
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] record=new int[26];
for(char c:ransomNote.toCharArray())
{
record[c-'a']++;
}
for(char c:magazine.toCharArray())
{
record[c-'a']--;
}
for(char c:ransomNote.toCharArray())
{
if(record[c-'a']>0)
{
return false;
}
}
return true;
}
}
反转字符串
class Solution {
public void reverseString(char[] s) {
for(int i=0,j=s.length-1;i<=j;i++,j--)
{
char temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
替换空格
class Solution {
public String replaceSpace(String s) {
if(s==null) return null;
StringBuilder str=new StringBuilder();
for(char c:s.toCharArray())
{
if(c==' ')
{
str.append("%20");
}
else
{
str.append(c);
}
}
return str.toString();
}
}
反转字符串
class Solution {
public String reverseWords(String s) {
StringBuilder sb=removeSpace(s);
reverse(sb,0,sb.length()-1);
reverseEach(sb);
return sb.toString();
}
public StringBuilder removeSpace(String s)
{
int start=0;
int end=s.length()-1;
while(s.charAt(start)==' ') start++;
while(s.charAt(end)==' ') end--;
StringBuilder sb=new StringBuilder();
for(int i=start;i<=end;i++)
{
char c=s.charAt(i);
if(c!=' '||sb.charAt(sb.length()-1)!=' ')
{
sb.append(c);
}
}
return sb;
}
public void reverse(StringBuilder sb,int start,int end)
{
while(start<=end)
{
char temp=sb.charAt(start);
sb.setCharAt(start,sb.charAt(end));
sb.setCharAt(end,temp);
start++;
end--;
}
}
public void reverseEach(StringBuilder sb)
{
int start=0;
int end=1;
while(start<sb.length())
{
while(end<sb.length()&&sb.charAt(end)!=' ')
{
end++;
}
reverse(sb,start,end-1);
start=end+1;
end=start+1;
}
}
}
字符串左旋
class Solution {
public String reverseLeftWords(String s, int n) {
StringBuilder sb=new StringBuilder(s);
reverse(sb,0,n-1);
reverse(sb,n,s.length()-1);
reverse(sb,0,s.length()-1);
return sb.toString();
}
public void reverse(StringBuilder sb,int start,int end)
{
while(start<end)
{
char c=sb.charAt(start);
sb.setCharAt(start,sb.charAt(end));
sb.setCharAt(end,c);
start++;
end--;
}
}
}
KMP算法:主串指针不回溯,模式串指针回溯
实现strStr()
class Solution {
public int strStr(String haystack, String needle) {
if(haystack.equals("")&&needle.equals("")) return 0;
if(haystack.equals("")) return -1;
if(needle.equals("")) return 0;
int i=1,j=1;
int[] next=getNext(needle);
while(i<=haystack.length()&&j<=needle.length())
{
if(j==0||haystack.charAt(i-1)==needle.charAt(j-1))
{
i++;
j++;
}
else{
j=next[j];
}
}
if(j>needle.length())
{
return i-j;
}
else{
return -1;
}
}
public int[] getNext(String str)
{
int i=1;
int j=0;
int[] next=new int[str.length()+1];
next[1]=0;
while(i<str.length())
{
if(j==0||str.charAt(i-1)==str.charAt(j-1))
{
i++;
j++;
next[i]=j;
}
else{
j=next[j];
}
}
return next;
}
}
重复子串(nextVal数组)
class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String haystack = sc.next();
System.out.println(repeatedSubstringPattern(haystack));
}
public static boolean repeatedSubstringPattern(String s) {
int[] next=getNext(s);
int[] nextVal=getNextVal(next,s);
return s.length()%(s.length()-nextVal[nextVal.length-1])==0;
}
public static int[] getNext(String s)
{
int[] next=new int[s.length()+1];
int i=1;
int j=0;
next[1]=0;
while(i<s.length())
{
if(j==0||s.charAt(i-1)==s.charAt(j-1))
{
i++;
j++;
next[i]=j;
}
else
{
j=next[j];
}
}
for(int x=1;x<next.length;x++)
{
System.out.print(next[x]+" ");
}
return next;
}
public static int[] getNextVal(int[] next,String s)
{
int[] nextVal=new int[s.length()+1];
nextVal[1]=0;
for(int j=2;j<=s.length();j++)
{
if(s.charAt(next[j]-1)==s.charAt(j-1))
{
nextVal[j]=nextVal[next[j]];
}
else
{
nextVal[j]=next[j];
}
}
for(int x=1;x<nextVal.length;x++)
{
System.out.print(nextVal[x]+" ");
}
return nextVal;
}
}