不常見算法

1.負(fù)數(shù)左邊,正數(shù)右邊咕宿,且相對位置不變

解決辦法币绩,冒泡+從右向左,大段大段的交換

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)

{//后序遍歷非遞歸實現(xiàn)

intleft =1;//在輔助棧里表示左節(jié)點

intright =2;//在輔助棧里表示右節(jié)點

Stack stack =newStack();

Stack stack2 =newStack();//輔助棧蜡秽,用來判斷子節(jié)點返回父節(jié)點時處于左節(jié)點還是右節(jié)點。

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

{

while(biTree !=null)

{//將節(jié)點壓入棧1缆镣,并在棧2將節(jié)點標(biāo)記為左節(jié)點

stack.push(biTree);

stack2.push(left);

biTree = biTree.left;

}

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

{//如果是從右子節(jié)點返回父節(jié)點芽突,則任務(wù)完成,將兩個棧的棧頂彈出

stack2.pop();

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

}

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

{//如果是從左子節(jié)點返回父節(jié)點董瞻,則將標(biāo)記改為右子節(jié)點

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.比原有數(shù)字大寞蚌,但是最小的數(shù)字,1234->1243

遞歸钠糊,注意本身數(shù)組挟秤,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.生產(chǎn)者-消費者

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.連續(xù)子串和最大

記錄當(dāng)前最大,和最終最大

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)刪除鏈表指定節(jié)點

區(qū)分頭部和尾部
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.字符串最多字符和最大次數(shù)

replace得到最大數(shù)量即可

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.排序鏈表

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末艘刚,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子截珍,更是在濱河造成了極大的恐慌攀甚,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件笛臣,死亡現(xiàn)場離奇詭異云稚,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)沈堡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門静陈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人诞丽,你說我怎么就攤上這事鲸拥。” “怎么了僧免?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵刑赶,是天一觀的道長。 經(jīng)常有香客問我懂衩,道長撞叨,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任浊洞,我火速辦了婚禮牵敷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘法希。我一直安慰自己枷餐,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布苫亦。 她就那樣靜靜地躺著毛肋,像睡著了一般怨咪。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上润匙,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天诗眨,我揣著相機(jī)與錄音,去河邊找鬼趁桃。 笑死辽话,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的卫病。 我是一名探鬼主播,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼典徘,長吁一口氣:“原來是場噩夢啊……” “哼蟀苛!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起逮诲,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤帜平,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后梅鹦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體裆甩,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年齐唆,在試婚紗的時候發(fā)現(xiàn)自己被綠了嗤栓。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡滑臊,死狀恐怖贿条,靈堂內(nèi)的尸體忽然破棺而出齐佳,到底是詐尸還是另有隱情,我是刑警寧澤堪澎,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站味滞,受9級特大地震影響樱蛤,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜剑鞍,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一昨凡、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧攒暇,春花似錦土匀、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至妒御,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間乎莉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工惋啃, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留哼鬓,地道東北人。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓异希,卻偏偏與公主長得像,于是被迫代替她去往敵國和親称簿。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容