一、求所有子数组的和的最大值

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';
}
}

经典算法(四) 数组相关 & 螺旋矩阵 & 数字大小写转换 & 字符串相关的更多相关文章

  1. Java经典算法四十例编程详解+程序实例

    JAVA经典算法40例 [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?   1.程 ...

  2. 【算法笔记】B1050 螺旋矩阵

    1050 螺旋矩阵 (25 分)   本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”.所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为 m 行  ...

  3. [算法][LeetCode]Spiral Matrix——螺旋矩阵

    题目要求 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...

  4. leecode第五十四题(螺旋矩阵)

    class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) ...

  5. PHP算法--将数字金额转换成大写金额

    最近在看一些PHP算法题,遇到一个将数字金额转换成大写金额的小算法题,这里贴出自己的一个例子. 注:这个小算法适用于10万以内的金额. <?php //$num = 12345.67; func ...

  6. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

  7. 经典算法题每日演练——第十四题 Prim算法

    原文:经典算法题每日演练--第十四题 Prim算法 图论在数据结构中是非常有趣而复杂的,作为web码农的我,在实际开发中一直没有找到它的使用场景,不像树那样的频繁使用,不过还是准备 仔细的把图论全部过 ...

  8. PHP实现螺旋矩阵(螺旋数组)

    今天碰到一个比较有意思的问题, 就是把A到Y这25个字母以下面的形式输出出来 A B C D E P Q R S F O X Y T G N W V U H M L K J I 问题很有意思,就是转圈 ...

  9. [经典算法题]寻找数组中第K大的数的方法总结

    [经典算法题]寻找数组中第K大的数的方法总结 责任编辑:admin 日期:2012-11-26   字体:[大 中 小] 打印复制链接我要评论   今天看算法分析是,看到一个这样的问题,就是在一堆数据 ...

随机推荐

  1. 嵌入式Web服务器boa在ARM平台的移植步骤

    1.下载http://www.boa.org/ 2.解压tar xzf boa-0.94.13.tar.gz 3.编译cd boa-0.94.13/src./configure 生成了makefile ...

  2. IntelliJ Cannot find declaration to goto----解决方案

    系统中已经有了该类库,还是找不到类提示 close the project in intellij. close intellij. go to the project folder and dele ...

  3. 3.Javascript实现instanceof

    instanceof instanceof 用于判断某个对象是否是另一个对象(构造方法)的实例.instanceof会查找原型链,直到null如果还不是后面这个对象的实例的话就返回false,否则就返 ...

  4. echarts 通过源码方法 传入对应data数据获取分割步长值

    通过源码方法获取这里的分割数字长度 /** * Quantity of a number. e.g. 0.1, 1, 10, 100 * * @param {number} val * @return ...

  5. shell脚本条件判断if中-a到-z的意思

    [ -a FILE ]  如果 FILE 存在则为真.  [ -b FILE ]  如果 FILE 存在且是一个块特殊文件则为真.  [ -c FILE ]  如果 FILE 存在且是一个字特殊文件则 ...

  6. Java httpclent请求httpclentUtils工具类

    第一种写法: import java.io.IOException; import java.io.InterruptedIOException; import java.io.Unsupported ...

  7. Tomcat内存溢出解决方法

    Java内存溢出详解 一.常见的Java内存溢出有以下三种: 1. java.lang.OutOfMemoryError: Java heap space ----JVM Heap(堆)溢出 JVM在 ...

  8. Example-based Machine Learning是什么?

    参考:https://christophm.github.io/interpretable-ml-book/proto.html EML简介 Example-based Machine Learnin ...

  9. Selenium_webdriver+java+TestNG入门UI自动化

    web ui自动化测试需要的工作:Eclipse(JAVA编译器).selenium(库文件).webdriver(浏览器驱动).testng的lib; 如图: 第一步:先部署坏境,下载seleniu ...

  10. wordpress获取当前页面链接

    我们知道wordpress的<?php the_permalink(); ?>和<?php echo get_permalink(); ?>可以获取页面链接,但是有些比较复杂的 ...