
一、求所有子数组的和的最大值
public static void main(String[] args) {
int[] a = { 1, -2, 3, 10, -4, 7, 2, -5 };
FindMaxSubAry(a);
} public static void FindMaxSubAry(int[] arr) {
int maxStartIndex = 0;// 最大数组开始坐标
int maxEndIndex = 0; // 最大数组结束坐标
int max = 0; // 最大数组的和
int tempSum = 0;
for (int i = 0; i < arr.length; i++) {
tempSum += arr[i];
if (tempSum <= 0) {
tempSum = 0;
max = 0;
maxStartIndex = i + 1;
}
if (tempSum > max) {
max = tempSum;
maxEndIndex = i;
}
}
System.out.println(" sum:" + max);
print(arr, maxStartIndex, maxEndIndex);
} public static void print(int[] arr, int startIndex, int endIndex) {
for (int i = startIndex; i <= endIndex; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
二、顺时针打印矩阵
分析:构造二维矩阵
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
按照从外到里以顺时针的顺序依次打印
打印:1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
public static void print(int[][] a,int start,int end){
if(start>=end||end<0){
return;
}
for(int i=start;i<=end;i++){
System.out.print(a[start][i]+" ");
} for(int i=start+1;i<=end;i++){
System.out.print(a[i][end]+" ");
}
for(int i=end-1;i>=start;i--){
System.out.print(a[end][i]+" ");
} for(int i=end-1;i>start;i--){
System.out.print(a[i][start]+" ");
}
print(a, start+1, end-1);
}
三、2110980789转化为“二十一亿一千零九十八万零七百八十九”
分析:可以分4位为一个量级。其中0789 量级单位 为空,1098量级单位为 万,21量级单位为亿。这四位字符串在以个十百千的形式计算。
package com.amei.java.algorithm; import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
/**
* 将阿拉伯数字转换为大写数字
* 123010980789转化为“二十一亿一千零九十八万零七百八十九”
* @param num
*/
public class Common {
static Map<Integer, String> map = new HashMap<Integer, String>();
static {
map.put(1, "");
map.put(2, "十");
map.put(3, "百");
map.put(4, "千");
} public static void main(String[] args) {
NumberChange(2110980789);
} static String change(int k, char ch) {
String number = numTobigString(ch);
String unit = map.get(k);
if (ch == '0') {
return number;
} else {
return number + unit;
}
} /**
* 123010980789转化为“二十一亿一千零九十八万零七百八十九”
*
* @param num
*/
static void NumberChange(int num) {
String s = num + ""; char[] ch = s.toCharArray();
int flag = 0;//控制量级单位
int k = 1;
Stack<String> st = new Stack<String>();
for (int i = ch.length - 1; i >= 0; i--) {
if (flag == 1 && k == 1) {
st.add("万");
}
if (flag == 2 && k == 1) {
st.add("亿");
}
st.add(change(k, ch[i])); if (k % 4 == 0) {
flag++;
k = 1;
} else {
k++;
}
}
int size = st.size();
for (int i = 0; i < size; i++) {
System.out.print(st.pop());
}
} static String numTobigString(char c) {
if (c == '1') {
return "一";
} else if (c == '2') {
return "二";
} else if (c == '3') {
return "三";
} else if (c == '4') {
return "四";
} else if (c == '5') {
return "五";
} else if (c == '6') {
return "六";
} else if (c == '7') {
return "七";
} else if (c == '8') {
return "八";
} else if (c == '9') {
return "九";
} else if (c == '0') {
return "零";
}
return null;
}
}
四、判断一个ip是否属于某个ip段
分析:将ip转换为数字比较大小,可以看成ip是逢256就进位的数字
public static boolean ipExistsInRange(String ip, String ipSection) {
ipSection = ipSection.trim();
ip = ip.trim();
int idx = ipSection.indexOf('-');
String beginIP = ipSection.substring(0, idx);
String endIP = ipSection.substring(idx + 1);
return getIp2long2(beginIP) <= getIp2long2(ip) && getIp2long2(ip) <= getIp2long2(endIP);
} public static long getIp2long(String ip) {
ip = ip.trim();
String[] ips = ip.split("\\.");
long ip2long = 0L;
for (int i = 0; i < 4; ++i) {
//左移8位,相当于乘以256
ip2long = ip2long << 8 | Integer.parseInt(ips[i]);
}
return ip2long;
} public static long getIp2long2(String ip) {
ip = ip.trim();
String[] ips = ip.split("\\.");
long ip1 = Integer.parseInt(ips[0]);
long ip2 = Integer.parseInt(ips[1]);
long ip3 = Integer.parseInt(ips[2]);
long ip4 = Integer.parseInt(ips[3]);
long ip2long = 1L * ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;
return ip2long;
} public static void main(String[] args) {
String ip = "10.10.10.116";
String ipSection = "10.10.1.00-10.10.255.255";
boolean exists = ipExistsInRange(ip, ipSection);
System.out.println(exists);
}
五、将字符串 abcd;3456789; 反转为 dcba;9876543;
public class StrReverse {
public static void main(String[] args) {
String str = "abcd;3456789";
System.out.println(sectionReverse(str));
} static String sectionReverse(String str) {
char[] c = str.toCharArray();
int start = ;
int end = ;
for (int i = ; i < c.length; i++) {
if (c[i] == ';') {
end = i - ;
reverse(c, start, end);
start = i + ;
}else if(i==c.length-){
end=i;
reverse(c, start, end);
}
}
return String.valueOf(c);
} static void reverse(char[] c, int start, int end) {
char temp;
for (int j = ; j < (end - start) / + ; j++) {
temp = c[end - j];
c[end - j] = c[j + start];
c[j + start] = temp;
}
}
}
六、统计字符串“abaabbccdfeffsfsgggaabgee”中 出现且仅出现n次的第一个字母
public class StrRepeat { public static void main(String[] args) {
String str = "abaabbccdfeffsfsgggaabgee;";
System.out.println(repeatCharFisrt(str, 4));
} static char repeatCharFisrt(String str, int repeatNum) {
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
char[] c = str.toCharArray();
for (int i = 0; i < c.length; i++) {
if (map.containsKey(c[i])) {
map.put(c[i], map.get(c[i]) + 1);
} else {
map.put(c[i], 1);
}
}
for (java.util.Map.Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue() == repeatNum) {
return entry.getKey();
}
}
return '0';
}
}