public void maxSum(int[] nums) {
int start = 0;
int end = 0;
int max = 0;
int temp = 0;
int ts = 0;
for(int i = 0; i < nums.length; i++) {
temp += nums[i];
if(temp < 0) {
ts = i + 1;
temp = 0;
} else {
if(temp > max) {
start = ts;
end = i;
max = temp;
}
}
}
System.out.println("maxSum = " + max + ", start : " + start + ", end = " + end);
}
复制代码
//时间复杂度:nlg(2n)
public void sort(int[] a,int low,int high){
int start = low;
int end = high;
int key = a[low];
while(end>start){
//从后往前比较
while(end>start&&a[end]>=key) //若是没有比关键值小的,比较下一个,直到有比关键值小的交换位置,而后又从前日后比较
end--;
if(a[end]<=key){
int temp = a[end];
a[end] = a[start];
a[start] = temp;
}
//从前日后比较
while(end>start&&a[start]<=key)//若是没有比关键值大的,比较下一个,直到有比关键值大的交换位置
start++;
if(a[start]>=key){
int temp = a[start];
a[start] = a[end];
a[end] = temp;
}
//此时第一次循环比较结束,关键值的位置已经肯定了。左边的值都比关键值小,右边的值都比关键值大,可是两边的顺序还有多是不同的,进行下面的递归调用
}
//递归
if(start>low) sort(a,low,start-1);//左边序列。第一个索引位置到关键值索引-1
if(end<high) sort(a,end+1,high);//右边序列。从关键值索引+1到最后一个
}
复制代码
/**
* 冒泡排序 时间复杂度:n^2
* 比较相邻的元素。若是第一个比第二个大,就交换他们两个。
* 对每一对相邻元素做一样的工做,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
* 针对全部的元素重复以上的步骤,除了最后一个。
* 持续每次对愈来愈少的元素重复上面的步骤,直到没有任何一对数字须要比较。
* @param numbers 须要排序的整型数组
*/
public static void bubbleSort(int[] numbers)
{
int temp = 0;
int size = numbers.length;
for(int i = 0 ; i < size-1; i ++) {
for(int j = 0 ;j < size-1-i ; j++) {
//交换两数位置
if(numbers[j] > numbers[j+1]) {
temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
}
复制代码
/**
* 选择排序算法 时间复杂度:n^2
* 在未排序序列中找到最小元素,存放到排序序列的起始位置
* 再从剩余未排序元素中继续寻找最小元素,而后放到排序序列末尾。
* 以此类推,直到全部元素均排序完毕。
* @param numbers
*/
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i; // 用来记录最小值的索引位置,默认值为i
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j; // 遍历 i+1~length 的值,找到其中最小值的位置
}
}
// 交换当前索引 i 和最小值索引 minIndex 两处的值
if (i != minIndex) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
// 执行完一次循环,当前索引 i 处的值为最小值,直到循环结束便可完成排序
}
}
复制代码
/**
* 插入排序 时间复杂度:n^2
*
* 从第一个元素开始,该元素能够认为已经被排序
* 取出下一个元素,在已经排序的元素序列中从后向前扫描
* 若是该元素(已排序)大于新元素,将该元素移到下一位置
* 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置
* 将新元素插入到该位置中
* 重复步骤2
* @param numbers 待排序数组
*/
public static void insertSort(int[] numbers) {
int size = numbers.length;
int temp = 0 ;
int j = 0;
for(int i = 0 ; i < size ; i++) {
temp = numbers[i];
//假如temp比前面的值小,则将前面的值后移
for(j = i ; j > 0 && temp < numbers[j-1] ; j --) {
numbers[j] = numbers[j-1];
}
numbers[j] = temp;
}
}
复制代码
public class MyList {
/**
* 递归方式合并两个单链表
*
* @param head1 有序链表1
* @param head2 有序链表2
* @return 合并后的链表
*/
public static Node mergeTwoList(Node head1, Node head2) {
//递归结束条件
if (head1 == null && head2 == null) {
return null;
}
if (head1 == null) {
return head2;
}
if (head2 == null) {
return head1;
}
//合并后的链表
Node head = null;
if (head1.data > head2.data) {
//把head较小的结点给头结点
head = head2;
//继续递归head2
head.next = mergeTwoList(head1, head2.next);
} else {
head = head1;
head.next = mergeTwoList(head1.next, head2);
}
return head;
}
复制代码
public int search(int[] arr, int key) {
int start = 0;
int end = arr.length - 1;
while (start <= end) {
int middle = (start + end) / 2;
if (key < arr[middle]) {
end = middle - 1;
} else if (key > arr[middle]) {
start = middle + 1;
} else {
return middle;
}
}
return -1;
}
复制代码
class ListNode{
ListNode left;
ListNode right;
int val;
public ListNode(int value){
this.val=value;
}
}
复制代码
//广度遍历
public void levelOrderTraversal(LsitNode node){
if(node==null){
System.out.print("empty tree");
return;
}
ArrayDeque<ListNode> deque = new ArrayDeque<ListNode>();
deque.add(node);
while(!deque.isEmpty()){
ListNode rnode = deque.remove();
System.out.print(rnode.val+" ");
if(rnode.left!=null){
deque.add(rnode.left);
}
if(rnode.right!=null){
deque.add(rnode.right);
}
}
}
复制代码
//深度遍历
public void depthTraversal(ListNode node){
if(node==null){
System.out.print("empty tree");
return;
}
Stack<ListNode> stack = new Stack<ListNode>();
stack.push(node);
while(!stack.isEmpty()){
ListNode rnode = stack.pop();
System.out.print(rnode.val);
if(rnode.right!=null){
stack.push(rnode.right);
}
if(rnode.left!=null){
stack.push(rnode.left);
}
}
}
复制代码
//利用for循环删除
for (int i = 0; i < aList.size(); i++) {
if ("abc".equals(aList.get(i))) {
// 索引回溯
aList.remove(i--);
}
}
复制代码
//利用迭代器的remove()方法删除
ListIterator<String> listIterator = aList.listIterator();
while(listIterator.hasNext()){
String str = listIterator.next();
if ("abc".equals(str)) {
//迭代器的remove() 方法删除
listIterator.remove();
}
}
复制代码
- Android 遍历ViewGroup找出某种类型的全部子View
// 遍历viewGroup
public void traverseViewGroup(View view) {
if(null == view) {
return;
}
if(view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
LinkedList<ViewGroup> linkedList = new LinkedList<>();
linkedList.add(viewGroup);
while(!linkedList.isEmpty()) {
ViewGroup current = linkedList.removeFirst();
//dosomething
for(int i = 0; i < current.getChildCount(); i ++) {
if(current.getChildAt(i) instanceof ViewGroup) {
linkedList.addLast((ViewGroup) current.getChildAt(i));
}else {
//dosomething
}
}
}
}else {
//dosomething
}
}
复制代码