public class Solution {
public int FindGreatestSumOfSubArray(int[] array) {
int max = array[0];
int res = array[0];
for(int i = 0; i< array.length-1; i++){
max = Math.max(max+array[i+1], array[i+1]);
res = Math.max(max, res);
}
return res;
}
}
2. 求出任意非负整数区间中1出现的次数(从1 到 n 中1出现的次数)。
public class Solution {
public int NumberOf1Between1AndN_Solution(int n) {
if(n <= 0) return 0;
int count = 0;
for(int i = 1; i <= n; i*=10){
long deliver = i*10;
count += (n/deliver)*i + Math.min(Math.max(n%deliver - i +1, 0), i);
}
return count;
}
}
3. 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。
import java.util.ArrayList;
public class Solution {
public String PrintMinNumber(int [] numbers) {
for(int i = 0; i < numbers.length; i++){
for(int j = i+1; j < numbers.length; j++){
int a = Integer.valueOf(numbers[i]+""+numbers[j]);
int b = Integer.valueOf(numbers[j]+""+numbers[i]);
if(a>b){
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
String s = "";
for(int i = 0; i < numbers.length; i++)
s += String.valueOf(numbers[i]);
return s;
}
}