![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 60
小白在敲代码
这个作者很懒,什么都没留下…
展开
-
Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".public class Solution { public String addBinary(String a, String b) {原创 2015-05-04 22:18:36 · 369 阅读 · 0 评论 -
Intersection of Two Linked Lists(有优化)
Write a program to find the node at which the intersection of two singly linked lists begins.A: a1 → a2 ↘ c1 → c2 → c3原创 2015-04-27 15:25:17 · 237 阅读 · 0 评论 -
Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5/** * Definition for si原创 2015-04-27 12:59:09 · 218 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You may not原创 2015-04-25 15:59:15 · 309 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on原创 2015-04-26 09:55:07 · 306 阅读 · 0 评论 -
Excel Sheet Column Title
public class Solution { //runtime = 171ms public String convertToTitle(int n) { StringBuilder s =new StringBuilder(); char ch[] = new char[50]; if (n <= 0){ return "原创 2015-04-17 10:01:08 · 250 阅读 · 0 评论 -
Valid Sudoku
论看懂题目的重要性!!!这道题目实际是要求判断给出的数独是否是有效数独(每行每列及每个小九宫格都没有重复数字),而不是填充数独!没有看清题目的人绞尽脑汁地在纸上把数独填满了,又绞尽脑汁地想着怎样用代码实现,结果知道真相后,只能绞尽脑汁地骂自己。。。//分别对行,列,以及九宫格进行判断,确认是否每行、每列以及原创 2015-04-16 20:23:32 · 232 阅读 · 0 评论 -
Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ..原创 2015-04-09 21:09:36 · 256 阅读 · 0 评论 -
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),原创 2015-04-09 14:00:02 · 267 阅读 · 0 评论 -
Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all va原创 2015-04-16 11:52:20 · 225 阅读 · 0 评论 -
Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists./** * Definition for singly-linked list. * public class原创 2015-04-27 11:44:51 · 222 阅读 · 0 评论 -
Remove Nth Node From End of List
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 li原创 2015-04-27 16:47:32 · 217 阅读 · 0 评论 -
判断数组中是否有重复数字(two ways +位运算)
//判断数组中是否有重复的数字,有则返回true,没有则返回false//方法一:先用时间复杂度为O(nlogn)的排序将数组重建,然后遍历寻找 //方法二:新建一个数组型hash表,key存num[i], value存num[i]出现的次数。时间复杂度为O(n),空间复杂度为O(n) public static boolean Dup(int[] num){ int hash[]原创 2015-04-28 21:47:58 · 1663 阅读 · 0 评论 -
Sort Colors (two ways)
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0,原创 2015-04-28 18:21:06 · 232 阅读 · 0 评论 -
Swap Nodes in Pairs(递归和非递归)
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You原创 2015-04-28 13:00:03 · 337 阅读 · 0 评论 -
Linked List Cycle I / II
Given a linked list, determine if it has a cycle in it. Can you solve it without using extra space?/** Definition for singly-linked list.*/class ListNode { int val; ListNode next;原创 2015-04-28 08:37:25 · 250 阅读 · 0 评论 -
Search Insert Position(two ways)
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.He原创 2015-04-28 15:58:00 · 249 阅读 · 0 评论 -
Container With Most Water (待消化)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin原创 2015-04-30 20:45:19 · 376 阅读 · 0 评论 -
Remove Duplicates from Sorted List II (read and think again!)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1-原创 2015-04-27 20:31:48 · 401 阅读 · 0 评论 -
Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3./** * Definition for s原创 2015-04-27 17:31:58 · 168 阅读 · 0 评论 -
Insertion Sort List
Sort a linked list using insertion sort.public class Whynot { public ListNode insertionSortList(ListNode head) { if (head == null){ return null; } ListNode dummy = new ListNode(-1); dum原创 2015-04-27 21:56:52 · 253 阅读 · 0 评论 -
Implement strStr()(3 ways)
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.public class Solution { public int strStr(String haystack, String needle) { char原创 2015-04-14 22:10:05 · 240 阅读 · 0 评论 -
Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.tips:1~9: {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};10~90: {"X",原创 2015-04-16 15:46:31 · 273 阅读 · 0 评论 -
Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.单看这句会觉得这道题so easy,但是再看下面的note:Your solution should be in logarithmic time complexity(时间复杂度为O(logN)).我说怎么用一开始的解法一直提示"Time limit ex原创 2015-03-31 14:20:46 · 252 阅读 · 0 评论 -
Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.Could negative integers be palindromes? (ie, -1) ——NO!public class Solution { public boolean isPalindrome(int x) {原创 2015-03-31 09:15:28 · 231 阅读 · 0 评论 -
String to Integer
Q: Implement atoi to convert a string to an integer.昨天很巧地在《剑指offer》的开篇看到了这个例子,作者举这个例子是为了强调程序的鲁棒性,鼓励我们在写代码之前就想好测试用例,避免程序漏洞导致系统运行崩溃,其实这在前面也提到过,还是那句话,想好所有可能会出现的情况再开始敲。public class Solution {原创 2015-03-30 20:05:43 · 262 阅读 · 0 评论 -
Remove Element
哎呀呀终于在中午吃饭前敲出了一个"Accepted",虽然属于Easy级别,但是激动之情仍然是无以言表呀呀呀!!Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed原创 2015-03-29 10:35:57 · 264 阅读 · 0 评论 -
Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.public class Solutio原创 2015-03-29 15:45:55 · 354 阅读 · 0 评论 -
Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321想要看到一个"Accepted"真的好艰难呀!每一次敲完之后都眼巴巴地等着,结果等到的不是"Compile error"就是"runtime error"。今天的这道题其实也是以前做过的,但是我一开原创 2015-03-28 14:45:27 · 346 阅读 · 0 评论 -
Valid Palindrome有效回文
刷了几天的题,终于遇上连题目都看不懂的题了,瞬间觉得高大上,果然我们每个人的崇拜感都是来自于对未知区域的盲目崇拜,如果你踏进了那片区域,会发现也不过如此。期待我会有感到“不过如此”的那一天。Given a string, determine if it is a palindrome, considering onlyalphanumeric characters (字母数字)an原创 2015-03-30 11:08:49 · 362 阅读 · 0 评论 -
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原创 2015-03-27 12:45:41 · 271 阅读 · 0 评论 -
Reverse Bits
刷了两题才知道,原来我需要提高的不仅是刷题的数量,更重要的还有做题的效率。天哪,做一道题恨不得要一天,200多题我得做到什么时候才行啊!Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 0000001010010原创 2015-03-26 16:29:08 · 346 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2015-03-31 19:01:54 · 268 阅读 · 0 评论 -
Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and co原创 2015-04-22 16:40:02 · 264 阅读 · 0 评论 -
ZigZag Conversion
//时间复杂度为O(n),运行时间为375mspublic class Solution { public String convert(String s, int nRows) { char str[] = s.toCharArray(); int len = str.length; if ((len == 0) || (nRows == 1)原创 2015-04-15 19:40:33 · 333 阅读 · 0 评论 -
Rotate Array(3 ways)
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Could you do it in-place with O(1) extra space?原创 2015-04-12 13:40:14 · 256 阅读 · 0 评论 -
House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2015-04-08 15:49:54 · 288 阅读 · 0 评论 -
Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?//161mspublic class Solution原创 2015-04-08 20:13:02 · 270 阅读 · 0 评论 -
Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from原创 2015-04-23 16:25:04 · 330 阅读 · 0 评论 -
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.找出所有字符串最长公共前缀public class Solution { //runtime = 291ms public String longestCommonPrefix(String[] strs) {原创 2015-04-23 09:33:53 · 269 阅读 · 0 评论