不常见算法

1.负数左边,正数右边,且相对位置不变

解决办法,冒泡+从右向左,大段大段的交换

public class Solution {

  public void sortArray(int[] nums){

if(nums.length == 0 || nums.length == 1){

return;

}

int end = nums.length-1;

while(end > 0){

if(nums[end] >= 0){

end--;

} else {

break;

}

}

if(end <= 0) {

return;

}

int start = end -1;

while(start >= 0){

if(nums[start]<0){

start--;

} else {

swapPartArray(nums,start,end);

end--;

start --;

}

}


  }


  public void swapPartArray(int[] nums, int i, int j){

int start = i;

while(start<j){

swapArray(nums,start,start + 1);

start++;

}

  }


  public void swapArray(int[] nums, int i,int j){

int temp = nums[i];

nums[i] = nums[j];

nums[j] = temp;

  }


}

2.二叉树后序遍历(非递归)

https://blog.csdn.net/coder__666/article/details/80349039

publicstaticvoidpostOrder(TreeNode biTree)

{//后序遍历非递归实现

intleft =1;//在辅助栈里表示左节点

intright =2;//在辅助栈里表示右节点

Stack stack =newStack();

Stack stack2 =newStack();//辅助栈,用来判断子节点返回父节点时处于左节点还是右节点。

while(biTree !=null|| !stack.empty())

{

while(biTree !=null)

{//将节点压入栈1,并在栈2将节点标记为左节点

stack.push(biTree);

stack2.push(left);

biTree = biTree.left;

}

while(!stack.empty() && stack2.peek() == right)

{//如果是从右子节点返回父节点,则任务完成,将两个栈的栈顶弹出

stack2.pop();

System.out.println(stack.pop().value);

}

if(!stack.empty() && stack2.peek() == left)

{//如果是从左子节点返回父节点,则将标记改为右子节点

stack2.pop();

stack2.push(right);

biTree = stack.peek().right;

}

}

}

public static List<Integer> lastorderTraversal(TreeNode root) {

// write your code here

List<Integer> list = new ArrayList<Integer>();

Stack<TreeNode> stack = new Stack<TreeNode>();

if (root == null) {

return list;

}

stack.push(root);

TreeNode cur = root.left;

TreeNode temp = null;

TreeNode pre = cur;

while (!stack.empty()) {

if (cur == null) {

temp = stack.peek();

if (temp.left == pre) {

System.out.println("temp.left == pre");

if (temp.right == null) {

list.add(temp.val);

stack.pop();

pre = temp;

} else {

stack.push(temp.right);

}

} else if (temp.right == pre) {

System.out.println("temp.right == pre");

temp = stack.pop();

list.add(temp.val);

pre = temp;

System.out.println("temp.val:" + temp.val);

} else if (temp.left != null) {

System.out.println("temp.left != null");

System.out.println("temp.left.val:"  + temp.left.val);

stack.push(temp.left);

cur = temp.left.left;

} else if (temp.right != null) {

System.out.println("temp.right != null");

System.out.println("temp.right.val:"  + temp.right.val);

stack.push(temp.right);

cur = temp.right.left;

}  else {

System.out.println("else");

System.out.println("temp.val:"  + temp.val);

list.add(temp.val);

stack.pop();

pre = temp;

}

} else {

stack.push(cur);

System.out.println("cur.val:" + cur.val);

cur = cur.left;

}

}

return list;

}

3.比原有数字大,但是最小的数字,1234->1243

递归,注意本身数组,index从0开始

static int cur = 0;

static int min = 0;

public  static int getMinNum(int num){

int len = 0;

int temp = num;

while(temp > 0) {

temp  = temp / 10;

len++;

}

if(len <= 1) {

return num;

}

System.out.println("len:" + len);

cur = num;

min = num;

int i = 0;

int[] nums = new int[len];

while(num > 0) {

nums[i] = num % 10;

num  = num / 10;

i++;

}

recursion(nums, 0);

return min;

}

private  static  void recursion(int[] nums, int index) {

System.out.println("index:" + index);

if(index == nums.length-1) {

int temp = numsToInt(nums);

System.out.println("temp:" + temp);

if(temp > cur) {

if(min == cur) {

min = temp;

} else if(min > temp){

min = temp;

}

}

return;

}

for(int i=index;i<nums.length;i++){

swapArray(nums,index, i);

System.out.println("nums[0]:" + nums[0]);

recursion(nums, index + 1);

swapArray(nums, index, i);

System.out.println("nums[0]:" + nums[0]);

}

}

private static void swapArray(int[] nums,int i,int j){

int temp = nums[i];

nums[i] = nums[j];

nums[j] = temp;

}

private static int numsToInt(int[] nums){

int i=0;

int result = 0;

while(i< nums.length){

result += nums[i] * (int)Math.pow(10,i);

i++;

}

return result;

}

4.生产者-消费者

package normal;

import java.util.LinkedList;

public class CurrentComm {

private int max  = 5;

private int cur = 0;

LinkedList<Integer> list = new LinkedList<Integer>();

public void product(){

synchronized(list){

if(list.size() >= 5) {

System.out.println("满了");

try {

list.wait();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

list.add(cur++);

System.out.println("add:" + cur);

list.notify();

}

}

public void customer(){

synchronized(list){

if(list.size() == 0) {

System.out.println("空了");

try {

list.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

int result = list.poll();

System.out.println("remove:" + result);

list.notify();

}

}

}

5.连续子串和最大

记录当前最大,和最终最大

public static int maxSubArray(int[] nums) {

if(nums == null || nums.length == 0) {

return 0;

}

int tempMax = nums[0];

int max = nums[0];

for(int i=1;i<nums.length;i++){

tempMax += nums[i];

tempMax = tempMax>nums[i]?tempMax:nums[i];

max = tempMax>max?tempMax:max;

}

return max;

}

6.O(1)删除链表指定节点

区分头部和尾部
public static void deleteNode(ListNode pHead, ListNode pDelNode){

if(pDelNode == null || pHead == null) {

return;

}

if(pHead == pDelNode) {

pHead = pHead.next;

} else if(pDelNode.next != null) {

pDelNode.val = pDelNode.next.val;

pDelNode.next = pDelNode.next.next;

} else {

ListNode temp = pHead;

while(temp.next != pDelNode) {

temp = temp.next;

}

temp.next = null;

}

}

7.字符串最多字符和最大次数

replace得到最大数量即可

public static void getMax(String strs){

int maxNum = 0;

String maxStr = "";

while(strs.length() > 0){

int startLength = strs.length();

String head = strs.substring(0, 1);

strs = strs.replace(head,"");

int endLength = strs.length();

if((startLength-endLength)>maxNum) {

maxNum = startLength-endLength;

maxStr = head;

}

}

System.out.println(maxStr + " " + maxNum);

}

8.二维矩阵搜索target

二分法搜索,注意边界

public class Solution {

    public boolean searchMatrix(int[][] matrix, int target) {

        // write your code here

int row = matrix.length;

if(row == 0) {

return false;

}

int col = matrix[0].length;

if( col == 0) {

return false;

}

int start=0,end=row-1;

int middle = (start + end)/2;

while(middle>= start && middle <= end && start < col  && end >= 0){

if(matrix[middle][0] == target) {

return true;

} else if(matrix[middle][0] > target){

end = middle - 1;

} else if(matrix[middle][0] < target){

start = middle +1;

}

middle = (start + end) /2;

}

if(end<0) return false;

return searchOneLine(matrix,start-1,col-1,target);

}

public boolean searchOneLine(int[][] matrix,int i,int end,int target){

int col = end;

int start=0;

int j=(start+end)/2;

while(j>=start&&j<=end&&start<=col&&end>=0){

if(matrix[i][j] == target){

return true;

} else if(matrix[i][j] > target){

end = j -1;

} else if(matrix[i][j] < target){

start = j + 1;

}

j=(start+end)/2;

}

return false;

}

}

9.排序链表

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容