- 博客(30)
- 资源 (2)
- 收藏
- 关注
原创 Algorithms—154.Find Minimum in Rotated Sorted Array II
思路:按照题目提示思考了下,假设一个长度为n的数组由(n-1)个3和1个1构成,那么这个1有可能出现在任何位置,所以时间复杂度为O(n),所以直接遍历了。耗时:404ms。处于下游,显然还有更快的方法。public class Solution { public int findMin(int[] nums) { int a=nums[0]; for (int
2015-06-29 14:34:49 306
原创 Algorithms—153.Find Minimum in Rotated Sorted Array
思路:二分法查找,观察样例,更改判断条件即可。耗时:324ms。处于中游水准。public class Solution { public int findMin(int[] nums) { int begin=0; int end=nums.length-1; int half=(begin+end)/2; whil
2015-06-29 14:16:49 330
原创 Algorithms—151.Reverse Words in a String
思路:思路很简单,反着读,非空格暂存,读到空格拼接。难点在于前后空格省略,中间空格合并为一个。只是优化的难点,不是实现的难点。耗时:440ms。处于下游水准,代码较渣。public class Solution { public String reverseWords(String s) { if (s.length()<1) { return s; } Stri
2015-06-29 13:33:01 460
原创 Algorithms—136.Single Number
思路:异或计算。0^a=a;a^b=b^a。public class Solution { public int singleNumber(int[] nums) { int answer=0; for (int i = 0; i < nums.length; i++) { answer^=nums[i]; } return answer;
2015-06-26 15:10:15 289
原创 Algorithms—134.Gas Station
思路:每个加油站提供的油减去下一段路上耗费的油,生成新的数组。循环数组长度2倍的量,找出其中一段长度为数组长且每部的剩余油量都不小于0的;public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int[] n = new int[gas.length]; for (int i
2015-06-26 14:19:32 271
原创 Algorithms—125.Valid Palindrome
思路:判断一个字符串是否是回文很简单,题目就是把给的字符串空格和标点符号去掉,大写小写转化成同等的比较,我选择的是大写转小写。public class Solution { public boolean isPalindrome(String s) { if (s.length()<=1) { return true; } char[] c=s.to
2015-06-26 10:59:39 317
原创 Algorithms—119.Pascal's Triangle II
思路:限制了空间复杂度,我选择了使用map来逐层覆盖的方法。public class Solution { public List getRow(int rowIndex) { Map map=new HashMap(); for (int i = 0; i <=rowIndex; i++) { map.put(i, 1); //反向计算防止值在使
2015-06-26 09:50:28 277
原创 Algorithms—118.Pascal's Triangle
思路:按照杨辉三角形定义去做。public class Solution { public List> generate(int numRows) { List> answerList = new ArrayList>(); for (int i = 0; i < numRows; i++) { List list = new ArrayList(); for (i
2015-06-26 09:09:57 286
原创 Algorithms—77.Combinations
思路:反复添加元素。直到需要返回的list中的每个list长度都符合k。public class Solution { public List> combine(int n, int k) { List> answerList=new ArrayList>(); for (int i = 1; i <=n; i++) { List list=new
2015-06-25 10:12:28 258
原创 Algorithms—75.Sort Colors
思路:从左开始查看,找到非0数,标记,从右开始查看,查看到0,标记,交换,然后从左标记开始继续,以此类推,可使得所有的0集中在数组的左端,同理可得答案。public class Solution { public void sortColors(int[] nums) { int t=0; int b=0; int e=nums.length-1; boolean flag=t
2015-06-24 17:09:30 382
原创 Algorithms—74.Search a 2D Matrix
思路:非常简单,有序数组找target,中间开始判断,唯一需要注意的就是各种数组超界问题。public class Solution { public boolean searchMatrix(int[][] matrix, int target) { int begin=0; int end=matrix.length-1; int t=end/2;
2015-06-24 13:53:37 287
原创 Algorithms—123.Best Time to Buy and Sell Stock III
思路:限制交易两次。首先,求出单次交易最大利润,然后,只有2种可能,与此次交易无关,那么不在左侧就在右侧,再求一次,或者是与此次交易有关,那么在此次交易的日期限制内找出一段利润最低(负值)。比较左侧,右侧,中间的负值的绝对值的大小。这段代码写的比较丑,凑合的看吧。sum3是中间的负值。public class Solution { public int maxProfit(int[
2015-06-22 01:04:13 274
原创 Algorithms—122.Best Time to Buy and Sell Stock II
思路:比121题更简单,不限制交易次数。public class Solution { public int maxProfit(int[] prices) { if (prices.length <= 1) { return 0; } int[] change = new int[prices.length - 1]; for (int i = 0; i < c
2015-06-22 00:39:27 335
原创 Algorithms—121.Best Time to Buy and Sell Stock
思路:很典型的一题,不细说了public class Solution { public int maxProfit(int[] prices) { if (prices.length <= 1) { return 0; } int[] change = new int[prices.length - 1]; for (int i = 0; i < change.
2015-06-22 00:37:18 360
原创 Algorithms—202.Happy Number
思路:用map存储过程中出现的每一个数,重复返回false,1返回truepublic class Solution { public boolean isHappy(int n) { String s=String.valueOf(n); Map map=new HashMap(); map.put(s, "存在");
2015-06-21 22:58:40 279
原创 Algorithms—42.Trapping Rain Water
public class Solution { public int trap(int[] height) { if (height.length<=1) { return 0; } int a=0; int h=height[a]; /** * 求出最高峰 */ for (int i = 1
2015-06-19 22:30:45 278
原创 Algorithms—46.Permutations
思路:排列组合public class Solution { public List> permute(int[] nums) { List> answerList=new ArrayList>(); for (int i = 0; i < nums.length; i++) { int size=answerList.size();
2015-06-11 15:54:53 298
原创 Algorithms—55.Jump Game
思路:从后往前读数组,如果读到0,则判断距离此0位置是否有数大于此距离(如果该0恰好在最后一位,则距离只要大于等于即可)public class Solution { public boolean canJump(int[] nums) { int k=-1; int tar=-1; for (int i = nums.length-1; i>=0 ;
2015-06-11 14:58:54 481
原创 Algorithms—22.Generate Parentheses
public class Solution { public List generateParenthesis(int n) { List list = new ArrayList(); String a = "("; list.add(a); for (int i = 1; i <= 2 * n - 1; i++) { int size = list.size();
2015-06-10 20:22:46 430
原创 Algorithms—17.Letter Combinations of a Phone Number
public class Solution { public List letterCombinations(String digits) { //存放答案的list List list=new ArrayList(); //建立map存放手机按键 Map> map=new HashMap>(); List list2=ne
2015-06-10 19:37:16 224
原创 Algorithms—200.Number of Islands
思路,遍历二维char数组,判断其是否在map中,如果不在map中,且值为‘1’;那么将计数+1,并将点加入map中,然后递归查询周围4个点(考虑边界)情况将public class Solution { public int numIslands(char[][] grid) { int il = 0; Map map = new HashMap(); for (int i
2015-06-04 17:10:54 252
原创 Algorithms—70.Climbing Stairs
思路:爬n阶台阶,有2种方法,先爬n-1阶,或者先爬n-2阶。n=1时,只有1种,n=2时有2种。递归计算public class Solution { public int climbStairs(int n) { if (n<=2) { return n; }else { int[] sk=new int[n]; sk[0]=1; sk[
2015-06-04 00:16:14 312
原创 Algorithms—219.Contains Duplicate II
思路,首先将数组nums中前k个数放入map中查看是否有相同的,如果在,返回true;如果没有,添加;然后,检查后面的每一个数是否在map中,如果在,返回ture;如果没有,添加,并删除map中最先添加的那个;public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) {
2015-06-03 21:50:12 278
原创 Algorithms—36.Valid Sudoku
public class Solution { public boolean isValidSudoku(char[][] board) { //验证每行每列有没有重复数据 for (int i = 0; i < 9; i++) { Map cross=new HashMap(); Map vertical=new HashMap(); for (int j =
2015-06-03 14:17:09 275
原创 Algorithms—35.Search Insert Position
public class Solution { public int searchInsert(int[] nums, int target) { int begin=0; int end=nums.length-1; int a=(begin+end)/2; if (target<nums[0]) { return 0; } if (target
2015-06-03 10:18:41 361
原创 Algorithms—34.Search for a Range
public class Solution { public int[] searchRange(int[] nums, int target) { int begin=0; int end=nums.length-1; int a=(begin+end)/2; int[] answer=new int[2]; while (nums[a]!=target)
2015-06-03 10:08:01 404
原创 Algorithms—27.Remove Element
public class Solution { public int removeElement(int[] nums, int val) { int end=nums.length;int k=0;for (int i = 0; i if (nums[i]==val) {for (int j = i; j k=nums[j];nums[j]=nu
2015-06-02 16:41:51 390
原创 Algorithms—12.Integer to Roman
public class Solution { public String intToRoman(int num) { String a = "";String b = "";String c = "";String d = "";int ia = num / 1000;int ib = (num / 100) % 10;int ic = (num
2015-06-02 12:30:10 355
原创 Algorithms—13.Roman to Integer
package leetcode;public class RomantoInt {public static void main(String[] args) {String s="MCDLXXVI";int a;int b;int c;int d;String sa="";String sb="";String sc="";String sd
2015-06-02 11:33:01 390
原创 Median of Two Sorted Arrays
public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { boolean flag=true;int a=nums1.length/2;int b=nums2.length/2;int c=Math.abs(a-b);int d=0;
2015-06-02 09:50:19 501
2积分系列——经典算法与人工智能在外卖物流调度中的应用
2018-04-26
Outlier Analysis 2nd Edition.pdf ——2积分系列
2018-03-28
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人