题四十一 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
import java.util.ArrayList;
public class Solution {
public ArrayList FindNumbersWithSum(int [] array,int sum) {
ArrayList result = new ArrayList();
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--;
}
}
}
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
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 cntZero? false:true;
}
}
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;
}
}
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'9') return 0;
sum = sum*10 +(ch[i] - '0');
}
return flag? (0-sum):sum;
}
}
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
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
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;
}
}