随笔分类 -  算法——cracking the coding interview150解答

cc150.第五版。所有题目解答
摘要:1,自己写的又长又臭的代码,也能AC,但是太丑了。主要是通过二进制来算。public static int addAB(int a, int b){ int res = 0; String str1 = Integer.toBinaryString(a); ... 阅读全文
posted @ 2016-01-07 12:31 仔细思考一切 阅读(277) 评论(0) 推荐(0)
摘要:思路:1,先给出LTE的代码: public static int countNumberOf2s(int n) { // write code here int res = 0; for(int i = 0; i 0){ i... 阅读全文
posted @ 2016-01-06 10:40 仔细思考一切 阅读(217) 评论(0) 推荐(0)
摘要:思路:注意一下,找猜中的时候什么都不用做,直接统计,找伪猜中也是先不管,然后再把结果减去猜中即可。public static int[] calcResult(String a, String b){ int[] res = new int[2]; Str... 阅读全文
posted @ 2016-01-05 17:54 仔细思考一切 阅读(404) 评论(0) 推荐(0)
摘要:牛客网的在线题。思路,比较简单。就是判断一下是否有连起来的1. public static boolean checkWon(int[][] board){ boolean res = false; for(int i = 0; i < 3;i++){ ... 阅读全文
posted @ 2016-01-05 16:59 仔细思考一切 阅读(164) 评论(0) 推荐(0)
摘要:思路,其实这题easy,就是看多少个5.答案: public static int getFactorSuffixZero(int n) { // write code here int res = 0; while(n != 0){ ... 阅读全文
posted @ 2016-01-05 14:46 仔细思考一切 阅读(224) 评论(0) 推荐(0)
摘要:思路:借助max公式就可以了。max(x,y)=0.5*(x+y+|x-y|)注意:1,结尾要加(int)。答案:max(x,y)=0.5*(x+y+|x-y|)View Code 阅读全文
posted @ 2016-01-05 13:08 仔细思考一切 阅读(175) 评论(0) 推荐(0)
摘要:思路:比较easy。就是借助hashset让他有序然后就能够比较节省时间了。答案: public static int[] getRankOfNumber(int[] a, int n){ int[] res = new int[n]; HashSet ha... 阅读全文
posted @ 2016-01-05 11:46 仔细思考一切 阅读(176) 评论(0) 推荐(0)
摘要:1,牛客网第一题:这其实跟找最长递增子序列是一个东西。注意的地方是,返回的是最大的dp,而不是dp[N-1]。答案:public static int getHeight(int[] men, int n) { // write code here int res = 0... 阅读全文
posted @ 2016-01-04 23:54 仔细思考一切 阅读(297) 评论(0) 推荐(0)
摘要:思路,一旦提到查找就要想到二分查找。 public static int[] findElement(int[][] a, int n, int m, int key) { // write code here int[] res = new int[2]; ... 阅读全文
posted @ 2016-01-04 23:42 仔细思考一切 阅读(186) 评论(0) 推荐(0)
摘要:注意,1,"" 和 " "。是不同的,空字符串指的是"";2,注意String的compareTo。小于是指 left && a[mid] == "") { mid--; } if(key.equals... 阅读全文
posted @ 2016-01-04 23:18 仔细思考一切 阅读(355) 评论(0) 推荐(0)
摘要:思路,这道题用二分,唯一的不同就是,1,a[left]= a[left]){ right = mid -1; } else{ left = mid + 1; ... 阅读全文
posted @ 2016-01-04 21:56 仔细思考一切 阅读(168) 评论(0) 推荐(0)
摘要:这道题主义的就是,要利用数组自带的sort函数。此外,注意,利用hash来判断是否出现了。 public static ArrayList sortStrings(String[] str, int n) { // write code here Arra... 阅读全文
posted @ 2016-01-04 21:09 仔细思考一切 阅读(280) 评论(0) 推荐(0)
摘要:1,牛客网的解题思路:其实这就是求一个最长的递增子序列。 以某一个箱子结尾的最大高度=放在它上面的所有类型中高度最大的那个+他自己的高度。import java.util.*; public class Box { public int getHeight(int[] w, int[] l,... 阅读全文
posted @ 2016-01-04 17:10 仔细思考一切 阅读(565) 评论(0) 推荐(0)
摘要:思路:首先写一个检查能不能摆的函数。boolean checkValid(int[] columns,int row1, int column1);意思是row1行摆在column1列可不可以。然后是place函数。第一个参数row表示现在摆第几行。第一行可以摆n次位置,然后往下也是8ci。也就是相... 阅读全文
posted @ 2016-01-04 10:31 仔细思考一切 阅读(497) 评论(0) 推荐(0)
摘要:这道题比较简单,就是通过从后往前复制大的就可以了。最后比较注意的就是如果B还没复制完要记得接着复制。import java.util.Arrays;public class Solution{ public static void main(String[] args){ int... 阅读全文
posted @ 2016-01-04 00:00 仔细思考一切 阅读(168) 评论(0) 推荐(0)
摘要:这道题卡了一天。要想AC非常难。1,第一个解决办法,优化暴力:public class Coins { public static int countWays(int n){ int num25 = n / 25; long res = 0; for(int i = 0; i ... 阅读全文
posted @ 2016-01-03 23:00 仔细思考一切 阅读(293) 评论(0) 推荐(0)
摘要:----思路:利用三个队列,一个存3,一个存5,一个存7.然后,3*3的都放第一个。然后3*5,5*5的放第二个。然后,3*7,5*7,7*7的都放第三个。答案: public static int findKth(int k){ ArrayList res = new Arra... 阅读全文
posted @ 2015-12-31 15:13 仔细思考一切 阅读(207) 评论(0) 推荐(0)
摘要:用^来操作:public static int[] exchangeAB(int[] AB){ AB[0] = AB[0] ^ AB[1]; AB[1] = AB[0] ^ AB[1]; AB[0] = AB[0] ^ AB[1]; retur... 阅读全文
posted @ 2015-12-31 10:45 仔细思考一切 阅读(303) 评论(0) 推荐(0)
摘要:直接两个点确定一条直线。然后两两组合,再写一个看过多少个点的函数。一直更新max就行。import java.util.Arrays;public class Solution { public static void main(String[] args){ Point[] a... 阅读全文
posted @ 2015-12-30 21:11 仔细思考一切 阅读(259) 评论(0) 推荐(0)
摘要:最主要的思路:1,这条直线就是要把两个正方形的中点链接。2,注意的特殊情况:中心点重合。答案:public class Solution { public static void main(String[] args){ Point[] a = {new Point(13... 阅读全文
posted @ 2015-12-30 14:28 仔细思考一切 阅读(247) 评论(0) 推荐(0)