这几天把之前的设计模式回顾了一遍,整理了一点以前的项目。同学说,打算刷leetcode题目,也勾起了我的兴趣,索性也刷一些题目,再提高一些内功。刚开始进去,leetcode随机分配的题目,直接也就做了,在后来,按顺序做,现在做到了第五题。大概做了如下题目:

1. Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

这个题目简单,直接上代码如下:

  1. public int[] twoSum(int []nums,int target){
  2. if(nums==null||nums.length==0){
  3. return null;
  4. }
  5. int re[]=new int[2];
  6. for(int i=0;i<nums.length;i++){
  7. for(int j=i+1;j<nums.length;j++){
  8. if(nums[i]+nums[j]==target){
  9. re[0]=i+1;
  10. re[1]=j+1;
  11. }
  12. }
  13. }
  14. return re;
  15. }

2. Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

这个题也不难,主要考虑好进位就行,稍微费点功夫,代码如下:

  1. private ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  2. ListNode re=null,head=re;
  3. int tmp;
  4. int flag=0;
  5. while(l1!=null||l2!=null){
  6. if(l1==null&&l2!=null){
  7. tmp=l2.val+flag;
  8. }else if(l1!=null&&l2==null){
  9. tmp=l1.val+flag;
  10. }else{
  11. tmp=l1.val+l2.val+flag;
  12. }
  13. if(tmp/10!=0){//进位
  14. ListNode lt=new ListNode(tmp%10);
  15. flag=1;
  16. if(l1==null&&l2!=null){
  17. l1=null;
  18. l2=l2.next;
  19. }else if(l1!=null&&l2==null){
  20. l1=l1.next;
  21. l2=null;
  22. }else{
  23. l1=l1.next;
  24. l2=l2.next;
  25. }
  26. if(re!=null){
  27. re.next=lt;
  28. re=re.next;
  29. }else{
  30. re=lt;
  31. head=re;
  32. }
  33. //最后进位
  34. if(l1==null&&null==l2){
  35. ListNode lt1=new ListNode(tmp/10);
  36. re.next=lt1;
  37. re=re.next;
  38. }
  39. }else{
  40. ListNode lt=new ListNode(tmp%10);
  41. flag=0;
  42. if(l1==null&&l2!=null){
  43. l1=null;
  44. l2=l2.next;
  45. }else if(l1!=null&&l2==null){
  46. l2=null;
  47. l1=l1.next;
  48. }else{
  49. l1=l1.next;
  50. l2=l2.next;
  51. }
  52. if(re!=null){
  53. re.next=lt;
  54. re=re.next;
  55. }else{
  56. re=lt;
  57. head=re;
  58. }
  59. }
  60. }
  61. return head;
  62. }

3.Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

这道题也不是太难,主要需要考虑到各种情况,刚开始的时候,没有考虑是一种包含元素的关系,直接用队列删除首元素,因此,AC不了。调整代码如下:

  1. public int lengthOfLongestSubstring(String s) {
  2. if(s.isEmpty()){
  3. return 0;
  4. }
  5. int max=0,left=0;
  6. Queue<Character> q=new LinkedList<Character>();
  7. for(int i=0;i<s.length();i++){
  8. if(q.contains(s.charAt(i))){
  9. while(left<i&&s.charAt(left)!=s.charAt(i)){
  10. q.remove(s.charAt(left));
  11. left++;
  12. }
  13. left++;
  14. }else{
  15. q.offer(s.charAt(i));
  16. max=Math.max(max,i-left+1);
  17. }
  18. }
  19. return max;
  20. }

4. Median of Two Sorted Arrays

  There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

这个题初一看,很简单的样子,但是,确实一个难度很大的题目,想到了前几天用到的二路归并的思想。代码如下:

  1. public double findMedianSortedArrays(int nums1[], int nums2[]) {
  2. if((nums1.length+nums2.length)%2==1)
  3. return merge(nums1,nums2,0,nums1.length-1,0,nums2.length-1,(nums1.length+nums2.length)/2+1);
  4. else
  5. return (merge(nums1,nums2,0,nums1.length-1,0,nums2.length-1,(nums1.length+nums2.length)/2)
  6. +merge(nums1,nums2,0,nums1.length-1,0,nums2.length-1,(nums1.length+nums2.length)/2+1))/2.0;
  7. }
  8. private int merge(int A[], int B[], int i, int i2, int j, int j2, int k){
  9. int m = i2-i+1;
  10. int n = j2-j+1;
  11. if(m>n)
  12. return merge(B,A,j,j2,i,i2,k);
  13. if(m==0)
  14. return B[j+k-1];
  15. if(k==1)
  16. return Math.min(A[i],B[j]);
  17. int posA = Math.min(k/2,m);
  18. int posB = k-posA;
  19. if(A[i+posA-1]==B[j+posB-1])
  20. return A[i+posA-1];
  21. else if(A[i+posA-1]<B[j+posB-1])
  22. return merge(A,B,i+posA,i2,j,j+posB-1,k-posA);
  23. else
  24. return merge(A,B,i,i+posA-1,j+posB,j2,k-posB);
  25. }

130. Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region.

For example,

  1. X X X X
  2. X O O X
  3. X X O X
  4. X O X X

After running your function, the board should be:

  1. X X X X
  2. X X X X
  3. X X X X
  4. X O X X
    这个题主要是图的深度优先和广度优先遍历问题,在深度优先的时候,使用递归实现的时候,是不能AC的,会报栈溢出的错误,因此修改为了非递归的方式:
  1. public void solve(char[][] board) {
  2. if(board==null||board.length==0){
  3. return;
  4. }
  5. int m=board.length;
  6. int n=board[0].length;
  7.  
  8. for(int i=0;i<m;i++){
  9. if(board[i][0]=='O'){
  10. dfs(board,i,0);
  11. }
  12. if(board[i][n-1]=='O'){
  13. dfs(board,i,n-1);
  14. }
  15. }
  16.  
  17. for(int j=0;j<n;j++){
  18. if(board[0][j]=='O'){
  19. dfs(board,0,j);
  20. }
  21. if(board[m-1][j]=='O'){
  22. dfs(board,m-1,j);
  23. }
  24. }
  25. print(board);
  26. System.out.println("===============");
  27. for(int i=0;i<m;i++){
  28. for(int j=0;j<n;j++){
  29. if(board[i][j]=='O'){
  30. board[i][j]='X';
  31. }else if(board[i][j]=='#'){
  32. board[i][j]='O';
  33. }
  34. }
  35. }
  36. print(board);
  37. }
  38. /**
  39. * @Title dfs
  40. * @Description 采用递归会报java.lang.StackOverflowError错误。非递归实现的时候,在if中加入continue。之后得不到正确结果。
  41. * @param board
  42. * @param i
  43. * @param j
  44. * @Return void
  45. * @Throws
  46. * @user Administrator
  47. * @Date 2016年1月24日
  48. */
  49. public void dfs(char[][]board,int i,int j){
  50.  
  51. //非递归实现
  52. Stack<Pos> s=new Stack<Pos>();
  53. Pos pos=new Pos(i,j);
  54. s.push(pos);
  55. board[i][j]='#';
  56. int m=board.length;
  57. int n=board[0].length;
  58. while(!s.isEmpty()){
  59. Pos top=s.pop();
  60. if(top.x>0&&board[top.x-1][top.y]=='O'){
  61. Pos up=new Pos(top.x-1,top.y);
  62. s.push(up);
  63. board[up.x][up.y]='#';
  64. }
  65. if(top.x<m-1&&board[top.x+1][top.y]=='O'){
  66. Pos down=new Pos(top.x+1,top.y);
  67. s.push(down);
  68. board[down.x][down.y]='#';
  69. }
  70. if(top.y>0&&board[top.x][top.y-1]=='O'){
  71. Pos left=new Pos(top.x,top.y-1);
  72. s.push(left);
  73. board[left.x][left.y]='#';
  74. }
  75. if(top.y<n-1&&board[top.x][top.y+1]=='O'){
  76. Pos right=new Pos(top.x,top.y+1);
  77. s.push(right);
  78. board[right.x][right.y]='#';
  79. }
  80. }
  81. }
  82. private class Pos{
  83. int x,y;
  84. public Pos(int x,int y){
  85. this.x=x;
  86. this.y=y;
  87. }
  88. };

206. Reverse Linked List

Reverse a singly linked list.

这个题很简单:

  1. public ListNode reverseList(ListNode head) {
  2. ListNode re=null;
  3. Stack<ListNode> s=new Stack<ListNode>();
  4. while(head!=null){
  5. ListNode tmp=new ListNode(head.val);
  6. s.push(tmp);
  7. head=head.next;
  8. }
  9. ListNode st=null,h=re;
  10. while(!s.isEmpty()){
  11. st=s.pop();
  12. if(re==null){
  13. re=st;
  14. h=re;
  15. }else{
  16. re.next=st;
  17. re=re.next;
  18. }
  19. }
  20. return h;
  21. }

328. Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.

Example:
Given 1->2->3->4->5->NULL,
return 1->3->5->2->4->NULL.

这个问题也不算太难:

  1. public ListNode oddEvenList(ListNode head) {
  2. if(head==null)return head;
  3. ListNode odd=head,even=head.next,evenHead=even;
  4. while(odd.next!=null&&even.next!=null){
  5. odd.next=even.next;
  6. odd=odd.next;
  7. even.next=odd.next;
  8. even=even.next;
  9. }
  10. odd.next=evenHead;
  11. return head;
  12. }

未完待续……

[leetcode]leetcode初体验的更多相关文章

  1. .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验

    不知不觉,“.NET平台开源项目速览“系列文章已经15篇了,每一篇都非常受欢迎,可能技术水平不高,但足够入门了.虽然工作很忙,但还是会抽空把自己知道的,已经平时遇到的好的开源项目分享出来.今天就给大家 ...

  2. Xamarin+Prism开发详解四:简单Mac OS 虚拟机安装方法与Visual Studio for Mac 初体验

    Mac OS 虚拟机安装方法 最近把自己的电脑升级了一下SSD固态硬盘,总算是有容量安装Mac 虚拟机了!经过心碎的安装探索,尝试了国内外的各种安装方法,最后在youtube上找到了一个好方法. 简单 ...

  3. Spring之初体验

                                     Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...

  4. Xamarin.iOS开发初体验

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKwAAAA+CAIAAAA5/WfHAAAJrklEQVR4nO2c/VdTRxrH+wfdU84pW0

  5. 【腾讯Bugly干货分享】基于 Webpack & Vue & Vue-Router 的 SPA 初体验

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57d13a57132ff21c38110186 导语 最近这几年的前端圈子,由于 ...

  6. 【Knockout.js 学习体验之旅】(1)ko初体验

    前言 什么,你现在还在看knockout.js?这货都已经落后主流一千年了!赶紧去学Angular.React啊,再不赶紧的话,他们也要变out了哦.身旁的90后小伙伴,嘴里还塞着山东的狗不理大蒜包, ...

  7. 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验

    在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...

  8. 百度EChart3初体验

    由于项目需要在首页搞一个订单数量的走势图,经过多方查找,体验,感觉ECharts不错,封装的很细,我们只需要看自己需要那种类型的图表,搞定好自己的json数据就OK.至于说如何体现出来,官网的教程很详 ...

  9. Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验

    Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...

  10. Docker初体验

    ## Docker初体验 安装 因为我用的是mac,所以安装很简单,下载dmg下来之后拖拽安装即可完成. 需要注意的就是由于之前的docker是基于linux开发,不支持mac,所以就出现了docke ...

随机推荐

  1. C#基础强化-进程操作

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  2. BootLoader的架构设计

    @注:黄色部分代表根据不同的开发板进行处理.

  3. Java中堆的实现类PriorityQueue队列接口Queue

    Application:这层的职责是对接收到的数据做一些非业务性验证,事务的控制,最重要的是协调多个聚合之间的操作.这里应该可以清晰的表达出整个操作所做的事情,并且与通用语言是一致的. 以上我们讲到可 ...

  4. mysql表单输入数据出现中文乱码解决方法

    MySQL会出现中文乱码的原因在于1.server本身设定问题,一般来说是latin1 2.建库建表时没有制定编码格式. 解决方法: 1.建库的时候 CREATE DATABASE test CHAR ...

  5. python 单例模式

    单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对象的类必须保证只有一个实例存在 用装饰器方式实现单例模式 #!/usr/bin/python # coding=utf-8 d ...

  6. 第一章 Part 1/2 Git 一览

    虽然这个系列的文章主要关注的是Github,然而首先了解下Git的基本概念和名词也是非常有帮助的. 工作目录(Working Directory) 工作目录是你个人计算机上的一个目录.在该目录下,每一 ...

  7. R语言:ggplot2精细化绘图——以实用商业化图表绘图为例

    本文版权归http://www.cnblogs.com/weibaar 本文旨在介绍R语言中ggplot2包的一些精细化操作,主要适用于对R画图有一定了解,需要更精细化作图的人,尤其是那些刚从exce ...

  8. ACM之鸡血篇

    一匹黑马的诞生 故事还要从南京现场赛讲起,话说这次现场赛,各路ACM英雄豪杰齐聚南京,为争取亚洲总舵南京分舵舵主之职位,都使出了看 家本领,其中有最有实力的有京城两大帮清华帮,北大帮,南郡三大派上交派 ...

  9. mysql_config 问题

    1 .you should have mysql_config available in $PATH For CentOS: yum install mysql-devel For openSUSE: ...

  10. getPx function

    function getPX(str){  return str.substring(0,str.indexOf('px'));}