Leetcode
Leetcode
Genius_yyyyyyyyeah
...
展开
-
Leetcode:19 Remove Nth Node From End of List(在链表中删除倒数第n个数)
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked l原创 2017-12-13 11:28:21 · 230 阅读 · 0 评论 -
lettcode:540. Single Element in a Sorted Array(已排序数组中出现一次的数字,其他数字出现两次)
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once.Example 1:Input: [1,1,2,3,原创 2017-12-19 20:12:12 · 262 阅读 · 0 评论 -
Leetcode:191. Number of 1 Bits(返回一个数字对应二进制中1的个数)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 0000000000000原创 2017-12-19 20:37:40 · 277 阅读 · 0 评论 -
Leetcode:48. Rotate Image(数组选择90度)
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D matrix directl原创 2017-12-12 19:03:53 · 324 阅读 · 0 评论 -
Leetcode:383.Ransom Note(统计每个字符串每个字母的个数)
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; oth原创 2017-12-04 17:49:02 · 424 阅读 · 0 评论 -
Leetcode:453. Minimum Moves to Equal Array Elements
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example:Input: [1,2,3]Output:原创 2017-12-05 21:06:23 · 228 阅读 · 0 评论 -
Leetcode:219. Contains Duplicate II(数组是否含有重复元素)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.原创 2017-12-06 19:11:42 · 658 阅读 · 0 评论 -
Leetcode:454. 4Sum II (解决时间和空间复杂度是关键)
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.To make problem a bit easier, all A, B, C, D have same length o原创 2017-12-06 20:18:02 · 586 阅读 · 0 评论 -
Leetcode:169. Majority Element(找到数组中出现次数最多的元素)
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element always原创 2017-12-04 11:46:06 · 2257 阅读 · 0 评论 -
Leetcode:73.Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.public void setZeroes(int[][] matrix) { int col0 = 1, rows = matrix.length, cols = matrix[0].length;原创 2017-11-29 10:50:26 · 161 阅读 · 0 评论 -
Leetcode:728. Self Dividing Numbers(是否为分裂数字)
A self-dividing number is a number that is divisible by every digit it contains.For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.Also, a self-dividing num原创 2017-11-29 11:39:37 · 290 阅读 · 0 评论 -
Leetcode:263. Ugly Number(factors是否只包含2 3 5)
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it in原创 2017-11-29 12:07:15 · 317 阅读 · 1 评论 -
Leetcode: 217. Contains Duplicate(数组是否包含重复数字)
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is原创 2017-12-03 20:49:44 · 507 阅读 · 0 评论 -
Leetcode:202. Happy Number
Write an algorithm to determine if a number is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of i原创 2017-11-27 10:27:27 · 192 阅读 · 0 评论 -
Leetcode:203 Remove Linked List Elements(删除链表中的某个元素))
Remove all elements from a linked list of integers that have value val.Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5Credits: Special thanks to @mithmatt for a原创 2017-11-27 11:01:15 · 389 阅读 · 0 评论 -
Leetcode:204. Count Primes 求素数的优化问题
Description:Count the number of prime numbers less than a non-negative number, n.Credits: Special thanks to @mithmatt for adding this problem and creating all test cases.最常见的一种代码。int count = 0;原创 2017-11-27 11:54:42 · 213 阅读 · 0 评论 -
Leetcode:205. Isomorphic Strings (同构字符串)
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another chara原创 2017-11-27 20:27:17 · 323 阅读 · 0 评论 -
Leetcode:713.Subarray Product Less Than K
题目描述: 题目:Your are given an array of positive integers nums.Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k.Example 1:Input: nu原创 2017-11-04 11:00:22 · 630 阅读 · 0 评论 -
Leetcode:121. Best Time to Buy and Sell Stock
题目: Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),原创 2017-11-05 11:41:37 · 160 阅读 · 0 评论 -
Leetcode:27.Remove Element
题目:Given an array and a value, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-plac原创 2017-11-06 10:34:13 · 227 阅读 · 0 评论 -
Leetcode:36.Valid Sudoku 初始化数独数组是否合法判断
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’. 分析:乍一看这个题感觉很难,但是其实只要分三种情况讨论即可、原创 2017-11-06 11:04:58 · 403 阅读 · 0 评论 -
Leetcode:349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note: Each element in the result must be unique. The result can be in原创 2017-11-10 20:32:59 · 183 阅读 · 0 评论 -
Leetcode:112. Path Sum (求Tree中是否存在路径的和等于给定值)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum =原创 2017-11-10 21:04:23 · 175 阅读 · 0 评论 -
Leetcode:111. Minimum Depth of Binary Tree(求Tree的最小的深度)
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 给定一个二叉树,找到它的最小深度。 最小深度是沿着从根节点到最近的叶节点的最短路原创 2017-11-10 21:42:14 · 152 阅读 · 0 评论 -
Leetcode:350. Intersection of Two Arrays II 求两个数组的交集
Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].Note: Each element in the result should appear as many times as it原创 2017-11-11 13:12:43 · 186 阅读 · 0 评论 -
Leetcode:475. Heaters (利用二分binary search)
题目: Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.Now, you are given positions of houses and heaters on a horizontal原创 2017-11-11 14:41:30 · 579 阅读 · 0 评论 -
Leetcode:167. Two Sum II - Input array is sorted 求数组中的两个数的和等于给定值,求这两个数的下标
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers suc原创 2017-11-11 15:06:25 · 262 阅读 · 0 评论 -
Leetcode:367 Valid Perfect Square(是否为平方数)
Given a positive integer num, write a function which returns True if num is a perfect square else False.Note: Do not use any built-in library function such as sqrt.Example 1:Input: 16 Returns: True E原创 2017-11-11 15:29:59 · 214 阅读 · 0 评论 -
Leetcode:628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note: The length of原创 2017-11-12 11:48:59 · 205 阅读 · 0 评论 -
Leetcode14
编写一个函数来查找字符串数组中最长的公共前缀字符串。package genius.wangyuxian.Algorithms;public class WYX14 { public static String longestCommonPrefix(String[] strs) { if(strs == null || strs.length == 0) return原创 2017-10-18 13:17:54 · 168 阅读 · 0 评论 -
Leetcode3 求最长子串的长度
思路: 1.借助HashMap一个一个put进去。 2.put进去的元素会在后续进行判断,如果map里面已经有了当前put的元素,那么更新j,j为当前j和当前字符串出现的键的值加1的最大值。 3.最大值为当前最大和i减去j+1的最大值。(因为不考虑重复的因素,最大的将是i,但是显然不成立。重复元素的到来必然引发i前去重复元素的位置再加上1,额。。。。有点绕)package genius.wan原创 2017-10-18 14:34:01 · 439 阅读 · 0 评论 -
LeetCode58 返回最后一个字符串的最后一个单词的长度
最开始,我的代码在开始输入”a “这种情况下通不过一些用例,知道最后我发现运用s = s.trim()可以解决这个问题,去除字符串开头和结尾的所有的空格。package genius.wyx.Algorithms;public class WYX58 { public static int lengthOfLastWord(String s) { s = s原创 2017-10-20 11:13:38 · 241 阅读 · 0 评论 -
LeetCode231 判断是否为2的幂
正解: return ((n & (n-1))==0 && n>0);我的错误答案:package genius.wyx.Algorithms;public class WYX231 { public static boolean isPowerOfTwo(int n) { String string = Long.toBinaryString(n).toString();原创 2017-10-20 12:53:35 · 215 阅读 · 0 评论 -
Leetcode 22 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is: [ “((()))”, “(()())”, “(())()”, “()(())”原创 2017-10-26 13:07:03 · 157 阅读 · 0 评论 -
Leetcode 520 Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not.We define the usage of capitals in a word to be right when one of the following cases holds:All letters in this word原创 2017-10-26 13:34:33 · 159 阅读 · 0 评论 -
Leetcode100:Same Tree 判断两棵树是否完全相等
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.递归实现public class T原创 2017-10-26 14:02:28 · 430 阅读 · 0 评论 -
Leetcode 657 Judge Route Circle回到原点
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.The move sequence is represented by a原创 2017-10-26 15:10:21 · 299 阅读 · 0 评论 -
Leetcode 344 Reverse String 反转字符串
把字符串转为数组public class Solution { public String reverseString(String s) { char[] word = s.toCharArray(); int i = 0; int j = s.length() - 1; while (i < j) {原创 2017-10-26 16:46:42 · 1414 阅读 · 0 评论 -
Leetcode 566 Reshape the Matrix 将数组转换为我们想要的行和列
In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.You’re given a matrix represented by a two-dimensio原创 2017-10-27 10:51:40 · 197 阅读 · 0 评论 -
Leetcod:557.Reverse Words in a String III 反转字符串中的每个单词。
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.Example 1: Input: “Let’s take LeetCode contest” Out原创 2017-10-31 21:18:15 · 432 阅读 · 0 评论