题四十一 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) {
ArrayList<Integer> result = new ArrayList<Integer>();
if(array.length == 0 || array == null || sum < 0) return result;
int low = 0;
int high = array.length-1;
while(low< high){
int currSum = array[low]+array[high];
if(currSum == sum){
result.add(array[low]);
result.add(array[high]);
return result;
}
else if(currSum > sum) high--;
else low++;
}
return result;
}
}
题四十二 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。
public class Solution {
public String LeftRotateString(String str,int n) {
char[] ch = str.toCharArray();
if(ch.length < n) return "";
reverse(ch,0,n-1);
reverse(ch,n,ch.length-1);
reverse(ch,0,ch.length-1);
StringBuffer sb = new StringBuffer(ch.length);
for(char c:ch){
sb.append(c);
}
return sb.toString();
}
public void reverse(char[] ch,int low, int high){
char tmp;
while(low < high){
tmp = ch[low];
ch[low] = ch[high];
ch[high] = tmp;
low++;
high--;
}
}
}
题四十三 翻转句子,例如 “student. a am I”翻转为“I am a student.”。
public class Solution {
public String ReverseSentence(String str) {
char[] ch = str.toCharArray();
if(ch.length < 0) return "";
reverse(ch,0,ch.length-1);
int blank = -1;
for(int i =0; i<ch.length; i++){
if(ch[i]==' '){
int nextBlank = i;
reverse(ch,blank+1,nextBlank-1);
blank = nextBlank;
}
}
reverse(ch,blank+1,ch.length-1);
return new String(ch);
}
public void reverse(char[] ch, int low, int high){
char tmp;
while(low<high){
tmp = ch[low];
ch[low] = ch[high];
ch[high] = tmp;
low++;
high--;
}
}
}
题四十四 一副扑克牌,里面居然有2个大王,2个小王(一副牌原本是54张)。随机从中抽出5张牌,看能不能抽到顺子。这里规定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。为了方便起见,可以认为大小王是0。
import java.util.*;
public class Solution {
public boolean isContinuous(int [] numbers) {
if(numbers.length <1 || numbers == null) return false;
Arrays.sort(numbers);
int cntZero = 0;
int cntGap = 0;
for(int i =0; i<numbers.length ; i++){
if(numbers[i] == 0)
cntZero++;
}
int index1 = cntZero;
int index2 = index1+1;
while(index2 < numbers.length){
if(numbers[index1] == numbers[index2]) return false;
cntGap += numbers[index2] - numbers[index1] - 1;
index1 = index2;
index2++;
}
return cntGap > cntZero? false:true;
}
}
题四十五 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
public class Solution {
public int Sum_Solution(int n) {
int sum = n;
boolean flag = (sum>0)&&((sum += Sum_Solution(--n))>0);
return sum;
}
}
题四十六 写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
public class Solution {
public int Add(int num1,int num2) {
int sum,carry;
while(num2 != 0){
sum = num1 ^ num2;
carry = (num1 & num2) << 1;
num1 = sum;
num2 = carry;
}
return num1;
}
}
题四十七 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。如果是合法的数值表达则返回该数字,否则返回0。
public class Solution {
public int StrToInt(String str) {
if(str == null || str.length() == 0) return 0;
char[] ch = str.toCharArray();
boolean flag = false;
if(ch[0] == '-') flag = true;
int sum = 0;
for(int i =0; i<ch.length; i++){
if(i==0&&ch[i]=='+' || ch[i]=='-') continue;
if(ch[i]<'0' || ch[i]>'9') return 0;
sum = sum*10 +(ch[i] - '0');
}
return flag? (0-sum):sum;
}
}
题四十八 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
public class Solution {
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication) {
if(numbers == null || length == 0) return false;
boolean[] help = new boolean[length];
for(int i =0;i<help.length;i++){
if(help[numbers[i]] == true){
duplication[0] =numbers[i];
return true;
}
help[numbers[i]] = true;
}
return false;
}
}
题四十九 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。
import java.util.ArrayList;
public class Solution {
public int[] multiply(int[] A) {
int len = A.length;
int[] forword = new int[len];
int[] backword = new int[len];
int[] result = new int[len];
forword[0] = 1;
backword[0] = 1;
for(int i =1; i<len; i++){
forword[i] = A[i-1]*forword[i-1];
backword[i] = A[len - i]*backword[i-1];
}
for(int i =0; i<len; i++){
result[i] = forword[i]*backword[len - 1-i];
}
return result;
}
}
题五十 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配。
public class Solution {
public boolean match(char[] str, char[] pattern)
{ if(str == null || pattern == null ) return false;
int strIndex = 0;
int patternIndex = 0;
return matchHelp(str,pattern,strIndex,patternIndex);
}
public boolean matchHelp(char[] str, char[] pattern,int strIndex, int patternIndex){
if(strIndex == str.length && patternIndex == pattern.length){
return true;
}
if(strIndex != str.length && patternIndex == pattern.length){
return false;
}
if(patternIndex+1< pattern.length && pattern[patternIndex+1] == '*'){
if((strIndex != str.length && pattern[patternIndex] == str[strIndex])||(strIndex != str.length && pattern[patternIndex] == '.')){
return matchHelp(str,pattern,strIndex,patternIndex+2)||matchHelp(str,pattern,strIndex+1,patternIndex)||matchHelp(str,pattern,strIndex+1,patternIndex+2);
}
else return matchHelp(str,pattern,strIndex,patternIndex+2);
}
if((strIndex != str.length && pattern[patternIndex] == str[strIndex]) || (pattern[patternIndex] == '.' && strIndex != str.length)){
return matchHelp(str,pattern,strIndex+1,patternIndex+1);
}
return false;
}
}