Sure. Why not? If you want to lease two apartments, why wouldn't it be legal? In fact, I'm sure lots of folks do that. Maybe a large apartment in one location and another (perhaps small apartment) some distance away. Some people who work until 11 pm or midnight might not want to trek across Manhattan (or back into Brooklyn, etc.) to plop into bed, then have to get up at 6 am the next morning and make the reverse trip. Instead, they might have a small apartment near where they work to "crash" at when necessary.
Can you provide additional details on why you're wondering about the legality?
Hope that helps.