Partition Array by Odd and Even
来源:互联网 发布:电脑绘画卡通人物软件 编辑:程序博客网 时间:2024/06/02 03:24
Partition an integers array into odd number first and even number second.
Have you met this question in a real interview? Yes
Example
Given [1, 2, 3, 4], return [1, 3, 2, 4]
java
public class Solution { /* * @param nums: an array of integers * @return: nothing */ public void partitionArray(int[] nums) { // write your code here if (nums == null || nums.length == 0 || nums.length == 1) { 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; left++; right--; } } }}
python
class Solution: """ @param: nums: an array of integers @return: nothing """ def partitionArray(self, nums): # write your code here if nums == None or len(nums) == 0 or len(nums) == 1: return left, right = 0, len(nums) - 1 while left <= right: while left <= right and nums[left] % 2 != 0: left += 1 while left <= right and nums[right] % 2 == 0: right -= 1 if left <= right: nums[left], nums[right] = nums[right], nums[left]
阅读全文
0 0
- Partition Array by Odd and Even
- Partition Array by Odd and Even
- [Lintcode]Partition Array by Odd and Even
- Partition Array by Odd and Even
- #373 Partition Array by Odd and Even
- Partition Array by Odd and Even
- Partition Array by Odd and Even 解题报告
- LintCode on Array by Odd and Even
- [剑指offer][第三章][14]Partition Array by Odd and Even
- 373.Partition Array by Odd and Even-奇偶分割数组(容易题)
- Rearrange int array to put odd number to left and even to right
- Odd-even sort: Sorting by Exchanging
- CF621A - Wet Shark and Odd and Even
- A. Wet Shark and Odd and Even
- Wet Shark and Odd and Even
- Wet Shark and Odd and Even
- Swap all odd and even bits
- A even and odd(13.7.6)
- HTML的DIV如何实现垂直居中
- JAVA_the user operation is waiting怎么办
- Linux如何更新软件源
- Linux 将一般的用户加入sudo组is_not_in_the_sudoers_file._This_incident_will_be_reported解决方法
- 火狐浏览器Firefox 如何下载网页的SWF视频,硅谷动力的网站视频怎么下载
- Partition Array by Odd and Even
- 洛谷 1731 生日蛋糕 dfs+剪枝
- Solidworks drwdot文件如何打开,如何制作Solidworks工程图模板
- 1055. 集体照 (25)
- Office EXCEL 2010如何取消宏密码保护
- Office EXCEL 2010如何启用宏编辑器,打开VB编辑器
- Office EXCEL 表格如何设置某个单元格是选择项,如何设置二级下拉菜单 MS Office版
- Error:Tag <uses-permission> attribute name has invalid character ' '.
- 每天一道LeetCode-----最长回文子串/序列,从头开始的最长回文子串长度