
Leetcode
算法积累
漁陽
knowledge reshapes destiny
展开
-
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
算法题,拼接出最大值原创 2022-07-04 15:01:09 · 300 阅读 · 0 评论 -
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原创 2016-04-20 12:20:39 · 365 阅读 · 0 评论 -
Given an array of size n, find the majority element. The majority element is the element that appear
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原创 2016-04-20 14:18:07 · 929 阅读 · 0 评论 -
找出来单个数据
存在一个数组,数组大小为2n+1,里面有n对个数,加上另外一个单独出现的数,例如:1,2,2,3,1.(数组是无序的,考虑排序的话一定会超过限制)这5个数中的单独的数就是3,要你用你能想到的最高效率的方法找出来。=====================================int testData[]={1,2,2,3,1};int data=0; for(in原创 2016-04-20 15:41:41 · 381 阅读 · 0 评论 -
罗马数字与整数相互的转换
第13题:整数转换成罗马数字&第14题:罗马数字转换成整数写在前面:这两道题合起来写吧,其实整数转罗马数字我前天就写完了,当我想写罗马数转整数的时候竟然脑子一片空白,想了几分钟就想起来Map,本着学习的目的最终还是不想用Map,坚持C语言,今天脑子里直接涌出了Switch方式转换,看来“蹲在马桶上编程”的方式还是蛮不错的o(^▽^)o整数转罗马数字:主要建立对应转载 2016-04-21 15:23:58 · 2424 阅读 · 0 评论 -
删除有序链表中的重复节点
//表示对递归掌握的不是很熟,链表的操作好多都是递归实现!!!是时候好好学学递归了 Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each element appear only once.For example原创 2016-04-21 17:12:15 · 991 阅读 · 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?Subscribe to see which c原创 2016-04-21 17:36:38 · 760 阅读 · 1 评论 -
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.含义:尼玛!!原创 2016-04-27 18:40:33 · 437 阅读 · 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原创 2016-04-28 11:22:37 · 352 阅读 · 0 评论 -
Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.//求阶乘后面0的个数,可以通过判断此数是零的多好倍数public class Solution { pub原创 2016-05-30 00:15:19 · 398 阅读 · 0 评论 -
Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is原创 2016-05-30 00:22:43 · 357 阅读 · 0 评论 -
char 类型转换成ascii码值
char a='2';char b='A';System.out.println(Integer.parseInt(a+""));System.out.println(Character.valueOf(b)+0);System.out.println(b+0);原创 2016-04-20 11:55:00 · 7307 阅读 · 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 ...原创 2016-04-20 11:22:10 · 365 阅读 · 0 评论 -
Assign Cookies(分蛋糕)
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a c原创 2016-11-25 17:24:34 · 393 阅读 · 0 评论 -
Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely原创 2016-11-25 16:56:22 · 492 阅读 · 0 评论 -
java实现冒泡排序
/** * 对int数组进行升序排序 * * @param sort[]:待排序的数组 * @param asc_type:值为true,表示升序排序;传入值为false,表示降序排序 * @return 返回排序后的int数组 */ public int[] bubble_sort(int sort[],boole...原创 2016-04-18 17:28:05 · 471 阅读 · 0 评论 -
short s1 =1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错;float型float f=3.4是否正确?
short s1 =1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?short s1=1; s1=(short)(s1+1); s1+=1;short s1 = 1; s1 = s1 + 1; (s1+1运算结果是int型,需要强制转换类型) short s1 = 1; s1 += 1;(可以正确编译)原创 2016-04-18 17:35:55 · 1200 阅读 · 0 评论 -
java中常用的几种设计模式
1.单例模式(有的书上说叫单态模式其实都一样)该模式主要目的是使内存中保持1个对象。看下面的例子:package org.sp.singleton;//方法一public class Singleton {//将自身的实例对象设置为一个属性,并加上Static和final修饰符private static final Singleton instance = n原创 2016-04-18 17:59:06 · 541 阅读 · 0 评论 -
leetCode Given an integer (signed 32 bits), write a function to check whether it is a power of 4
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example:Given num = 16, return true. Given num = 5, return false.Follow up: Could you solve it without原创 2016-04-19 12:17:20 · 980 阅读 · 0 评论 -
Nim games 博弈论
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 the原创 2016-04-19 15:05:32 · 458 阅读 · 0 评论 -
Delete Node in a Linked List(有待完善)
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value原创 2016-04-19 19:53:49 · 393 阅读 · 0 评论 -
Given two strings s and t, write a function to determine if t is an anagram of s.
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may a原创 2016-04-19 23:28:09 · 891 阅读 · 0 评论 -
判断二叉树是否相等
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.Subscr原创 2016-04-20 11:00:35 · 608 阅读 · 0 评论 -
链表中环的问题
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?表示对递归真的是比较不会用啊,大神们如果有好的学习递归的信息,留下脚印啊!循环实现表示超时!!!/** * Definition for sin原创 2016-06-01 22:29:02 · 518 阅读 · 0 评论 -
Ugly Number
//丑数就TM丑所谓一个数m是另一个数n的因子,是指n能被m整除,也就是n % m == 0。根据丑数的定义,丑数只能被2、3和5整除。也就是说如果一个数如果它能被2整除,我们把它连续除以2;如果能被3整除,就连续除以3;如果能被5整除,就除以连续5。如果最后我们得到的是1,那么这个数就是丑数,否则不是。 Write a program to check whether原创 2016-04-21 18:10:42 · 345 阅读 · 0 评论 -
Happy Numbe
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 o...原创 2016-04-21 19:26:09 · 386 阅读 · 0 评论 -
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has on原创 2016-04-19 15:30:35 · 1603 阅读 · 0 评论 -
判断一个非负整数是否为2的n次幂?-by大彬
Given an integer, write a function to determine if it is a power of two.Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.===============原创 2016-04-22 11:17:54 · 693 阅读 · 0 评论 -
Overload(重载)Override(重写)
override(重写) 1)方法名、参数、返回值相同。2)子类方法不能缩小父类方法的访问权限。3)子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常)。4)存在于父类和子类之间。5)方法被定义为final不能被重写。overload(重载)1)参数类型、个数、顺序至少有一个不相同。 2)不能重载只有返回值不同的方法名。3)存在于父类和子类、同类中原创 2016-04-18 16:25:51 · 482 阅读 · 0 评论 -
abstract class和interface有什么区别?
Interface只能有成员常量,只能是方法的声明;而abstract class可以有成员变量,可以声明普通方法和抽象方法。声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现该类的情况。不能创建abstract 类的实例。然而可以创建一个变量,其类型是一个抽象类,并让它指向具体子类的一个实原创 2016-04-18 16:28:26 · 410 阅读 · 0 评论 -
int 和 Integer 有什么区别
Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。Int是java的原始数据类型,Integer是java为int提供的封装类。Java为每个原始类型提供了封装类。原始类型封装类booleanBoolean charCharacter byteByte shortShort intInteger longLong floatFloat doubleDou原创 2016-04-18 16:39:15 · 472 阅读 · 0 评论 -
public,private,protected,defalut
访问 Public Protected 缺省的 Private同类 √ √√ √同包 √ √√ ×子类 √ √× ×通用性 √ ×× ×原创 2016-04-18 16:51:51 · 543 阅读 · 0 评论 -
面向对象的特征有哪些方面
1)抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。抽象包括两个方面,一是过程抽象,二是数据抽象。2)继承:继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承。新类继承了原始类的特性,原创 2016-04-18 16:35:06 · 469 阅读 · 0 评论 -
ArrayList和Vector的区别,HashMap和Hashtable的区别
ArrayList和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入数据慢,Vector由于使用了synchronized方法(线程安全),通常性能上较ArrayList差。HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口原创 2016-04-18 17:40:12 · 581 阅读 · 0 评论 -
二叉树最大深度的值
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Subscribe to see which compani原创 2016-04-19 15:49:12 · 455 阅读 · 0 评论 -
First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.Note:原创 2016-09-13 18:20:43 · 347 阅读 · 0 评论 -
Find the Difference
Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was原创 2016-09-13 16:59:37 · 331 阅读 · 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.public class Solution { public ListNode mergeTwoLis转载 2016-06-01 22:35:46 · 354 阅读 · 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. Y转载 2016-06-01 22:44:12 · 398 阅读 · 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),原创 2016-06-21 22:33:34 · 706 阅读 · 0 评论