面试题41:和为S的连续正整数序列
import java.util.ArrayList;
public class Solution {
public ArrayList<ArrayList<Integer> > FindContinuousSequence(int sum) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
int start = 1;
int end = 2;
int mid = (1 + sum)/2;
int curSum = start + end;
while(start < mid){
if(curSum == sum){
ArrayList<Integer> list = makeList(start,end);
res.add(list);
}
while(curSum > sum && start < mid){
curSum -= start;
start++;
if(curSum == sum){
ArrayList<Integer> list = makeList(start,end);
res.add(list);
}
}
end ++;
curSum += end;
}
return res;
}
public ArrayList<Integer> makeList(int start , int end){
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=start;i<=end;i++){
list.add(i);
}
return list;
}
}
面试题41:递增序列中乘积最小的和为S的两个数
import java.util.ArrayList;
import java.util.HashMap;
public class Solution {
public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) {
ArrayList<Integer> res = new ArrayList<Integer>();
if(array == null && array.length <2 ) return res;
int first = 0;
int second = array.length-1;
int len = array.length;
int mul = Integer.MAX_VALUE;
int retFirst = 0;
int retSecond = 0 ;
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i=0;i<array.length;i++){
map.put(array[i],i);
}
while(first<second && first<len-1 && second<len){
if(map.get(sum - array[first])!=null && map.get(sum - array[first])>first){
second = map.get(sum - array[first]);
int tmpMul = array[first] * array[second];
if(tmpMul < mul){
mul = tmpMul;
retFirst = array[first];
retSecond = array[second];
}
}
first++;
}
if(retFirst>0 && retSecond>0){
res.add(retFirst);
res.add(retSecond);
}
return res;
}
}
面试题42:左旋转字符串
public class Solution {
public String LeftRotateString(String str,int n) {
if(str == null || n>str.length()) return "";
char[] arr = str.toCharArray();
reverse(arr,0,n-1);
reverse(arr,n,arr.length-1);
reverse(arr,0,arr.length-1);
StringBuilder strBuilder = new StringBuilder();
for(int i=0;i<arr.length;i++){
strBuilder.append(arr[i]);
}
return strBuilder.toString();
}
public void reverse(char[] arr,int start,int end){
while(start < end){
char tmp = arr[end];
arr[end--] = arr[start];
arr[start++] = tmp;
}
}
}
面试题42:翻转单词序列
public class Solution {
public String ReverseSentence(String str) {
if (str == null || str.length() == 0 || str.trim().equals("")) {
return str;
}
char[] arr = str.toCharArray();
reverse(arr,0,arr.length-1);
int start = 0,end=0;
for(int i=0;i<arr.length;i++){
if(arr[i] != ' '){
arr[end++] = arr[i];
}else if(i>0 && arr[i-1] != ' '){
reverse(arr,start,end-1);
arr[end++] = ' ';
start = end;
}
}
reverse(arr,start,end-1);
return new String(arr,0,end>0&&arr[end-1]==' ' ? end-1:end);
}
public void reverse(char[] arr,int start,int end){
while(start < end){
char tmp = arr[end];
arr[end--] = arr[start];
arr[start++] = tmp;
}
}
}
面试题44:扑克牌的顺子
import java.util.Arrays;
public class Solution {
public boolean isContinuous(int [] numbers) {
if(numbers.length==0) return false;
if(numbers.length==1) return true;
Arrays.sort(numbers);
int i=0;
int zeroCnt = 0;
while(numbers[i] == 0){
zeroCnt++;
i++;
}
int notConCnt = 0;
while(i<numbers.length){
if(i>0 && numbers[i] == numbers[i-1]) return false;
if(i>0 && numbers[i] != numbers[i-1] && numbers[i-1]!=0){
notConCnt += (numbers[i] - numbers[i-1] - 1);
}
i++;
}
if(zeroCnt >= notConCnt || notConCnt == 0) return true;
else return false;
}
}
面试题45:约瑟夫环
public class Solution {
public int LastRemaining_Solution(int n, int m) {
if(n==0) return -1;
int i=0,r=0;
for(i=2;i<=n;i++){
r = (r + m)%i;
}
return r;
}
}
面试题46:不用循环和判断语句求从1到n的和
public class Solution {
public int Sum_Solution(int n) {
int res = n;
boolean ans = (n>0) && ((res = Sum_Solution(n-1) + res)>0) ;
return res;
}
}
面试题47:不用加减乘除做加法
public class Solution {
public int Add(int num1,int num2) {
int sum ,carry;
do{
sum = num1 ^ num2;
carry = (num1 & num2)<<1;
num1 = sum;
num2 = carry;
}while(num2 != 0);
return num1;
}
}
面试题49:把字符串转成整数
public class Solution {
public int StrToInt(String str) {
if(str==null || str.length() == 0) return 0;
char[] arr = str.toCharArray();
boolean sign = true;
int i=0;
int res = 0;
while(arr[i] == ' '){i++;}
if(arr[i] == '+') {sign = true;i++;}
else if(arr[i] == '-'){sign = false;i++;}
for(;i<arr.length;i++){
if(arr[i]<'0' || arr[i]>'9'){return 0;}
int digit = arr[i] - '0';
if(sign == true && (Integer.MAX_VALUE - digit) / 10 < res){
return Integer.MAX_VALUE;
}else if(sign==false &&(Integer.MIN_VALUE + digit)/10>res){
return Integer.MIN_VALUE;
}
res =res*10 + (sign==true ? digit:-digit);
}
return res;
}
}