
LeetCode
Carmelo_Z
这个作者很懒,什么都没留下…
展开
-
leetcode [Number of 1 Bits]//待整理多种解法
public class Solution { // you need treat n as an unsigned value //所以方法中参数类型如果是int,最大能接受是0+31个1,也就是2147483647,假如输入2147483648,转化时就会溢出,变成32个1(补码),-2 //解决办法是对传入的n进行修改,不修改方法定义本身的参数int与返回值int,而是在方法内部放一个原创 2017-04-10 22:50:17 · 210 阅读 · 0 评论 -
leetcode [Rotate Array]//待整理多种解法
public class Solution { //解法一:利用一个辅助数组可以要求,但不是很好,题目要求空间复杂度是O(1)的 public void rotate(int[] nums, int k) { //题目要求:Rotate an array of n elements to the right by k steps. //先处理k的值,鲁棒性原创 2017-04-08 23:51:57 · 236 阅读 · 0 评论 -
leetcode [Reverse Bits]//待整理多种解法
public class Solution { // you need treat n as an unsigned value //所以方法中参数类型如果是int,最大能接受是0+31个1,也就是2147483647,假如输入2147483648,转化时就会溢出,变成32个1(补码),-2 //解决办法是对传入的n进行修改,不修改方法定义本身的参数int与返回值int,而是在方法内部放一个原创 2017-04-09 22:51:00 · 319 阅读 · 0 评论 -
leetcode [Factorial Trailing Zeroes]
/*The idea is: 1.The ZERO comes from 10. 2.The 10 comes from 2 x 5 3.And we need to account for all the products of 5 and 2. likes 4×5 = 20 ... 4.So, if we take all the numbers with 5原创 2017-04-08 23:12:52 · 317 阅读 · 0 评论 -
leetcode [Excel Sheet Column Number]
public class Solution { public int titleToNumber(String s) { //即把二十六进制数转化为10进制数 int res = 0; for(int i = 0; i < s.length(); i++){ res = res * 26 + s.charAt(i) - 'A' +原创 2017-04-06 21:02:53 · 512 阅读 · 0 评论 -
leetcode [Majority Element]//待整理多种解法
public class Solution { public int majorityElement(int[] nums) { //解法一:利用Map来统计数组中的元素的出现次数 int res = 0; Map map = new HashMap<>(); for(int i = 0; i < nums.length; i++)原创 2017-04-05 23:12:27 · 342 阅读 · 0 评论 -
leetcode [Excel Sheet Column Title]
public class Solution { public String convertToTitle(int n) { //即把一个十进制数转化为二十六进制数,思路同把十进制数转化为二进制数 StringBuilder res = new StringBuilder(); char temp; while(n != 0){原创 2017-04-04 21:43:36 · 224 阅读 · 0 评论 -
leetcode [Two Sum II - Input array is sorted]
public class Solution { public int[] twoSum(int[] numbers, int target) { //还是采用之前TwoSum的解法,可以有O(N*N),或者O(N)+Map //但这里提到了给定的数组是按升序排列的,那么是否可以利用这个来找到更好的解法? int[] res = new int[2];原创 2017-04-03 21:43:36 · 201 阅读 · 0 评论 -
leetcode [Intersection of Two Linked Lists]//待整理多种解法
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public原创 2017-04-02 23:05:04 · 209 阅读 · 0 评论 -
leetcode [Min Stack]
public class MinStack { /** initialize your data structure here. */ private TreeMap min; private LinkedList stack; public MinStack() { min = new TreeMap<>(); stack = new LinkedLi原创 2017-04-01 20:56:03 · 229 阅读 · 0 评论 -
leetcode [Linked List Cycle]
class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; }}class Solution { public boolean hasCycle(ListNode head) {//这个算法是在判断一个list中是否出现循环的数段,与val有关 //而原题是原创 2017-03-31 22:55:11 · 226 阅读 · 0 评论 -
leetcode [Single Number]
解法一:使用了额外空间,用map来存public class Solution { public int singleNumber(int[] nums) { Map map = new HashMap<>(); for(int val : nums){ if(map.containsKey(val)){ map.put(va原创 2017-03-30 21:55:36 · 211 阅读 · 0 评论 -
leetcode [Valid Palindrome]//待整理多种解法
public class Solution { public boolean isPalindrome(String s) { boolean res = true; Stack stack = new Stack<>(); Queue queue = new LinkedList<>(); s = s.toLowerCase();原创 2017-03-30 21:21:38 · 278 阅读 · 0 评论 -
leetcode [Best Time to Buy and Sell Stock II]//待整理多种解法
class Solution { public int maxProfit(int[] prices) { int res = 0; int low = 0, high = 0; if(prices.length == 0) return 0; high = prices[prices.length - 1];原创 2017-03-29 22:01:59 · 249 阅读 · 0 评论 -
leetcode [Best Time to Buy and Sell Stock]//待整理多种解法
class Solution { public int maxProfit(int[] prices) { //暴力求解,从后往前找每一个数对应前面的数的差,O(N*N),遇到很长的数据会超时 int res = 0; for(int i = prices.length - 1; i >= 0; i--){ for(int j =原创 2017-03-28 21:24:59 · 319 阅读 · 0 评论 -
leetcode [Pascal's Triangle II]//待整理多种解法
public class Solution { public List getRow(int rowIndex) { List res = new ArrayList<>(); for(int i = 0; i <= rowIndex; i++){ res.add(1); for(int j = 0; j + 1 < i;原创 2017-03-27 22:52:57 · 274 阅读 · 0 评论 -
leetcode [Pascal's Triangle]//待整理多种解法
public class Solution { public List> generate(int numRows) { List> res = new ArrayList<>(); List level = null; List lastLevel = null; if(numRows == 0) return res;原创 2017-03-26 22:44:43 · 273 阅读 · 0 评论 -
leetcode [House Robber]
public class Solution { /* 可以看到下面的解法,正确的解法逻辑是很清晰的,最先想解这题的时候想到每四个房子分为一组, 分别为1+3,2+4,1+4的组合,然后去更新,但是逻辑很不清晰 下面的正确解法中用prevNo,prevYes来模拟了偷盗当前房子可能的情况 */ public int rob(int[] nums) {原创 2017-04-12 21:38:23 · 221 阅读 · 0 评论 -
leetcode [Happy Number]//待整理多种解法
public class Solution { public int divedeAndProduct(int n){ int res = 0; while(n != 0){ int temp = n % 10; res += temp * temp; n = n / 10; } //System.out.println(res); return res;原创 2017-04-11 22:07:07 · 230 阅读 · 0 评论 -
leetcode [Path Sum]//待整理多种解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2017-03-25 22:16:44 · 238 阅读 · 0 评论 -
leetcode [Minimum Depth of Binary Tree]//待整理多种解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2017-03-25 21:33:39 · 290 阅读 · 0 评论 -
leetcode [Balanced Binary Tree]//待整理多种解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2017-03-24 23:48:07 · 243 阅读 · 0 评论 -
leetcode [Convert Sorted Array to Binary Search Tree]//待整理多种解法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ /* 本题要求求出给定有序数列的高度平衡二原创 2017-03-23 22:30:49 · 219 阅读 · 0 评论 -
leetcode [Binary Tree Level Order Traversal II]
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2017-03-20 22:44:31 · 187 阅读 · 0 评论 -
leetcode [Maximum Depth of Binary Tree]
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2017-03-19 22:44:34 · 248 阅读 · 0 评论 -
leetcode [Symmetric Tree]
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2017-03-18 22:21:17 · 209 阅读 · 0 评论 -
leetcode [Merge Sorted Array]
public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { //虽然题目要求返回排序的结果是nums1,但是可以新开一个数组,到时候赋值给nums1就行 //要不然直接在nums1上移动修改太麻烦,而且浪费时间 int[] res = new int[n原创 2017-03-16 22:38:57 · 231 阅读 · 0 评论 -
leetcode [Same Tree]
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2017-03-17 22:48:21 · 214 阅读 · 0 评论 -
leetcode [Remove Duplicates from Sorted List]
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode dele原创 2017-03-15 23:01:42 · 203 阅读 · 0 评论 -
leetcode [Climbing Stairs]
public int climbStairs(int n) { /*if(n == 1) return 1; if(n == 2) return 2; return climbStairs(n - 1) + climbStairs(n - 2);*/ //用递归会超时,而且效率低 ArrayList list = ne原创 2017-03-14 22:28:20 · 223 阅读 · 0 评论 -
leetcode [Sqrt(x)]
二分搜索法求平方根:public class Solution { public int mySqrt(int x) { //既然是寻找一个数的平方根,就属于查找的方式,查找中有二分查找法 //二分查找法求平方根,记住算法,无限逼近 if (x == 0) return 0; int left = 1, right = x, ans原创 2017-03-13 23:08:41 · 257 阅读 · 0 评论 -
leetcode [Reverse Integer]
public class Solution { public int reverse(long a) {//题目中提到如果超过32-bit的返回0,想到的解决办法是用long来存,判断结果是否大于Integer.MAX_VALUE或小于Integer.MIN_VALUE long temp = 0; int res = 0; if(a < 0){ a = Ma原创 2017-02-11 15:02:34 · 246 阅读 · 0 评论 -
leetcode [Palindrome Number]
public class Solution { public static int Reverse(long a){ long temp = 0; int res = 0; if(a < 0){ a = Math.abs(a); while(a != 0){ temp = temp * 10 + (a % 10); a = a / 10; }原创 2017-02-11 15:28:55 · 188 阅读 · 0 评论 -
leetcode [Roman to Integer]
public class Solution { public int romanToInt(String s){ int res = 0; int i = 0; Map map = new HashMap(); map.put('I', 1); map.put('V', 5); map.put('X', 10); map.put('L', 50); map.put原创 2017-02-12 14:41:59 · 184 阅读 · 0 评论 -
leetcode [Longest Common Prefix]
public class Solution { public String longestCommonPrefix(String[] strs){ String res = ""; boolean flag = true; for(int i = 0; flag; i++){ if(strs.length == 0){ flag = false;//处理空串的情况原创 2017-02-12 23:43:36 · 234 阅读 · 0 评论 -
leetcode [Add Two Numbers]
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode addT原创 2017-02-16 22:38:39 · 180 阅读 · 0 评论 -
leetcode [Valid Parentheses]
public class Solution { public boolean isValid(String s) { boolean res = true; Stack stack = new Stack(); Character ch, chPop; for(int i = 0; i < s.length(); i++){原创 2017-02-18 00:01:08 · 328 阅读 · 0 评论 -
leetcode [Merge Two Sorted Lists]
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode merg原创 2017-02-27 22:11:37 · 205 阅读 · 0 评论 -
leetcode [Remove Duplicates from Sorted Array]
import java.util.Arrays;import java.util.Vector;public class Solution { public int removeDuplicates(int[] nums) {//注意题中提到给的数组是排好序的,所以可以一步步往后判断 int res = nums.length; int swap =原创 2017-03-01 22:31:19 · 210 阅读 · 0 评论 -
leetcode [Remove Element]
/* *算法思路,题目中提到返回除val外的数组元素个数,那么从头遍历数组,设res的初值为数组的长度,遇到val就res--; *同时题目中要求数组的前res个元素不能含有val,而且元素的顺序可以改变 *那么就将遍历时遇到的val放到数组的最后,设一个j,依次从数组的最后往前挪 *放到数组的最后采用交换nums[i]与nums[j],而且交换后还要判断nums[i]是否是val,所以原创 2017-03-02 21:06:17 · 188 阅读 · 0 评论