lintcode,奇偶分割数组
来源:互联网 发布:n反斜杠百分之d c语言 编辑:程序博客网 时间:2024/06/10 07:37
分割一个整数数组,使得奇数在前偶数在后。
解题思路:前后指针向中间靠
一刷ac
public class Solution { /** * @param nums: an array of integers * @return: nothing */ public void partitionArray(int[] nums) { if(nums == null || nums.length == 0) return; int left = 0; int right = nums.length - 1; while(left < right){ if(nums[left] % 2 == 0 && nums[right] % 2 == 1){ int tmp = nums[left]; nums[left] = nums[right]; nums[right] = tmp; left++; right--; }else if(nums[left] % 2 == 1 && nums[right] % 2 == 0){ left++; right--; }else if(nums[left] % 2 == 1 && nums[right] % 2 == 1){ left++; }else if(nums[left] % 2 == 0 && nums[right] % 2 == 0){ right--; } } }}
public class Solution { /** * @param nums: an array of integers * @return: nothing */ public void partitionArray(int[] nums) { if ( nums.length == 0) return; int left = 0; int right = nums.length - 1; while (left < right){ while (left < right && nums[left]%2 != 0){ left++; } while (left < right && nums[right]%2 == 0){ right--; } if(left < right){ int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; } } }}
0 0
- lintcode ----奇偶分割数组
- lintcode,奇偶分割数组
- Lintcode奇偶分割数组
- 奇偶分割数组-LintCode
- lintcode-奇偶分割数组-373
- 奇偶分割数组(LintCode)
- [LintCode]373.奇偶分割数组
- LintCode-剑指Offer-(373)奇偶分割数组
- 题目:奇偶分割数组
- 奇偶分割数组
- LintCode_373_奇偶分割数组
- 奇偶分割数组
- 奇偶分割数组
- 奇偶分割数组
- LintCode373:奇偶分割数组
- 奇偶分割数组
- 奇偶分割数组
- 奇偶分割数组
- Linux安装及配置vsftpd
- 利用阿里大于发送短信验证码(java)
- DEMO大全,超赞【申明:来源于网络】
- 【ife】任务二:零基础HTML及CSS编码(一)
- # 安卓透明状态栏
- lintcode,奇偶分割数组
- 编码方式
- Android 自定义View 条形图
- 修改Hadoop配置文件
- <center>oc内存管理机制<:center>
- 坚持#第125天~坚守梦想!
- java--数据类型和运算符备忘知识点
- linux中文件上传
- 安卓技术文章积累