leetcode
Genius_yyyyyyyyeah
...
展开
-
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 · 334 阅读 · 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 · 267 阅读 · 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 · 330 阅读 · 1 评论 -
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 · 304 阅读 · 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 · 164 阅读 · 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 · 414 阅读 · 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 · 233 阅读 · 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 · 168 阅读 · 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 · 650 阅读 · 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 · 1433 阅读 · 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 · 288 阅读 · 0 评论 -
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 · 514 阅读 · 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 · 2296 阅读 · 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 · 600 阅读 · 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 · 672 阅读 · 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 · 230 阅读 · 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 · 398 阅读 · 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 · 229 阅读 · 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 · 241 阅读 · 0 评论 -
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 · 234 阅读 · 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 · 435 阅读 · 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 · 333 阅读 · 0 评论 -
Leetcode:513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree.Example 1: Input:2/ \ 1 3Output: 1 Example 2: Input: 1 / \ 2 3 / / \4 5 6 / 7Output: 7 Note原创 2017-11-03 11:33:08 · 372 阅读 · 0 评论 -
Leetcode:442. Find All Duplicates in an Array 找数组中的重复数字
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements that appear twice in this array.Could you do it without extra space原创 2017-11-03 11:08:55 · 381 阅读 · 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 · 312 阅读 · 0 评论 -
Leetcode:485. Max Consecutive Ones 找二进制串连续1最多是多少
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consecutive 1s.原创 2017-11-01 11:35:35 · 307 阅读 · 0 评论 -
Leetcode:136.Single Number 找到数组里面的非重复数字,其他数字也最多重复两次
题目:Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra原创 2017-11-01 11:15:56 · 298 阅读 · 0 评论 -
Leetcode:292. Nim Game 是否能赢得比赛
题目:You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be t原创 2017-11-01 10:57:34 · 329 阅读 · 0 评论 -
Leetcode:258.Add Digits 加数字,直到为个位数
package genius.wyx.Algorithms;public class WYX258 { public static int addDigits(int num) { int rev = num; while(true){ rev = med(rev); if (rev<10) {原创 2017-10-31 22:16:51 · 472 阅读 · 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 · 435 阅读 · 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 · 221 阅读 · 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 · 248 阅读 · 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 · 202 阅读 · 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 · 447 阅读 · 0 评论 -
Leetcode:171. Excel Sheet Column Number 求Excel表字母对应的行号
描述: Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example:A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28原创 2017-11-01 19:27:12 · 276 阅读 · 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 · 188 阅读 · 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 · 181 阅读 · 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 · 443 阅读 · 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 · 166 阅读 · 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 · 170 阅读 · 0 评论