题目:

  1. Compare two version numbers version1 and version2.
  2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
  3. You may assume that the version strings are non-empty and contain only digits and the . character.
  4. The . character does not represent a decimal point and is used to separate number sequences.
  5. For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level
  6. revision of the second first-level revision.
  7. Here is an example of version numbers ordering:
  8. 0.1 < 1.1 < 1.2 < 13.37

思路:

  • 题意:比较两个版本号字符串的大小
  • 把字符串用split转化为数组,注意split(\.),然后转化为整数数组,遍历比较。注意如果版本号后面都是零的情况

代码:

  1. public class Solution {
  2. public int compareVersion(String version1, String version2) {
  3. String[] v1,v2;
  4. if(version1.indexOf(".") == -1){
  5. v1 = new String[1];
  6. v1[0] = version1;
  7. }else{
  8. v1 = new String[version1.split("\\.").length];
  9. v1 = version1.split("\\.");
  10. }
  11. if(version2.indexOf(".") == -1){
  12. v2 = new String[1];
  13. v2[0] = version2;
  14. }else{
  15. v2 = new String[version2.split("\\.").length];
  16. v2 = version2.split("\\.");
  17. }
  18. int[] array1 = sToInt(v1);
  19. int[] array2 = sToInt(v2);
  20. int nn = Math.min(array1.length,array2.length);
  21. for(int a = 0;a < nn;a++){
  22. if(array1[a] > array2[a]){
  23. return 1;
  24. }else if(array1[a] < array2[a]){
  25. return -1;
  26. }
  27. }
  28. if(array1.length > array2.length){
  29. for(int k = nn; k < array1.length;k++){
  30. if(array1[k] != 0){
  31. return 1;
  32. }
  33. }
  34. return 0;
  35. }else if(array1.length < array2.length){
  36. for(int m = nn;m < array2.length;m++){
  37. if(array2[m] != 0){
  38. return -1;
  39. }
  40. }
  41. return 0;
  42. }
  43. return 0;
  44. }
  45. public int[] sToInt(String[] ss){
  46. int n = ss.length;
  47. int[] result = new int[n];
  48. for(int i = 0;i < n;i++){
  49. try{
  50. result[i] = Integer.parseInt(ss[i]);
  51. }catch(Exception e){
  52. }
  53. }
  54. return result;
  55. }
  56. }

LeetCode(68)-Compare Version Numbers的更多相关文章

  1. LeetCode(165) Compare Version Numbers

    题目 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version ...

  2. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  3. LeetCode(68) Text Justification

    题目 Given an array of words and a length L, format the text such that each line has exactly L charact ...

  4. LeetCode(2)Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  5. 【leetcode❤python】 165. Compare Version Numbers

    #-*- coding: UTF-8 -*-class Solution(object):    def compareVersion(self, version1, version2):       ...

  6. LeetCode(68):文本左右对齐

    Hard! 题目描述: 给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本. 你应该使用“贪心算法”来放置给定的单词:也就是 ...

  7. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  8. 【刷题-LeetCode】165 Compare Version Numbers

    Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...

  9. 165. Compare Version Numbers - LeetCode

    Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...

随机推荐

  1. ROS_Kinetic_x 基於ROS和Gazebo的RoboCup中型組仿真系統(多機器人協作)

    國防科學技術大學發布了RoboCup中型組仿真平臺,基於ROS和Gazebo設計. 該平臺可以用於多機器人協作研究.參考資料如下: ROS新聞:1    http://www.ros.org/news ...

  2. Shell 整数比较、字符串比较

    整数比较  -eq       等于,如:if [ "$a" -eq "$b" ]  -ne       不等于,如:if [ "$a" - ...

  3. hive的strict模式;where,group by,having,order by同时使用的执行顺序

    主要限制三种情况 (1) 有partition的表查询需要加上where子句,筛选部分数据实现分区裁剪,即不允许全表全分区扫描,防止数据过大 (2) order by 执行时只产生一个reduce,必 ...

  4. 创建银行API

    DECLARE lc_output VARCHAR2(3000); lc_msg_dummy VARCHAR2(3000); lc_return_status VARCHAR2(3000); lc_m ...

  5. 文件操作:fseek函数和ftell函数

    1.fseek函数: int fseek(FILE * _File, long _Offset, int _Origin); 函数设置文件指针stream的位置.如果执行成功,stream将指向以fr ...

  6. 【Unity Shaders】Shader学习资源和Surface Shader概述

    写在前面 写这篇文章的时候,我断断续续学习Unity Shader半年了,其实还是个门外汉.我也能体会很多童鞋那种想要学好Shader却无从下手的感觉.在这个期间,我找到一些学习Shader的教程以及 ...

  7. C++中友元详解

    问题的提出 我们已知道类具备封装和信息隐 藏的特性.只有类的成员函数才能访问类的私有成员,程式中的其他函数是无法访问私有成员的.非成员函数能够访问类中的公有成员,但是假如将数据成员都定义 为公有的,这 ...

  8. 远程调试Eclipse插件的设置

    1. 被调试方建立一个命令行来运行Eclipse debugEclipse.cmd: eclipse.exe -nl zh_CN -vmargs -XX:+HeapDumpOnOutOfMemoryE ...

  9. redhat安装vsftpd

    一个小问题 rpm -qa|ftp 但是出现3个ftp 只安装了一个 关于网卡ip 首先,我们看到 网卡上面有个x 说明网络是有问题的 我们双击,看到 我们先把connected和connect at ...

  10. moonmq: 用go实现的高性能message queue

    介绍 moonmq是一个用go实现的高性能消息队列系统,后续准备用于我们消息推送服务以及各个后台的异步任务. 在设计上面,moonmq主要借鉴了rabbitmq以及rocketmq相关的思想,但是做了 ...