【算法】程序猿不写代码是不对的75
来源:互联网 发布:log4j写入mysql数据库 编辑:程序博客网 时间:2024/05/22 09:32
package com.kingdz.algorithm.time201706;/** * <pre> * 冒泡排序计数 * * http://judgecode.com/problems/1007 * * Given a permutation of integers from 0 to n - 1, sorting them is easy. * But what if you can only swap 2 adjacent integers? * Please calculate the minimal number of swaps. * * 每次都从头到尾进行比较,如果有交换则说明没有到最终的顺序则继续循环, * 直到某一轮比较没有交换,说明已经按照从小到大排序,则可以输出所需的交换的次数 * </pre> * * @author kingdz * */public class Algo29 { public static void main(String[] args) { String input = "3 2 1 0"; int times = swapToSort(input); System.out.println("need times:" + times); } private static int swapToSort(String input) { String[] arr = input.split(" "); int[] number = new int[arr.length]; for (int i = 0; i < number.length; i++) { number[i] = Integer.parseInt(arr[i]); } int sum = 0; int flag = 1; while (flag > 0) { flag = 0; for (int i = 0; i < number.length - 1; i++) { if (number[i] > number[i + 1]) { int temp = number[i]; number[i] = number[i + 1]; number[i + 1] = temp; flag++; } } sum = sum + flag; } return sum; }}
阅读全文
0 0
- 【算法】程序猿不写代码是不对的75
- 【算法】程序猿不写代码是不对的1
- 【算法】程序猿不写代码是不对的2
- 【算法】程序猿不写代码是不对的3
- 【算法】程序猿不写代码是不对的4
- 【算法】程序猿不写代码是不对的5
- 【算法】程序猿不写代码是不对的6
- 【算法】程序猿不写代码是不对的7
- 【算法】程序猿不写代码是不对的8
- 【算法】程序猿不写代码是不对的9
- 【算法】程序猿不写代码是不对的10
- 【算法】程序猿不写代码是不对的11
- 【算法】程序猿不写代码是不对的12
- 【算法】程序猿不写代码是不对的13
- 【算法】程序猿不写代码是不对的14
- 【算法】程序猿不写代码是不对的15
- 【算法】程序猿不写代码是不对的16
- 【算法】程序猿不写代码是不对的17
- Linux操作系统部分指令
- Letter Combinations of a Phone Number(回溯,dfs)
- ArrayList<Integer>如何转换为int[]数组
- [R语言]蒙特卡罗模拟检验CLRM假定下最小二乘量的BLUE性质
- Spring 中 @Controller 和 @RestController
- 【算法】程序猿不写代码是不对的75
- Spark Python 快速体验
- 12th 【基础】二进制计数
- 走进官方手册系列 --- 详解InnoDB针对不同类型的SQL所采取的锁策略
- 反向传播BP算法
- Mac下写bitcoin执行脚步
- SQL 从一个sql 语句结果中(作为AS一个表) 查询结果 ;按照count排序
- android 生成随机颜色
- MySQL之inner join、left join、right join、limit